uber-go / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
1.98k stars 107 forks source link

Adding method to Controller allowing to check whether all expected calls have been made #10

Closed FlorianLoch closed 1 year ago

FlorianLoch commented 1 year ago

I originally created this issue in February 2022 in the golang/mock repository. But as this project has been abandoned in favour of this fork I will suggest the feature over here too.

I want to be able to check whether all expected calls have been made before Controller#Finish() gets invoked as the code being tested is running asynchronously and I can only approximate when all calls will have been made.

This addition should allow it to check whether all expected calls towards the mock(s) have been made in an idempotent way. This is useful when testing asynchronous/non-deterministic things that aren't fully under the developers control (e.g., in end-2-end tests).

If necessary I can elaborate on my specific testing scenario.

Having such a method allows the user to write something like

require.Eventually(t, mockCtrl.AllExpectedCallsSatisfied, 60time.Second, 100time.Millisecond) instead of having to wait for an approximated amount of time (which slows down the tests unnecessarily) in order to make sure all expected calls have been satisfied before Controller#Finish() gets invoked.

Why is this needed? Because right now AFAIU I have to add a sleep for an amount of time I approximate to be enough for my system to be done with its work. This is not nice for several reasons:

It's dangerous (and a bad idea in general) because my estimation could be too little and calls have not been made at that point It's slow because I basically have to approximate very conservatively due to the problem described above.

I already created an MR in golang/mock (golang/mock#627), which I could easily recreate over here.

sywhang commented 1 year ago

Hey @FlorianLoch, thanks for submitting this issue! I think this is a fine addition to add to Controller.

Feel free to submit a PR and we'll review it soon!

FlorianLoch commented 1 year ago

Great, just did so!

sywhang commented 1 year ago

Closing as #21 is merged