package host import ( "context" "errors" "testing" "github.com/carlosframework/platform/internal/hostwrite" ) type assemblyTestWork struct { Before func() Wait bool } func (w assemblyTestWork) With(ctx context.Context, _ hostwrite.Identity, fn func() error) error { if w.Before != nil { w.Before() } if w.Wait { <-ctx.Done() return ctx.Err() } return fn() } func TestClaimLeaseWorkRechecksAfterGuardWait(t *testing.T) { lost := false ran := false denial := errors.New("lease lost") w := claimLeaseWork{Work: assemblyTestWork{Before: func() { lost = true }}, Alive: context.Background(), Check: func() error { if lost { return denial } return nil }} if err := w.With(context.Background(), hostwrite.Identity{}, func() error { ran = true; return nil }); !errors.Is(err, denial) || ran { t.Fatalf("effect=%v err=%v", ran, err) } } func TestClaimLeaseWorkCancellationInterruptsGuardWait(t *testing.T) { live, cancel := context.WithCancel(context.Background()) w := claimLeaseWork{Work: assemblyTestWork{Before: cancel, Wait: true}, Alive: live, Check: func() error { return nil }} if err := w.With(context.Background(), hostwrite.Identity{}, func() error { t.Fatal("effect after cancellation"); return nil }); !errors.Is(err, context.Canceled) { t.Fatal(err) } }