How to test an async event

105 views
Skip to first unread message

Barzo

unread,
Apr 5, 2013, 6:00:25 AM4/5/13
to catch...@googlegroups.com
Hi,

first of all my compliments for the project, I discover it just yesterday and it seems great!
I'm quite new to TDD ('cause the time is always too short to write tests :-( )
Now, I'm developing an SDK and I have some object for network communication.
This object works in an event-driven approach:


class MsgHandler : public MessageEventHandler
{
public:
 
MsgHandler() {
   
// Register the messages I want to manage
   
this->RegisterMsg(E_MSG_EVENT_CONNECT, Poco::delegate(this, &MsgHandler::OnMsgEventConnect));
 
}
 
// Messages Handlers
 
void OnMsgEventConnect(const void* pSender, TArgs& args) {
    cout
<< "MsgHandler receive MSG_EVENT_CONNECT" << endl;
 
};
};


class SessionFactory : public ISessionFactory
{
public:
 
~SessionFactory() {};
 
SessionFactory(MessageEventHandler* handler) : m_handler(handler) {};

 
/// Creates an instance of a subclass of Session,
 
/// using the given message handler.
 
inline Session* createSession(const IMessageDispatcher* dispatcher) {
   
Session* s = new Session(dispatcher);
    s
->AddMsgHandler( m_handler );
   
return s;
 
};

private:
 
MessageEventHandler* m_handler;
};


TEST_CASE
( "NetMessageDispatcherTest/Main", "NetMessageDispatcher Test" ) {

 
Application& app = Application::instance();
 
SessionFactory* sf = new SessionFactory( new MsgHandler() );
 
IMessageFactory* mf = new PBMessageFactory();
 
NetMessageDispatcher* server = new NetMessageDispatcher(sf, mf, app.logger(), EDispatcherType::E_DISP_SERVER);
 
NetMessageDispatcher* client = new NetMessageDispatcher(sf, mf, app.logger(), EDispatcherType::E_DISP_CLIENT);

  REQUIRE
( server->Init("LOCALHOST", "TestServer") == EC_NO_ERRORS );
  REQUIRE
( server->Connect(8000, "127.0.0.1") == EC_NO_ERRORS );

  REQUIRE
( client->Init("LOCALHOST", "TestClient") == EC_NO_ERRORS );
  REQUIRE
( client->Connect(8000, "127.0.0.1") == EC_NO_ERRORS );
}

The NetMessageDispatcher works on Sections object which works on MessageEventHandlers to notify messages.
Which is the right way to test the MsgHandler::OnMsgEventConnect handler method?

Regards,
Daniele.

Phil Nash

unread,
Apr 8, 2013, 1:02:03 PM4/8/13
to catch...@googlegroups.com
Hi Daniele,

Thanks for the compliments.
As for your question - it looks like you need a mock object for the MsgHandler.
From the code you posted it looks like that shouldn't be too hard to do manually (MsgHandler, or some version of it, is part of the test code and you can put test specific code in there). It's not clear whether this is a simplified version of your real code where that is not so easy?

Assuming you can do that the next challenge is to deal with the asynchronisity. Obviously at this point we're not dealing with a unit test (you could mock your NetMessageDispatcher too, but that would probably defeat the purpose). As long as you accept that it's fine.
So you'd probably need to introduce some sort of signaling mechanism that that OnMsgEventConnect could raise and the test case code could wait on. I don't know what threading APIs you have available to your project, although I see you're using POCO, which has its own threading support so you'd probably be using that. I'm not familiar enough with POCO to advise there - but you'd probably want to be able to specify a timeout too.

So then the next line in your test case code would be to wait on that signal and check the status of your MsgHandler mock (or fail if it times out - you can use the explicit FAIL macro for that).

HTH,

Phil

Barzo

unread,
Apr 9, 2013, 3:16:42 AM4/9/13
to catch...@googlegroups.com
Hi Phil,
thanks a lot for your reply!
As I said, I'm new on TDD, so I discover after have posted what is a mock object! :-P
Now, after some reading, I'm just a little bit more competent and my situation is exactly as you described! So I'm going to code it!

PS. You think to add a "mocking feature" in the future?

Thanks again.
Cheers,
Daniele.

Phil Nash

unread,
Apr 9, 2013, 3:22:47 AM4/9/13
to catch...@googlegroups.com
Glad I could help, Daniele,

I don't currently have plans to add mocks to Catch. They're a big feature, largely orthogonal to the testing framework itself.
Most third party mocking frameworks should worth with Catch.
Reply all
Reply to author
Forward
0 new messages