Packets: extended description of bad_cast exception in Packet.as()
[senf.git] / senf / PPI / QueueingSocketSink.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief QueueingSocketSink unit tests */
25
26 #include "QueueingSocketSink.hh"
27
28 // Custom includes
29 #include <senf/Socket/Protocols/INet/UDPSocketHandle.hh>
30 #include <senf/Socket/Protocols/INet/ConnectedUDPSocketHandle.hh>
31 #include "DebugModules.hh"
32 #include "SocketSink.hh"
33 #include "Setup.hh"
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 namespace ppi = senf::ppi;
41 namespace module = ppi::module;
42 namespace debug = module::debug;
43 namespace scheduler = senf::scheduler;
44
45 namespace {
46     void runPPI(senf::ClockService::clock_type t)
47     {
48         scheduler::TimerEvent timeout(
49                 "test-timeout", &scheduler::terminate, scheduler::now() + t);
50         ppi::run();
51     }
52
53     int base_pid = 0;
54
55     unsigned port(unsigned i)
56     {
57         if (! base_pid)
58             base_pid = ::getpid();
59         return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
60     }
61
62     std::string localhost4str(unsigned i)
63     {
64         return (boost::format("localhost:%d") % port(i)).str();
65     }
66
67     struct TestingConnectedDgramWriter
68         : public ppi::ConnectedDgramWriter
69     {
70         bool throttled;
71
72         bool operator()(Handle handle, PacketType const & packet)
73         {
74             if (throttled)
75                 return false;
76             return ConnectedDgramWriter::operator()( handle, packet);
77         }
78
79         TestingConnectedDgramWriter(){
80             throttled = false;
81         }
82     };
83 }
84
85 SENF_AUTO_UNIT_TEST(passiveQueueingSocketSink)
86 {
87     senf::ConnectedUDPv4ClientSocketHandle os (senf::noinit);
88
89     senf::ConnectedUDPv4ClientSocketHandle outputSocket (
90             senf::INet4SocketAddress( localhost4str(0)));
91     module::PassiveQueueingSocketSink<TestingConnectedDgramWriter> udpSink (
92             os, ppi::FIFOQueueingAlgorithm::create());
93     
94     // test re-assignment of socket
95     udpSink.handle( outputSocket);
96     
97     udpSink.writer().throttled = false;
98     debug::ActiveSource source;
99     ppi::connect(source, udpSink);
100     senf::ppi::init();
101
102     std::string data ("TEST");
103     senf::Packet p (senf::DataPacket::create(data));
104
105     senf::UDPv4ClientSocketHandle inputSocket;
106     inputSocket.bind(senf::INet4SocketAddress(localhost4str(0)));
107
108     source.submit(p);
109
110     std::string input (inputSocket.read());
111     BOOST_CHECK_EQUAL( data, input );
112     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 0);
113
114     udpSink.writer().throttled = true;
115
116     source.submit(p);
117     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 1);
118
119     for (int n = 0; n < 100; n++) {
120         source.submit(p);
121     }
122     // queue default size is 64
123     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 64);
124
125     udpSink.writer().throttled = false;
126
127     runPPI( senf::ClockService::milliseconds(250));
128
129     inputSocket.blocking(false);
130     while (true) {
131         input = inputSocket.read();
132         if (input.empty()) break;
133         BOOST_CHECK_EQUAL( data, input );
134     }
135
136     runPPI( senf::ClockService::milliseconds(250));
137     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 0);
138 }
139
140
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
142 #undef prefix_
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // comment-column: 40
149 // c-file-style: "senf"
150 // indent-tabs-mode: nil
151 // ispell-local-dictionary: "american"
152 // compile-command: "scons -u test"
153 // End: