dimanche 24 mai 2015

Testing for asynchronous results without sleep in Go

I have quite a few components in my code that have persistent go-routines that listen for events to trigger actions. Most of the time, there is no reason (outside of testing) for them to send back a notification when they have completed that action.

However, my unittests are using sleep to wait for these async tasks to complete:

// Send notification event.
mock.devices <- []sparkapi.Device{deviceA, deviceFuncs, deviceRefresh}

// Wait for go-routine to process event.
time.Sleep(time.Microsecond)

// Check that no refresh method was called.
c.Check(mock.actionArgs, check.DeepEquals, mockFunctionCall{})

That seems broken, but I haven't been able to come up with a better solution that doesn't add unreasonable overhead to non-test usage. Is there a reasonable solution that I've missed?

Aucun commentaire:

Enregistrer un commentaire