4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
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.
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.
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.
24 \brief QueueingSocketSink unit tests */
26 #include "QueueingSocketSink.hh"
29 #include <senf/Socket/Protocols/INet/UDPSocketHandle.hh>
30 #include <senf/Socket/Protocols/INet/ConnectedUDPSocketHandle.hh>
31 #include "DebugModules.hh"
32 #include "SocketSink.hh"
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
39 ///////////////////////////////cc.p////////////////////////////////////////
40 namespace ppi = senf::ppi;
41 namespace module = ppi::module;
42 namespace debug = module::debug;
43 namespace scheduler = senf::scheduler;
46 void runPPI(senf::ClockService::clock_type t)
48 scheduler::TimerEvent timeout(
49 "test-timeout", &scheduler::terminate, scheduler::now() + t);
55 unsigned port(unsigned i)
58 base_pid = ::getpid();
59 return 23456u + (((base_pid^(base_pid>>8)^(base_pid>>16)^(base_pid>>24))&0xff)<<2) + i;
62 std::string localhost4str(unsigned i)
64 return (boost::format("localhost:%d") % port(i)).str();
67 struct TestingConnectedDgramWriter
68 : public ppi::ConnectedDgramWriter
72 bool operator()(Handle handle, PacketType const & packet)
76 return ConnectedDgramWriter::operator()( handle, packet);
79 TestingConnectedDgramWriter(){
85 SENF_AUTO_UNIT_TEST(passiveQueueingSocketSink)
87 senf::ConnectedUDPv4ClientSocketHandle os(senf::noinit);
89 senf::ConnectedUDPv4ClientSocketHandle outputSocket (
90 senf::INet4SocketAddress( localhost4str(0)));
91 module::PassiveQueueingSocketSink<TestingConnectedDgramWriter> udpSink (
92 os, ppi::FIFOQueueingAlgorithm::create());
94 // test re-assignment of socket
95 udpSink.handle( outputSocket);
97 udpSink.writer().throttled = false;
98 debug::ActiveSource source;
99 ppi::connect(source, udpSink);
102 std::string data ("TEST");
103 senf::Packet p (senf::DataPacket::create(data));
105 senf::UDPv4ClientSocketHandle inputSocket;
106 inputSocket.bind(senf::INet4SocketAddress(localhost4str(0)));
110 std::string input (inputSocket.read());
111 BOOST_CHECK_EQUAL( data, input );
112 BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 0);
114 udpSink.writer().throttled = true;
117 BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 1);
119 for( int n = 0; n < 100; n++){
122 // queue default size is 64
123 BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 64);
125 udpSink.writer().throttled = false;
127 runPPI( senf::ClockService::milliseconds(200));
129 input = inputSocket.read();
130 BOOST_CHECK_EQUAL( data, input );
131 BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 0);
135 ///////////////////////////////cc.e////////////////////////////////////////
142 // comment-column: 40
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"