| index 67a729d21..ea937d816 100644 |
| --- a/internal/termfence/confirm_test.go |
| +++ b/internal/termfence/confirm_test.go |
| @@ -6,9 +6,11 @@ import ( |
| "fmt" |
| "strings" |
| "sync" |
| + "sync/atomic" |
| "testing" |
| "time" |
| |
| + "github.com/aws/aws-sdk-go-v2/aws" |
| "github.com/aws/aws-sdk-go-v2/service/ec2" |
| "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| |
| @@ -211,3 +213,181 @@ func TestConfirmNegativeCachedTenSeconds(t *testing.T) { |
| t.Fatalf("after ttl: got %v, %v, %d describes", ok, err, len(r.f.regions)) |
| } |
| } |
| + |
| +// gatedEC2 answers every describe with out once release is closed, and |
| +// counts the calls. entered gets a value each time a describe starts. |
| +type gatedEC2 struct { |
| + out *ec2.DescribeInstancesOutput |
| + calls atomic.Int32 |
| + entered chan string |
| + release chan struct{} |
| +} |
| + |
| +func (g *gatedEC2) DescribeInstances(_ context.Context, in *ec2.DescribeInstancesInput, _ ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) { |
| + g.calls.Add(1) |
| + g.entered <- in.InstanceIds[0] |
| + <-g.release |
| + out := *g.out |
| + out.Reservations = []types.Reservation{{OwnerId: g.out.Reservations[0].OwnerId, |
| + Instances: []types.Instance{{InstanceId: aws.String(in.InstanceIds[0]), State: g.out.Reservations[0].Instances[0].State}}}} |
| + return &out, nil |
| +} |
| + |
| +func (c *Confirmer) waitersFor(iid string) int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if f, ok := c.inflight[iid]; ok { |
| + return f.waiters |
| + } |
| + return -1 |
| +} |
| + |
| +func TestConfirmConcurrentCallersShareOneDescribe(t *testing.T) { |
| + const n = 8 |
| + g := &gatedEC2{out: describe("111122223333", "i-b", types.InstanceStateNameTerminated), |
| + entered: make(chan string, n), release: make(chan struct{})} |
| + r := newConfirmRig(&fakeEC2{}) |
| + r.c.API = func(context.Context, string) (EC2, error) { return g, nil } |
| + var wg sync.WaitGroup |
| + got := make([]bool, n) |
| + errs := make([]error, n) |
| + for i := 0; i < n; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + got[i], errs[i] = r.c.Terminated(context.Background(), "i-b") |
| + }() |
| + } |
| + <-g.entered // the one describe is in flight; the rest must queue behind it |
| + for r.c.waitersFor("i-b") != n-1 { |
| + time.Sleep(time.Millisecond) |
| + } |
| + close(g.release) |
| + wg.Wait() |
| + for i := range got { |
| + if !got[i] || errs[i] != nil { |
| + t.Fatalf("caller %d: %v, %v", i, got[i], errs[i]) |
| + } |
| + } |
| + if g.calls.Load() != 1 || r.count("termfence: describe i-b") != 1 || r.count("termfence: i-b confirmed") != 1 { |
| + t.Fatalf("describes %d, logs %q", g.calls.Load(), r.logs) |
| + } |
| + // The answer was cached before the waiters left. |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil || g.calls.Load() != 1 { |
| + t.Fatalf("after: %v, %v, %d describes", ok, err, g.calls.Load()) |
| + } |
| +} |
| + |
| +func TestConfirmWaiterLeavesWhenItsContextEnds(t *testing.T) { |
| + g := &gatedEC2{out: describe("111122223333", "i-b", types.InstanceStateNameTerminated), |
| + entered: make(chan string, 2), release: make(chan struct{})} |
| + r := newConfirmRig(&fakeEC2{}) |
| + r.c.API = func(context.Context, string) (EC2, error) { return g, nil } |
| + done := make(chan struct{}) |
| + go func() { |
| + defer close(done) |
| + r.c.Terminated(context.Background(), "i-b") |
| + }() |
| + <-g.entered |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + cancel() |
| + if ok, err := r.c.Terminated(ctx, "i-b"); ok || !errors.Is(err, context.Canceled) { |
| + t.Fatalf("got %v, %v", ok, err) |
| + } |
| + close(g.release) |
| + <-done |
| +} |
| + |
| +func TestConfirmPositiveIsNeverOverwrittenByANegative(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{out: describe("111122223333", "i-b", types.InstanceStateNameTerminated)}) |
| + t0 := r.at |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil { |
| + t.Fatalf("got %v, %v", ok, err) |
| + } |
| + // A later reply that says running, or fails, is not even asked for. |
| + r.f.out = describe("111122223333", "i-b", types.InstanceStateNameRunning) |
| + for _, d := range []time.Duration{11 * time.Second, time.Hour, 23 * time.Hour} { |
| + r.at = t0.Add(d) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil { |
| + t.Fatalf("at %v: %v, %v", d, ok, err) |
| + } |
| + } |
| + if len(r.f.regions) != 1 { |
| + t.Fatalf("describes %d", len(r.f.regions)) |
| + } |
| + // And a negative that finishes late, publishing over an unexpired |
| + // positive, leaves the positive in place and answers with it. |
| + late := &flight{done: make(chan struct{}), res: confirmed{}} |
| + r.c.mu.Lock() |
| + r.c.inflight["i-b"] = late |
| + r.c.mu.Unlock() |
| + r.c.publish("i-b", late) |
| + if !late.res.yes { |
| + t.Fatalf("late negative answered %+v", late.res) |
| + } |
| + r.at = t0.Add(23*time.Hour + 30*time.Minute) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil || len(r.f.regions) != 1 { |
| + t.Fatalf("after late negative: %v, %v, %d describes", ok, err, len(r.f.regions)) |
| + } |
| +} |
| + |
| +func TestConfirmConcurrentFirstCallsBuildOneClient(t *testing.T) { |
| + const n = 6 |
| + f := &fakeEC2{} |
| + r := newConfirmRig(f) |
| + var built atomic.Int32 |
| + r.c.API = func(context.Context, string) (EC2, error) { |
| + built.Add(1) |
| + time.Sleep(20 * time.Millisecond) // long enough for every caller to arrive |
| + return &safeEC2{out: describe("111122223333", "i-x", types.InstanceStateNameRunning)}, nil |
| + } |
| + var wg sync.WaitGroup |
| + for i := 0; i < n; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + if _, err := r.c.Terminated(context.Background(), "i-"+string(rune('b'+i))); err != nil { |
| + t.Error(err) |
| + } |
| + }() |
| + } |
| + wg.Wait() |
| + if built.Load() != 1 { |
| + t.Fatalf("API called %d times", built.Load()) |
| + } |
| +} |
| + |
| +// safeEC2 answers any instance id with out's state, safe for concurrent use. |
| +type safeEC2 struct{ out *ec2.DescribeInstancesOutput } |
| + |
| +func (s *safeEC2) DescribeInstances(_ context.Context, in *ec2.DescribeInstancesInput, _ ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error) { |
| + return describe(aws.ToString(s.out.Reservations[0].OwnerId), in.InstanceIds[0], s.out.Reservations[0].Instances[0].State.Name), nil |
| +} |
| + |
| +func TestConfirmClientConstructionFailureIsRetried(t *testing.T) { |
| + r := newConfirmRig(&fakeEC2{}) |
| + boom := errors.New("no credentials") |
| + calls := 0 |
| + r.c.API = func(context.Context, string) (EC2, error) { |
| + calls++ |
| + if calls == 1 { |
| + return nil, boom |
| + } |
| + return &safeEC2{out: describe("111122223333", "i-x", types.InstanceStateNameTerminated)}, nil |
| + } |
| + t0 := r.at |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); ok || !errors.Is(err, boom) { |
| + t.Fatalf("first: %v, %v", ok, err) |
| + } |
| + // A different instance is not held back by the failure. |
| + if ok, err := r.c.Terminated(context.Background(), "i-c"); !ok || err != nil { |
| + t.Fatalf("i-c: %v, %v", ok, err) |
| + } |
| + r.at = t0.Add(11 * time.Second) |
| + if ok, err := r.c.Terminated(context.Background(), "i-b"); !ok || err != nil { |
| + t.Fatalf("retry: %v, %v", ok, err) |
| + } |
| + if calls != 2 { |
| + t.Fatalf("API called %d times", calls) |
| + } |
| +} |