Re: [googlemock: 430] Timeout on Expect

1,036 views
Skip to first unread message
Message has been deleted

Zhanyong Wan (λx.x x)

unread,
Jun 23, 2009, 1:06:03 PM6/23/09
to Alexis, Google C++ Mocking Framework, ade...@imris.com
On Tue, Jun 23, 2009 at 9:52 AM, Alexis<alexi...@gmail.com> wrote:
>
> Hi,
>
>  Is there a way to have a timeout on an EXPECT? I am using Google Mock

There's no built-in support for that, but you should be able to do it
by using googlemock constructs creatively.

> to test an interface to some real hardware and part of my interface
> definition is a maximum timeout period.
>
> Can I write:
> EXPECT_CALL(foo, Bar(_))
>    .TimeOut(100); // in ms

Here's one idea:

ACTION_P(TimeOut, ms) {
ASSERT_LE(GetElapsedTime(), ms);
}
...
EXPECT_CALL(foo, Bar(_)).WillOnce(TimeOut(100));

>
> Thanks,
>
> Alexis
>
>
>

--
Zhanyong

Message has been deleted

Zhanyong Wan (λx.x x)

unread,
Jun 23, 2009, 1:24:51 PM6/23/09
to Alexis, Google C++ Mocking Framework, Alexis Denis
On Tue, Jun 23, 2009 at 10:20 AM, Alexis<alexi...@gmail.com> wrote:
>
> Hi,
>
> Thanks. So to make sure I get this right, this test will fail if I
> didn't get the callback Bar within 100 ms?

To be precise, the test will fail if you get the callback and the time
it takes to get the callback is >= 100 ms.

If it takes 500 ms to get the backback, the test will take 500 ms to fail.

If you want a hard time-out, you can do something like:

sleep(100);
Mock::VerifyAndClearExpectations(&foo);

>
> Alexis
>
> On Jun 23, 12:06 pm, Zhanyong Wan (λx.x x) <w...@google.com> wrote:

--
Zhanyong

Vlad Losev

unread,
Jun 23, 2009, 1:27:41 PM6/23/09
to Zhanyong Wan (λx.x x), Alexis, Google C++ Mocking Framework, ade...@imris.com


2009/6/23 Zhanyong Wan (λx.x x) <w...@google.com>

An alternative is to use a custom matcher:

MATCHER_P(WithinTimeout, ms, "called within %(ms)d ms") {
  return GetElapsedTime() <= ms;
}

TEST(..., ...) {
  ...
  EXPECT_CALL(foo, Bar(_)).With(WithinTimeout(100));
}

You get a slightly more meaningful error message in this case.


>
> Thanks,
>
> Alexis
>
>
>



--
Zhanyong

Message has been deleted

Zhanyong Wan (λx.x x)

unread,
Jun 23, 2009, 2:50:35 PM6/23/09
to Vlad Losev, Alexis, Google C++ Mocking Framework, ade...@imris.com
2009/6/23 Vlad Losev <vl...@google.com>:

>
>
> 2009/6/23 Zhanyong Wan (λx.x x) <w...@google.com>
>>
>> On Tue, Jun 23, 2009 at 9:52 AM, Alexis<alexi...@gmail.com> wrote:
>> >
>> > Hi,
>> >
>> >  Is there a way to have a timeout on an EXPECT? I am using Google Mock
>>
>> There's no built-in support for that, but you should be able to do it
>> by using googlemock constructs creatively.
>>
>> > to test an interface to some real hardware and part of my interface
>> > definition is a maximum timeout period.
>> >
>> > Can I write:
>> > EXPECT_CALL(foo, Bar(_))
>> >    .TimeOut(100); // in ms
>>
>> Here's one idea:
>>
>> ACTION_P(TimeOut, ms) {
>>  ASSERT_LE(GetElapsedTime(), ms);
>> }
>> ...
>> EXPECT_CALL(foo, Bar(_)).WillOnce(TimeOut(100));
>
> An alternative is to use a custom matcher:
>
> MATCHER_P(WithinTimeout, ms, "called within %(ms)d ms") {
>   return GetElapsedTime() <= ms;
> }
>
> TEST(..., ...) {
>   ...
>   EXPECT_CALL(foo, Bar(_)).With(WithinTimeout(100));
> }

A matcher should be a pure function of its input (i.e. given the same
value, it should always yield the same result, no matter when it is
invoked). WithinTimeout() depends on a mutable global state (the
time), and thus is not a valid matcher. I wouldn't recommend it as
googlemock depends on matchers being stateless - we probably should
document this somewhere.

> You get a slightly more meaningful error message in this case.
>
>>
>> >
>> > Thanks,
>> >
>> > Alexis
>> >
>> >
>> >
>>
>>
>>
>> --
>> Zhanyong
>
>

--
Zhanyong

Zhanyong Wan (λx.x x)

unread,
Jun 23, 2009, 3:05:17 PM6/23/09
to Vlad Losev, Alexis, Google C++ Mocking Framework, ade...@imris.com
2009/6/23 Zhanyong Wan (λx.x x) <w...@google.com>:

http://code.google.com/p/googlemock/issues/detail?id=55 tracks this.

>
>> You get a slightly more meaningful error message in this case.
>>
>>>
>>> >
>>> > Thanks,
>>> >
>>> > Alexis
>>> >
>>> >
>>> >
>>>
>>>
>>>
>>> --
>>> Zhanyong
>>
>>
>
>
>
> --
> Zhanyong
>

--
Zhanyong

Vlad Losev

unread,
Jun 23, 2009, 3:56:31 PM6/23/09
to Zhanyong Wan (λx.x x), Alexis, Google C++ Mocking Framework, ade...@imris.com

D'oh! This completely slipped my mind. You are right about the wiki fix.

Reply all
Reply to author
Forward
0 new messages