Documentation
¶
Overview ¶
Package async provides a test‑aware sync.WaitGroup.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
func Run[T CommonT](t T, name string, f func(t T), options ...testoplugin.Option)
Run calls testo.Run inside independent goroutine and returns immediately.
Inside Run, testo.T.Context is cancelled as soon as at least one neighbor test inside Run calls to testo.T.FailNow.
async.Run(t, "foo", func(t T) { t.FailNow() })
async.Run(t, "bar", func(t T) { t.Log(<-t.Context.Done()) }) // cancelled
All tasks are awaited before test completion with sync.WaitGroup.Wait. Use PluginAsync.Wait to manually await all running goroutines and propagate calls to testo.T.FailNow from goroutines (if any).
Difference from parallel tests ¶
When you call testo.T.Parallel it pauses current test until all other synchronous tests are completed. Sometimes it might be a problem.
For example, when testing a concurrent component where you need to run several operations at the same time, then check its state inside the same test function:
type T struct{
*testo.T
*async.PluginAsync
}
func Test(t *testing.T) {
testo.RunTest(t, func(t T) {
const workers = 10
const incs = 100
var counter Counter
for i := range workers {
async.Run(t, fmt.Sprintf("worker %d", i), func(t T) {
for range incs {
counter.Inc()
}
})
}
t.Wait()
want := workers * incs
got := counter.Value()
if want != got {
t.Fatalf("counter = %d, want %d", got, want)
}
})
}
func WithLimit ¶
func WithLimit(n int) testoplugin.Option
WithLimit limits the number of active goroutines in this group to at most n. A negative value indicates no limit. A limit of zero will prevent any new goroutines from being added.
Any subsequent call to the Run function will block until it can add an active goroutine without exceeding the configured limit.
Types ¶
type CommonT ¶
CommonT is an interface common for all Ts with PluginAsync installed.
type PluginAsync ¶
PluginAsync simplifies goroutine spawning in tests.
func (*PluginAsync) Plugin ¶
func (pa *PluginAsync) Plugin( _ testoplugin.Plugin, options ...testoplugin.Option, ) testoplugin.Spec
Plugin implements testoplugin.Plugin.
func (*PluginAsync) Wait ¶
func (pa *PluginAsync) Wait()
Wait blocks until all tests started from Run are finished. If at least one test called testo.T.FailNow inside Run, Wait will propagate it.
Note, that calling this function is optional, as it will be called by the plugin after the current test or sub-test ends.