Gmock action request: PostOnceCallback/PostCallback & PostOnceClosure/PostClosure

33 views
Skip to first unread message

Daniel Murphy

unread,
May 20, 2022, 2:10:16 PM5/20/22
to Chromium-dev
Hello!

I filed this bug but IDK if there is a category for it.

I LOVE the gmock_callback_support functionality. One thing that I often need is this same ability, but where the callback is posted on a task runner. I tried to hack this up but was quickly over my head.

Any chance this is something someone else wanted too? Is there a place to put this feature request? Happy to collaborate on creating this as well, any gmock action experts?

Thanks,
Dan

K. Moon

unread,
May 20, 2022, 2:25:51 PM5/20/22
to dmu...@chromium.org, Chromium-dev
The bug is probably the best place to discuss this, but ignoring the implementation for a moment, can you give an example or snippet of what your ideal usage would look like?

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CA%2B4qT31TjR_RYQEM%2B-TJpjj77Goetck2saOj%2BnNzS6EF34M9UA%40mail.gmail.com.

Daniel Murphy

unread,
May 20, 2022, 4:44:38 PM5/20/22
to K. Moon, Chromium-dev
One option is to look exactly like the existing support in https://source.chromium.org/chromium/chromium/src/+/main:base/test/gmock_callback_support.h
* Instead of calling the callback right away they grab a base::SequencedTaskRunnerHandle::Get() on action creation, and post the resulting callback to that when the action is executed.
* Maybe have a PostOnceCallbackWithTaskRunner version that takes a task runner as a first argument, then the callback arguments after it.

Loko Kung

unread,
May 20, 2022, 5:38:50 PM5/20/22
to dmu...@chromium.org, Chromium-dev
Hey! Actually I am writing tests for something very similar (see ReturnAndRunOnceCallbackAfter/ReturnAndRunOnceCallbackAfterDelay and the respective *Impls in gpu/command_buffer/service/dawn_caching_interface_unittest.cc in https://chromium-review.googlesource.com/c/chromium/src/+/3617222). I assume that is essentially what you are looking for? I did not put it into anything common atm, since it was just for our testing, but it could be a starting point for what you are asking for.

--

Kyle Charbonneau

unread,
May 25, 2022, 10:05:26 AM5/25/22
to Chromium-dev, Daniel Murphy, Chromium-dev, K. Moon
I think base::BindTaskRunner() + RunClosure() does what you want. It would look something like .WillOnce(test::Run[Once]Closure(base::BindPostTask(base::SequencedTaskRunnerHandle::Get(), callback))). The gmock action will post a task to run callback on the task runner.

Kyle
On Friday, May 20, 2022 at 4:44:38 PM UTC-4 Daniel Murphy wrote:
One option is to look exactly like the existing support in https://source.chromium.org/chromium/chromium/src/+/main:base/test/gmock_callback_support.h
* Instead of calling the callback right away they grab a base::SequencedTaskRunnerHandle::Get() on action creation, and post the resulting callback to that when the action is executed.
* Maybe have a PostOnceCallbackWithTaskRunner version that takes a task runner as a first argument, then the callback arguments after it.

On Fri, May 20, 2022 at 11:24 AM K. Moon <km...@chromium.org> wrote:
The bug is probably the best place to discuss this, but ignoring the implementation for a moment, can you give an example or snippet of what your ideal usage would look like?

On Fri, May 20, 2022 at 11:09 AM Daniel Murphy <dmu...@chromium.org> wrote:
Hello!

I filed this bug but IDK if there is a category for it.

I LOVE the gmock_callback_support functionality. One thing that I often need is this same ability, but where the callback is posted on a task runner. I tried to hack this up but was quickly over my head.

Any chance this is something someone else wanted too? Is there a place to put this feature request? Happy to collaborate on creating this as well, any gmock action experts?

Thanks,
Dan

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev+unsubscribe@chromium.org.

Daniel Murphy

unread,
May 26, 2022, 9:35:34 PM5/26/22
to Kyle Charbonneau, Chromium-dev, K. Moon
The key part here is that the callback is not something the test has, it's part of the argument to the mocked action.


So I would imagine that it would look like:

class Foo {
  void FunctionCall(base::OnceCallback<void(bool result, int data)> on_complete);
}
// assume mock version
EXPECT_THAT(object, FunctionCall(base::test::IsNotNullCallback())
    .WillOnce(base::test::PostOnceCallback<0>(/*result=*/true,
                                              /*data=*/kTestData));

and then when this happens somewhere inside of the tested code:

object.FunctionCall(std::move(callback));

That callback would be called with (true, kTestData) but not synchronously (as the existing support does), but posted on a task runner.


To unsubscribe from this group and stop receiving emails from it, send an email to chromium-dev...@chromium.org.
Reply all
Reply to author
Forward
0 new messages