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