package harness import ( "context" "sync" "testing" "time" ) func TestAsyncRunAppliesAndNotifies(t *testing.T) { var mu sync.Mutex notified := make(chan struct{}, 1) a := NewAsync(&mu, func() { notified <- struct{}{} }) var applied bool a.Run(time.Second, func(ctx context.Context) {}, func() bool { return false }, func() { applied = true }, ) select { case <-notified: case <-time.After(time.Second): t.Fatal("notify never fired") } mu.Lock() defer mu.Unlock() if !applied { t.Fatal("apply did not run") } } func TestAsyncRunStaleSkipsApplyAndNotify(t *testing.T) { var mu sync.Mutex a := NewAsync(&mu, func() { t.Fatal("notify fired on stale result") }) done := make(chan struct{}) a.Run(time.Second, func(ctx context.Context) {}, func() bool { close(done); return true }, func() { t.Fatal("apply ran on stale result") }, ) select { case <-done: case <-time.After(time.Second): t.Fatal("stale guard never evaluated") } time.Sleep(20 * time.Millisecond) // allow a wrongful notify to surface }