package zitcli
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestSharedRunsOnceForConcurrentCallers(t *testing.T) {
var g flightGroup
var runs atomic.Int64
release := make(chan struct{})
run := func(context.Context) (int, error) {
runs.Add(1)
<-release
return 7, nil
}
const n = 8
got := make([]int, n)
errs := make([]error, n)
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
got[i], errs[i] = shared(&g, context.Background(), "k", func() (int, bool) { return 0, false }, func(int) {}, run)
}(i)
}
time.Sleep(50 * time.Millisecond)
close(release)
wg.Wait()
if r := runs.Load(); r != 1 {
t.Errorf("FAIL: %d runs for one key, want 1", r)
}
for i := range got {
if errs[i] != nil || got[i] != 7 {
t.Errorf("FAIL: caller %d got %d, %v", i, got[i], errs[i])
}
}
if !t.Failed() {
t.Log("ok")
}
}
func TestSharedCancelledWaiterLeavesRunAlone(t *testing.T) {
var g flightGroup
release := make(chan struct{})
started := make(chan struct{})
var runs atomic.Int64
run := func(ctx context.Context) (int, error) {
runs.Add(1)
close(started)
select {
case <-release:
return 9, nil
case <-ctx.Done():
return 0, ctx.Err()
}
}
none := func() (int, bool) { return 0, false }
// The owner is the caller that starts the run, then gives up.
ownerCtx, cancelOwner := context.WithCancel(context.Background())
ownerDone := make(chan error, 1)
go func() {
_, err := shared(&g, ownerCtx, "k", none, func(int) {}, run)
ownerDone <- err
}()
<-started
otherDone := make(chan int, 1)
go func() {
v, _ := shared(&g, context.Background(), "k", none, func(int) {}, run)
otherDone <- v
}()
time.Sleep(30 * time.Millisecond)
cancelOwner()
select {
case err := <-ownerDone:
if !errors.Is(err, context.Canceled) {
t.Errorf("FAIL: cancelled caller got %v", err)
}
case <-time.After(2 * time.Second):
t.Fatal("FAIL: cancelled caller did not return early")
}
close(release)
select {
case v := <-otherDone:
if v != 9 {
t.Errorf("FAIL: the other caller got %d, want 9", v)
}
case <-time.After(2 * time.Second):
t.Fatal("FAIL: the other caller never got the shared result")
}
if r := runs.Load(); r != 1 {
t.Errorf("FAIL: %d runs, want 1", r)
}
if !t.Failed() {
t.Log("ok")
}
}
func TestSharedDoesNotCacheAFailure(t *testing.T) {
var g flightGroup
var stored atomic.Int64
boom := errors.New("boom")
calls := 0
run := func(context.Context) (int, error) {
calls++
if calls == 1 {
return 0, boom
}
return 5, nil
}
none := func() (int, bool) { return 0, false }
store := func(int) { stored.Add(1) }
if _, err := shared(&g, context.Background(), "k", none, store, run); !errors.Is(err, boom) {
t.Errorf("FAIL: first call: %v", err)
}
if stored.Load() != 0 {
t.Errorf("FAIL: a failed run was stored")
}
v, err := shared(&g, context.Background(), "k", none, store, run)
if err != nil || v != 5 || calls != 2 {
t.Errorf("FAIL: retry got %d, %v after %d runs", v, err, calls)
}
if stored.Load() != 1 {
t.Errorf("FAIL: success stored %d times", stored.Load())
}
if !t.Failed() {
t.Log("ok")
}
}