❯ go-clikit
v0.10.0 pkg.go.dev ↗

packages › progress & output › fanout/fanouttest

fanouttest

import "github.com/GSI-HPC/go-clikit/fanout/fanouttest"

Count the calls that a fake is in the middle of, to prove that a fan-out keeps its limit.

since v0.6.0 imports: no package of the kit decisions 6
The place of fanout/fanouttest in go-clikit: the packages it imports, and the packages that import it.termtextprogressdisplaycliprogressprogresstestmcpprogresseventlogfanoutfanouttestclioutdoctorfsutilcobratreecobratreetestlocalwebsessionsselocalwebtestlaunchassetspagehtmx

A fake that answers at once rarely has two calls under way together, so a count alone proves little. fanouttest.InFlight holds each call until enough others have joined it. A test of a limit holds calls until one more than the limit are under way. A fan-out that keeps its limit never allows that, so each round waits the full time with exactly the limit in flight. A fan-out that starts one call too many is caught.

Wrap your fake in a few lines:

func (f *fake) Run(ctx context.Context, req Request) (Result, error) {
	defer f.calls.Enter()()
	return f.answer(ctx, req)
}
fake := &uptime{}
fake.calls.Hold = 3
fake.calls.Patience = time.Minute

fanout.Each(context.Background(), 3, 3, func(i int) {
	fake.Ask(fmt.Sprintf("exe%02d", i+1))
})
fmt.Println(fake.calls.Peak(), fake.calls.Started())
3 3
ExampleInFlight in fanout/fanouttest/example_test.go. CI runs it and checks its output.
fake := &uptime{}
fake.calls.Hold = 2
fake.calls.Patience = time.Millisecond

fanout.Each(context.Background(), 3, 1, func(i int) {
	fake.Ask(fmt.Sprintf("exe%02d", i+1))
})
fmt.Println(fake.calls.Peak(), fake.calls.Started())
1 3
ExampleInFlight_patience in fanout/fanouttest/example_test.go. CI runs it and checks its output.