jeudi 26 mars 2015

Mocking Async Calls [duplicate]


This question already has an answer here:




I am writing tests for a project using Google Mock. There are async calls and i am having a hard time checking the sequence of function calls. Here is an example run:



  1. service::start

  2. manager::start

  3. manager::apples //first thread

  4. manager::egg //second thread

  5. service::doSomething

  6. apples::start //async

  7. apples::create_apples //async

  8. eggs::start //async

  9. eggs::create_eggs //async

  10. service::doAnother

  11. eggs::pack_eggs //async

  12. apples::pack_apples //async


I checked google mock CheatSheet: http://ift.tt/1ybj1OV But i can't get it to work as I wanted to.



sequence_check_mock obj;
mock_obj_ = &obj;
{
Sequence s1, s2;
EXPECT_CALL( *mock_obj_, service_start() ).InSequence( s1, s2 );
EXPECT_CALL( *mock_obj_, manager_start() ).InSequence( s1, s2 );
EXPECT_CALL( *mock_obj_, manager_apples() ).InSequence( s1, s2 );
EXPECT_CALL( *mock_obj_, manager_eggs() ).InSequence( s1, s2 );
EXPECT_CALL( *mock_obj_, service_doSomething() ).InSequence( s1, s2 );
EXPECT_CALL( *mock_obj_, apples_start() ).InSequence( s1 );
EXPECT_CALL( *mock_obj_, apples_create_apples() ).InSequence( s1 );
EXPECT_CALL( *mock_obj_, eggs_start() ).InSequence( s2 );
EXPECT_CALL( *mock_obj_, eggs_create_eggs() ).InSequence( s2 );
EXPECT_CALL( *mock_obj_, service_doAnother() ).InSequence( s1, s2 );
EXPECT_CALL( *mock_obj_, eggs_pack_eggs() ).InSequence( s2 );
EXPECT_CALL( *mock_obj_, apples_pack_apples() ).InSequence( s1 );
}


The checking is ok until service_doAnother. But sometimes, service_doAnother is called after eggs_pack_eggs or apples_pack_apples so the test would fail. Since in the sequence, it is expected that doAnother should be called before.



  1. eggs::pack_eggs //async

  2. apples::pack_apples //async

  3. service::doAnother


Aucun commentaire:

Enregistrer un commentaire