Base class for entities receiving data.
Not intended to be as a final class. Not thread safe.
#define BOOST_TEST_MODULE zeroeq_receiver
#include "broker.h"
#include <chrono>
bool gotOne = false;
bool gotTwo = false;
void onEvent1() { gotOne = true; }
void onEvent2() { gotTwo = true; }
bool& var1, bool& var2, const int line )
{
gotOne = false;
gotTwo = false;
const auto startTime = std::chrono::high_resolution_clock::now();
for( ;; )
{
BOOST_CHECK( publisher.
publish( test::Echo( test::echoMessage )));
if( var1 && var2 )
break;
const auto endTime = std::chrono::high_resolution_clock::now();
const auto elapsed =
std::chrono::nanoseconds( endTime - startTime ).count() / 1000000;
if( elapsed > 2000 )
break;
}
BOOST_CHECK_MESSAGE( var1, (&var1 == &gotOne ? "Event 1" : "Event 2") <<
" not received (l." << line << ")" );
if( &var1 != &var2 )
BOOST_CHECK_MESSAGE( var2, (&var2 == &gotOne ? "Event 1" : "Event 2") <<
" not received (l." << line << ")" );
}
bool& var, const int line )
{
testReceive( publisher, receiver, var, var, line );
}
BOOST_AUTO_TEST_CASE(test_two_subscribers)
{
subscriber1 );
BOOST_CHECK( subscriber1.subscribe( test::Echo::IDENTIFIER(), &onEvent1 ));
BOOST_CHECK( subscriber2.subscribe( test::Echo::IDENTIFIER(), &onEvent2 ));
testReceive( publisher, subscriber1, gotOne, gotTwo, __LINE__ );
testReceive( publisher, subscriber2, gotOne, gotTwo, __LINE__ );
}
BOOST_AUTO_TEST_CASE(test_publisher_routing)
{
*subscriber1 );
BOOST_CHECK( subscriber1->
subscribe( test::Echo::IDENTIFIER(), &onEvent1 ));
BOOST_CHECK( subscriber2.subscribe( test::Echo::IDENTIFIER(), &onEvent2 ));
testReceive( publisher, *subscriber1, gotTwo, __LINE__ );
BOOST_CHECK( !gotOne );
testReceive( publisher, subscriber2, gotTwo, __LINE__ );
BOOST_CHECK( !gotOne );
delete subscriber1;
testReceive( publisher, subscriber2, gotTwo, __LINE__ );
BOOST_CHECK( !gotOne );
}