|
4 | 4 | "context" |
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
| 7 | + "os" |
7 | 8 | "path/filepath" |
8 | 9 | "strings" |
9 | 10 | "testing" |
@@ -220,6 +221,66 @@ func TestWatchLoop_ReportsFilesystemWatchAddError(t *testing.T) { |
220 | 221 | } |
221 | 222 | } |
222 | 223 |
|
| 224 | +func TestWatchLoop_FallsBackToPollingWhenWatchPathAppearsLate(t *testing.T) { |
| 225 | + t.Parallel() |
| 226 | + |
| 227 | + dir := t.TempDir() |
| 228 | + artifactPath := filepath.Join(dir, "missing", "latest.json") |
| 229 | + watchFS := true |
| 230 | + watchPolling := true |
| 231 | + serveCfg := config.ServeConfig{ |
| 232 | + SchemaVersion: config.ServeSchemaVersion, |
| 233 | + Artifact: config.ArtifactSource{ |
| 234 | + Path: artifactPath, |
| 235 | + Mode: "file", |
| 236 | + }, |
| 237 | + PollInterval: "50ms", |
| 238 | + WatchFS: &watchFS, |
| 239 | + WatchPolling: &watchPolling, |
| 240 | + History: config.HistoryConfig{ |
| 241 | + MaxItems: 1, |
| 242 | + }, |
| 243 | + }.Normalized() |
| 244 | + svc := newWithDeps( |
| 245 | + serveCfg, |
| 246 | + config.Policy{ |
| 247 | + Mode: config.ModeWarn, |
| 248 | + DefaultAction: config.ModeWarn, |
| 249 | + GlobalThreshold: 0.9, |
| 250 | + FailureProbability: 0.1, |
| 251 | + Trials: 100, |
| 252 | + }.ToAnalysisConfig().Normalized(), |
| 253 | + realClock{}, |
| 254 | + newLocator(serveCfg.Artifact), |
| 255 | + nil, |
| 256 | + analyzer.AnalyzeLoaded, |
| 257 | + prometheus.NewRegistry(), |
| 258 | + ) |
| 259 | + |
| 260 | + ctx, cancel := context.WithCancel(context.Background()) |
| 261 | + defer cancel() |
| 262 | + go svc.watchLoop(ctx) |
| 263 | + |
| 264 | + if err := os.MkdirAll(filepath.Dir(artifactPath), 0o755); err != nil { |
| 265 | + t.Fatalf("create artifact dir: %v", err) |
| 266 | + } |
| 267 | + if err := model.WriteToFile(artifactPath, testModel(2, "topology-late")); err != nil { |
| 268 | + t.Fatalf("write late model: %v", err) |
| 269 | + } |
| 270 | + |
| 271 | + deadline := time.Now().Add(3 * time.Second) |
| 272 | + for time.Now().Before(deadline) { |
| 273 | + current := svc.CurrentReport() |
| 274 | + if current != nil && current.InputArtifact != nil && current.InputArtifact.TopologyVersion == "topology-late" { |
| 275 | + return |
| 276 | + } |
| 277 | + time.Sleep(50 * time.Millisecond) |
| 278 | + } |
| 279 | + |
| 280 | + status := svc.CurrentStatus() |
| 281 | + t.Fatalf("expected current report after late artifact appears, status=%+v", status) |
| 282 | +} |
| 283 | + |
223 | 284 | func TestHTTPServerAppliesTimeouts(t *testing.T) { |
224 | 285 | t.Parallel() |
225 | 286 |
|
|
0 commit comments