|
| 1 | +package drivers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "slices" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/srcfl/ftw/go/internal/config" |
| 11 | + "github.com/srcfl/ftw/go/internal/telemetry" |
| 12 | +) |
| 13 | + |
| 14 | +// blockingSerialRegistry fails the first open, then blocks every later open |
| 15 | +// until the test sends a result. |
| 16 | +func blockingSerialRegistry(t *testing.T) (*Registry, chan struct{}, chan error) { |
| 17 | + t.Helper() |
| 18 | + entered := make(chan struct{}, 4) |
| 19 | + result := make(chan error) |
| 20 | + first := true |
| 21 | + r := NewRegistry(telemetry.NewStore()) |
| 22 | + r.SerialFactory = func(name string, c *config.SerialConfig) (SerialCap, error) { |
| 23 | + if first { |
| 24 | + first = false |
| 25 | + return nil, errors.New("no such device") |
| 26 | + } |
| 27 | + entered <- struct{}{} |
| 28 | + if err := <-result; err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + return fakeSerial{}, nil |
| 32 | + } |
| 33 | + t.Cleanup(r.ShutdownAll) |
| 34 | + return r, entered, result |
| 35 | +} |
| 36 | + |
| 37 | +// startInflightRetry fails a boot Add, then runs its retry until it blocks |
| 38 | +// in the serial open. The returned channel closes when the retry returns. |
| 39 | +func startInflightRetry(t *testing.T, r *Registry, cfg config.Driver, entered chan struct{}) chan struct{} { |
| 40 | + t.Helper() |
| 41 | + if err := r.Add(context.Background(), cfg); err == nil { |
| 42 | + t.Fatal("expected first Add to fail") |
| 43 | + } |
| 44 | + e := pendingSpawnRetry(r, cfg.Name) |
| 45 | + if e == nil { |
| 46 | + t.Fatal("no retry scheduled") |
| 47 | + } |
| 48 | + e.timer.Stop() |
| 49 | + done := make(chan struct{}) |
| 50 | + go func() { r.retrySpawn(cfg.Name, e); close(done) }() |
| 51 | + select { |
| 52 | + case <-entered: |
| 53 | + case <-time.After(5 * time.Second): |
| 54 | + t.Fatal("retry did not reach the serial open") |
| 55 | + } |
| 56 | + return done |
| 57 | +} |
| 58 | + |
| 59 | +// returnsWithin reports whether done closes within d. |
| 60 | +func returnsWithin(done chan struct{}, d time.Duration) bool { |
| 61 | + select { |
| 62 | + case <-done: |
| 63 | + return true |
| 64 | + case <-time.After(d): |
| 65 | + return false |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// The user disables a failing driver while a retry runs, and the retry then |
| 70 | +// succeeds. The disabled driver must not be left running. |
| 71 | +func TestReloadDuringInflightRetrySuccessStopsDriver(t *testing.T) { |
| 72 | + r, entered, result := blockingSerialRegistry(t) |
| 73 | + cfg := serialRetryDriver(t) |
| 74 | + done := startInflightRetry(t, r, cfg, entered) |
| 75 | + |
| 76 | + reloadDone := make(chan struct{}) |
| 77 | + go func() { |
| 78 | + disabled := cfg |
| 79 | + disabled.Disabled = true |
| 80 | + r.Reload(context.Background(), []config.Driver{disabled}, false) |
| 81 | + close(reloadDone) |
| 82 | + }() |
| 83 | + if returnsWithin(reloadDone, 300*time.Millisecond) { |
| 84 | + t.Error("Reload returned while the retry attempt was still in flight") |
| 85 | + } |
| 86 | + result <- nil |
| 87 | + <-done |
| 88 | + <-reloadDone |
| 89 | + if slices.Contains(r.Names(), "p1") { |
| 90 | + t.Fatalf("driver disabled in config is running after an in-flight retry: %v", r.Names()) |
| 91 | + } |
| 92 | + if e := pendingSpawnRetry(r, "p1"); e != nil { |
| 93 | + t.Fatalf("retry scheduled for a disabled driver: %+v", e) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Same, but the retry fails. No further retry may be scheduled. |
| 98 | +func TestReloadDuringInflightRetryFailureStopsRetrying(t *testing.T) { |
| 99 | + r, entered, result := blockingSerialRegistry(t) |
| 100 | + cfg := serialRetryDriver(t) |
| 101 | + done := startInflightRetry(t, r, cfg, entered) |
| 102 | + |
| 103 | + reloadDone := make(chan struct{}) |
| 104 | + go func() { |
| 105 | + r.Reload(context.Background(), nil, false) |
| 106 | + close(reloadDone) |
| 107 | + }() |
| 108 | + returnsWithin(reloadDone, 300*time.Millisecond) |
| 109 | + result <- errors.New("still missing") |
| 110 | + <-done |
| 111 | + <-reloadDone |
| 112 | + if e := pendingSpawnRetry(r, "p1"); e != nil { |
| 113 | + t.Fatalf("retry still scheduled (in %v) for a driver removed from config", e.delay) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// ShutdownAll blocks until all poll loops exit, so it must wait for a retry |
| 118 | +// that may still start a driver. |
| 119 | +func TestShutdownAllWaitsForInflightRetry(t *testing.T) { |
| 120 | + r, entered, result := blockingSerialRegistry(t) |
| 121 | + cfg := serialRetryDriver(t) |
| 122 | + done := startInflightRetry(t, r, cfg, entered) |
| 123 | + |
| 124 | + shutDone := make(chan struct{}) |
| 125 | + go func() { r.ShutdownAll(); close(shutDone) }() |
| 126 | + if returnsWithin(shutDone, 300*time.Millisecond) { |
| 127 | + t.Error("ShutdownAll returned while a retry attempt was still starting the driver") |
| 128 | + } |
| 129 | + result <- nil |
| 130 | + <-done |
| 131 | + <-shutDone |
| 132 | + if slices.Contains(r.Names(), "p1") { |
| 133 | + t.Fatalf("driver running after shutdown: %v", r.Names()) |
| 134 | + } |
| 135 | +} |
0 commit comments