mercredi 12 septembre 2018

Testing Delayed Actions with GTest

I have scenario template like :

    // this function is called as timer callback
void funcToBeCalled ();

Timer t;
void
push_data ()
{
  if (t.isRunning ())
    {
      return;
    }
  t.start (oneshot = true);
}

//TestCase
      EXPECT_CALL(mockObj,funcToBeCalled).Times(1);
      push_data ();
      push_data ();
      push_data ();

Purpose is when push_data() is called n times in time period t it will be bound to timer callback so only once funcToBeCalled() is called all push_data request after t time is ignored. The above case is a prototype version and tests are succesfull.You can assume that mock setup is correct.

But I want to have a test case like :

#define TIMER_TIMEOUT 10
bool funcToBeCalled ();


Timer t;
void
push_data ()
{
  if (t.isRunning ())
    {
      return;
    }
  t.start (TIMER_TIMEOUT, oneshot = true);
}



 // TestCase
          SharedSignal msgnal;
          EXPECT_CALL (mockObj,
                   funcToBeCalled).Times (2).
            WillOnce (Return (true)).WillOnce (SetSignal (msgnal));
          push_data ();
          push_data ();
          push_data ();

          Sleep (TIMER_TIMEOUT);
          push_data ();
          push_data ();
          push_data ();

          msgnal.wait (5 * TIMER_TIMEOUT);

Sleep ensures that first time window is expired second call to funcToBeCalled() should be made.

I do not want to use verify Mock::VerifyAndClearExpectations it does not simulate one long time window,instead it simulates two time windows from scratch.

How can I achieve this test case with Google Test Framework?

Aucun commentaire:

Enregistrer un commentaire