|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "sync" |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/shellcell/snailmail/internal/domain" |
| 12 | +) |
| 13 | + |
| 14 | +func cases(count int) []domain.VerificationCase { |
| 15 | + made := make([]domain.VerificationCase, 0, count) |
| 16 | + for index := range count { |
| 17 | + made = append(made, domain.VerificationCase{Package: fmt.Sprintf("p%d", index), Version: "1"}) |
| 18 | + } |
| 19 | + return made |
| 20 | +} |
| 21 | + |
| 22 | +func TestVerifyCasesRunsEveryCase(t *testing.T) { |
| 23 | + var mutex sync.Mutex |
| 24 | + seen := map[string]bool{} |
| 25 | + if err := verifyCases(context.Background(), cases(9), func(_ context.Context, verification domain.VerificationCase) error { |
| 26 | + mutex.Lock() |
| 27 | + defer mutex.Unlock() |
| 28 | + if seen[verification.Package] { |
| 29 | + t.Errorf("case %s was verified twice", verification.Package) |
| 30 | + } |
| 31 | + seen[verification.Package] = true |
| 32 | + return nil |
| 33 | + }); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + if len(seen) != 9 { |
| 37 | + t.Fatalf("verified %d cases, want 9", len(seen)) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// A failed publication must name the same case every time, or the same defect |
| 42 | +// reads as a different problem on each run. |
| 43 | +func TestVerifyCasesReportsTheFirstFailureInOrder(t *testing.T) { |
| 44 | + for range 20 { |
| 45 | + err := verifyCases(context.Background(), cases(8), func(_ context.Context, verification domain.VerificationCase) error { |
| 46 | + if verification.Package == "p3" || verification.Package == "p6" { |
| 47 | + return errors.New(verification.Package + " failed") |
| 48 | + } |
| 49 | + return nil |
| 50 | + }) |
| 51 | + if err == nil || err.Error() != "p3 failed" { |
| 52 | + t.Fatalf("got %v, want the earliest failing case p3", err) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Every case runs even once one has failed. That is what makes the reported |
| 58 | +// case deterministic: a case skipped because a sibling failed first cannot be |
| 59 | +// compared against the ones that did run, and the earliest failure would depend |
| 60 | +// on which container won the race for a slot. |
| 61 | +func TestVerifyCasesRunsEveryCaseEvenAfterAFailure(t *testing.T) { |
| 62 | + var started atomic.Int64 |
| 63 | + err := verifyCases(context.Background(), cases(32), func(_ context.Context, verification domain.VerificationCase) error { |
| 64 | + started.Add(1) |
| 65 | + if verification.Package == "p0" { |
| 66 | + return errors.New("refused") |
| 67 | + } |
| 68 | + return nil |
| 69 | + }) |
| 70 | + if err == nil || err.Error() != "refused" { |
| 71 | + t.Fatalf("got %v, want the failing case reported", err) |
| 72 | + } |
| 73 | + if count := started.Load(); count != 32 { |
| 74 | + t.Fatalf("ran %d of 32 cases; skipping any makes the reported failure depend on scheduling", count) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Cancelling the parent must not read as every case having passed. |
| 79 | +func TestVerifyCasesHonoursACancelledContext(t *testing.T) { |
| 80 | + ctx, cancel := context.WithCancel(context.Background()) |
| 81 | + cancel() |
| 82 | + if err := verifyCases(ctx, cases(4), func(context.Context, domain.VerificationCase) error { |
| 83 | + return nil |
| 84 | + }); !errors.Is(err, context.Canceled) { |
| 85 | + t.Fatalf("got %v, want context.Canceled", err) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// The bound is what keeps an emulated architecture from oversubscribing a small |
| 90 | +// runner, so it has to actually hold. |
| 91 | +func TestVerifyCasesRespectsItsConcurrencyBound(t *testing.T) { |
| 92 | + var running, peak atomic.Int64 |
| 93 | + if err := verifyCases(context.Background(), cases(32), func(context.Context, domain.VerificationCase) error { |
| 94 | + current := running.Add(1) |
| 95 | + defer running.Add(-1) |
| 96 | + for { |
| 97 | + was := peak.Load() |
| 98 | + if current <= was || peak.CompareAndSwap(was, current) { |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + return nil |
| 103 | + }); err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + if peak.Load() > maxConcurrentVerifications { |
| 107 | + t.Fatalf("%d cases ran at once, above the bound of %d", peak.Load(), maxConcurrentVerifications) |
| 108 | + } |
| 109 | +} |
0 commit comments