180af5b4c58b9c5f6856c7f9c70d2890c12ac836
[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 ///////////////////////////////cc.p////////////////////////////////////////
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 }
80
81 SENF_AUTO_UNIT_TEST(passiveQueueingSocketSink)
82 {
83     senf::ConnectedUDPv4ClientSocketHandle outputSocket (
84             senf::INet4SocketAddress( localhost4str(0)));
85     ppi::FIFOQueueingAlgorithm<TestingConnectedDgramWriter::PacketType> queueingAlgorithm ( 100);
86     module::PassiveQueueingSocketSink<TestingConnectedDgramWriter> udpSink (
87             outputSocket, queueingAlgorithm);
88     udpSink.writer().throttled = false;
89     debug::ActiveSource source;
90     ppi::connect(source, udpSink);
91     senf::ppi::init();
92
93     std::string data ("TEST");
94     senf::Packet p (senf::DataPacket::create(data));
95
96     senf::UDPv4ClientSocketHandle inputSocket;
97     inputSocket.bind(senf::INet4SocketAddress(localhost4str(0)));
98
99     source.submit(p);
100
101     std::string input (inputSocket.read());
102     BOOST_CHECK_EQUAL( data, input );
103     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 0);
104
105     udpSink.writer().throttled = true;
106
107     source.submit(p);
108     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 1);
109
110     udpSink.writer().throttled = false;
111
112     runPPI( senf::ClockService::milliseconds(200));
113
114     input = inputSocket.read();
115     BOOST_CHECK_EQUAL( data, input );
116     BOOST_CHECK_EQUAL( udpSink.qAlgorithm().size(), 0);
117 }
118
119
120 ///////////////////////////////cc.e////////////////////////////////////////
121 #undef prefix_
122
123 \f
124 // Local Variables:
125 // mode: c++
126 // fill-column: 100
127 // comment-column: 40
128 // c-file-style: "senf"
129 // indent-tabs-mode: nil
130 // ispell-local-dictionary: "american"
131 // compile-command: "scons -u test"
132 // End: