From: g0dil Date: Thu, 16 Aug 2007 15:47:37 +0000 (+0000) Subject: PPI: More unit tests X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=48bbf27e9e89d6eba4754fba65d70a15b115ac8b;p=senf.git PPI: More unit tests git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@393 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/PPI/DebugModules.cci b/PPI/DebugModules.cci index 278aac3..bbc3e9d 100644 --- a/PPI/DebugModules.cci +++ b/PPI/DebugModules.cci @@ -87,7 +87,8 @@ prefix_ void senf::ppi::module::debug::PassivePacketSource::request() prefix_ void senf::ppi::module::debug::PassivePacketSource::init() { - output.throttle(); + if (empty()) + output.throttle(); } /////////////////////////////////////////////////////////////////////////// diff --git a/PPI/EventManager.test.cc b/PPI/EventManager.test.cc index ea9b886..5a4b771 100644 --- a/PPI/EventManager.test.cc +++ b/PPI/EventManager.test.cc @@ -36,7 +36,9 @@ ///////////////////////////////cc.p//////////////////////////////////////// BOOST_AUTO_UNIT_TEST(eventManager) -{} +{ + // Tested indirectly by Route.test.cc and Module.test.cc +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/PPI/Events.test.cc b/PPI/Events.test.cc index 5f3b4f2..1c687cd 100644 --- a/PPI/Events.test.cc +++ b/PPI/Events.test.cc @@ -36,7 +36,9 @@ ///////////////////////////////cc.p//////////////////////////////////////// BOOST_AUTO_UNIT_TEST(events) -{} +{ + // Tested in Route.test.cc +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/PPI/Module.test.cc b/PPI/Module.test.cc index 01434df..1fe9b7b 100644 --- a/PPI/Module.test.cc +++ b/PPI/Module.test.cc @@ -25,6 +25,9 @@ //#include "Module.test.ih" // Custom includes +#include "DebugEvent.hh" +#include "DebugModules.hh" +#include "Setup.hh" #include "Module.hh" #include @@ -34,8 +37,45 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// +namespace ppi = senf::ppi; +namespace connector = ppi::connector; +namespace debug = ppi::module::debug; + +namespace { + class TestModule : public ppi::module::Module + { + public: + connector::ActiveOutput output; + + ppi::DebugEvent event; + + TestModule() { + noroute(output); + registerEvent(&TestModule::onEvent, event); + } + + void onEvent() { + output(senf::DataPacket::create()); + } + + using ppi::module::Module::eventTime; + }; +} + BOOST_AUTO_UNIT_TEST(module) -{} +{ + // route and registerEvent are tested in Route.test.cc + + TestModule tester; + debug::PassivePacketSink sink; + ppi::connect(tester.output, sink.input); + ppi::init(); + + tester.event.trigger(); + BOOST_CHECK_EQUAL( sink.size(), 1u ); + BOOST_CHECK_EQUAL( (boost::posix_time::microsec_clock::universal_time() - + tester.eventTime()).total_seconds(), 0 ); +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/PPI/SocketReader.test.cc b/PPI/SocketReader.test.cc index 7738e6f..1d58e1c 100644 --- a/PPI/SocketReader.test.cc +++ b/PPI/SocketReader.test.cc @@ -27,7 +27,12 @@ //#include "SocketReader.test.ih" // Custom includes +#include +#include "Socket/Protocols/INet/UDPSocketHandle.hh" +#include "Scheduler/Scheduler.hh" #include "SocketReader.hh" +#include "DebugModules.hh" +#include "Setup.hh" #include #include @@ -35,8 +40,38 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// +namespace ppi = senf::ppi; +namespace connector = ppi::connector; +namespace module = ppi::module; +namespace debug = module::debug; + +namespace { + void timeout() { + senf::Scheduler::instance().terminate(); + } +} + BOOST_AUTO_UNIT_TEST(socketReader) -{} +{ + senf::UDPv4ClientSocketHandle inputSocket; + inputSocket.bind(senf::INet4SocketAddress("localhost:44344")); + inputSocket.blocking(false); + module::ActiveSocketReader<> udpReader(inputSocket); + debug::PassivePacketSink sink; + ppi::connect(udpReader.output, sink.input); + + std::string data ("TEST"); + + senf::UDPv4ClientSocketHandle outputSocket; + outputSocket.writeto(senf::INet4SocketAddress("localhost:44344"),data); + senf::Scheduler::instance().timeout(1000, &timeout); + senf::ppi::run(); + + BOOST_REQUIRE( ! sink.empty() ); + BOOST_CHECK_EQUAL( sink.front().data().size(), data.size() ); + BOOST_CHECK( std::equal( sink.front().data().begin(), sink.front().data().end(), + data.begin()) ); +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/PPI/SocketWriter.ct b/PPI/SocketWriter.ct index c2dc70f..9c01233 100644 --- a/PPI/SocketWriter.ct +++ b/PPI/SocketWriter.ct @@ -31,6 +31,26 @@ ///////////////////////////////ct.p//////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// +// senf::ppi::module::ActiveSocketWriter + +template +prefix_ senf::ppi::module::ActiveSocketWriter::ActiveSocketWriter(Handle handle) + : handle_(handle), event_(handle_, IOEvent::Write), writer_() +{ + registerEvent( &ActiveSocketWriter::write, event_ ); + route(input, event_); +} + +//////////////////////////////////////// +// private members + +template +prefix_ void senf::ppi::module::ActiveSocketWriter::write() +{ + writer_(handle_,input()); +} + +/////////////////////////////////////////////////////////////////////////// // senf::ppi::module::PassiveSocketWriter template diff --git a/PPI/SocketWriter.hh b/PPI/SocketWriter.hh index f2f8611..233322c 100644 --- a/PPI/SocketWriter.hh +++ b/PPI/SocketWriter.hh @@ -95,6 +95,12 @@ namespace module { ActiveSocketWriter(Handle handle); ///< Create new writer for the given handle /**< Data will be written to \a handle using \a Writer. \param[in] handle Handle to write data to */ + private: + void write(); + + Handle handle_; + IOEvent event_; + Writer writer_; }; /** \brief Output module writing data to arbitrary FileHandle diff --git a/PPI/SocketWriter.test.cc b/PPI/SocketWriter.test.cc index a6f6055..5e40772 100644 --- a/PPI/SocketWriter.test.cc +++ b/PPI/SocketWriter.test.cc @@ -27,7 +27,12 @@ //#include "SocketWriter.test.ih" // Custom includes +#include "Socket/Protocols/INet/UDPSocketHandle.hh" +#include "Socket/Protocols/INet/ConnectedUDPSocketHandle.hh" +#include "SocketReader.hh" +#include "DebugModules.hh" #include "SocketWriter.hh" +#include "Setup.hh" #include #include @@ -35,8 +40,59 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -BOOST_AUTO_UNIT_TEST(socketWriter) -{} +namespace ppi = senf::ppi; +namespace connector = ppi::connector; +namespace module = ppi::module; +namespace debug = module::debug; + +namespace { + void timeout() { + senf::Scheduler::instance().terminate(); + } +} + +BOOST_AUTO_UNIT_TEST(passiveSocketWriter) +{ + senf::ConnectedUDPv4ClientSocketHandle outputSocket ( + senf::INet4SocketAddress("localhost:44344")); + module::PassiveSocketWriter<> udpWriter(outputSocket); + debug::ActivePacketSource source; + ppi::connect(source.output, udpWriter.input); + + std::string data ("TEST"); + senf::Packet p (senf::DataPacket::create(data)); + + senf::UDPv4ClientSocketHandle inputSocket; + inputSocket.bind(senf::INet4SocketAddress("localhost:44344")); + inputSocket.blocking(false); + senf::ppi::init(); + source.submit(p); + + std::string input (inputSocket.read()); + BOOST_CHECK_EQUAL( data, input ); +} + +BOOST_AUTO_UNIT_TEST(activeSocketWriter) +{ + senf::ConnectedUDPv4ClientSocketHandle outputSocket ( + senf::INet4SocketAddress("localhost:44344")); + module::ActiveSocketWriter<> udpWriter(outputSocket); + debug::PassivePacketSource source; + ppi::connect(source.output, udpWriter.input); + + std::string data ("TEST"); + senf::Packet p (senf::DataPacket::create(data)); + + senf::UDPv4ClientSocketHandle inputSocket; + inputSocket.bind(senf::INet4SocketAddress("localhost:44344")); + inputSocket.blocking(false); + senf::Scheduler::instance().timeout(1000, &timeout); + source.submit(p); + senf::ppi::run(); + + std::string input (inputSocket.read()); + BOOST_CHECK_EQUAL( data, input ); +} ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_