carlos / platform Public

termfence: test that the confirming caller's cancellation is cached like any error

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 3d1fd79f50a2c03eff9476b4d6a23b7510212c5a parent f60f129
1 file changed, +70 −0
  • internal/termfence/confirm_test.go +70 −0
diff --git a/internal/termfence/confirm_test.go b/internal/termfence/confirm_test.go
index ea937d816..6b9c7e703 100644
--- a/internal/termfence/confirm_test.go
+++ b/internal/termfence/confirm_test.go
@@ -298,6 +298,76 @@ func TestConfirmWaiterLeavesWhenItsContextEnds(t *testing.T) {
<-done
}
+// ctxEC2 blocks its first describe until that describe's own context ends and
+// fails with the context's error; later describes answer out.
+type ctxEC2 struct {
+ out *ec2.DescribeInstancesOutput
+ calls atomic.Int32
+ entered chan struct{}
+}
+
+func (g *ctxEC2) DescribeInstances(ctx context.Context, _ *ec2.DescribeInstancesInput, _ ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) {
+ if g.calls.Add(1) == 1 {
+ g.entered <- struct{}{}
+ <-ctx.Done()
+ return nil, ctx.Err()
+ }
+ return g.out, nil
+}
+
+// The caller performing the describe is the leader. When its context ends,
+// the waiters already joined get the leader's error, the error is cached
+// for the negative TTL like any other, and the next describe is a fresh one
+// made after it expires.
+func TestConfirmLeaderCancellation(t *testing.T) {
+ g := &ctxEC2{out: describe("111122223333", "i-b", types.InstanceStateNameTerminated), entered: make(chan struct{}, 1)}
+ r := newConfirmRig(&fakeEC2{})
+ r.c.API = func(context.Context, string) (EC2, error) { return g, nil }
+ t0 := r.at
+ const waiters = 3
+ type answer struct {
+ yes bool
+ err error
+ }
+ lctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ leader := make(chan answer, 1)
+ go func() {
+ yes, err := r.c.Terminated(lctx, "i-b")
+ leader <- answer{yes, err}
+ }()
+ <-g.entered // the leader's describe is in flight
+ joined := make(chan answer, waiters)
+ for i := 0; i < waiters; i++ {
+ go func() {
+ yes, err := r.c.Terminated(context.Background(), "i-b")
+ joined <- answer{yes, err}
+ }()
+ }
+ for r.c.waitersFor("i-b") != waiters {
+ time.Sleep(time.Millisecond)
+ }
+ cancel()
+ if a := <-leader; a.yes || !errors.Is(a.err, context.Canceled) {
+ t.Fatalf("leader: %+v", a)
+ }
+ for i := 0; i < waiters; i++ {
+ if a := <-joined; a.yes || !errors.Is(a.err, context.Canceled) {
+ t.Fatalf("waiter: %+v", a)
+ }
+ }
+ // Inside the negative TTL: no describe, the cached error.
+ r.at = t0.Add(ConfirmNegativeTTL - time.Second)
+ if yes, err := r.c.Terminated(context.Background(), "i-b"); yes || !errors.Is(err, context.Canceled) || g.calls.Load() != 1 {
+ t.Fatalf("within the TTL: %v, %v, %d describes", yes, err, g.calls.Load())
+ }
+ // After it: a fresh describe, which succeeds.
+ r.at = t0.Add(ConfirmNegativeTTL + time.Second)
+ if yes, err := r.c.Terminated(context.Background(), "i-b"); !yes || err != nil || g.calls.Load() != 2 {
+ t.Fatalf("after the TTL: %v, %v, %d describes", yes, err, g.calls.Load())
+ }
+}
+
func TestConfirmPositiveIsNeverOverwrittenByANegative(t *testing.T) {
r := newConfirmRig(&fakeEC2{out: describe("111122223333", "i-b", types.InstanceStateNameTerminated)})
t0 := r.at