How do I repeatedly expect a sequence of calls?
Asked Answered
C

1

7

I'm writing a test to verify the sequence of calls on an image processing thread. The relevant piece of test code looks like this:

Sequence s1, s2;
...
EXPECT_CALL(*mMockVideoSource, getFrame()).InSequence(s2).WillRepeatedly(Return(mFakeBuffer));
EXPECT_CALL(*mMockProcessor, processFrame(_,_)).InSequence(s2).WillRepeatedly(Return(0));
EXPECT_CALL(*mMockVideoSource, releaseFrame(_)).Times(AnyNumber()).InSequence(s2);
...

In this case, the sequence of calls is extremely important. getFrame(), processFrame() and releaseFrame() must be called in this order. Unfortunately, the above code isn't really accomplishing what I want. The above code will allow getFrame() to be called repeatedly before the call to processFrame(), and a call to getFrame() after releaseFrame() is considered an error since it breaks the sequence.

Is there a way to expect a specific sequence of calls to be made repeatedly? I don't care how many times the sequence is executed, so long as the functions are called in order: get, process, release, get, process, release...

Come answered 27/5, 2014 at 17:59 Comment(2)
Why are you using WillRepeatedly then instead of e.g. WillOnce?Lycurgus
My goal is to allow the expectations to hold repeatedly. If I use WillOnce, each call will only be allowed to be called one time.Come
T
8

You can create side action on calling your mock (https://code.google.com/p/googlemock/wiki/CookBook#Combining_Actions) and some global state like "lastAction".

Side action will look like:

void checkSequenceCorrectness(ActionType currAction)
{
    if (currAction == PROCESS_FRAME) EXPECT_EQ(GET_FRAME, lastAction);
    (more ifs)
    ...

    lastAction = currAction;
}

You can bind it to mock by:

EXPECT_CALL(*mMockProcessor, processFrame(_,_))
    .WillRepeatedly(DoAll
        Return(0), 
        Invoke(checkSequenceCorrectness(PROCESS_FRAME)));
Thor answered 19/6, 2014 at 5:49 Comment(1)
Oh, nice! I'll give this a try tomorrow. It certainly looks promising.Come

© 2022 - 2024 — McMap. All rights reserved.