(t *testing.T)
| 1318 | } |
| 1319 | |
| 1320 | func TestBackgroundCleanupLifecycle(t *testing.T) { |
| 1321 | t.Run("Background cleanup starts on first CreateConnection", func(t *testing.T) { |
| 1322 | writer := &Writer{ |
| 1323 | mu: sync.RWMutex{}, |
| 1324 | openConnections: make(map[string]Connection), |
| 1325 | closedConnections: make(map[string]FailedConnection), |
| 1326 | cleanupDone: make(chan struct{}), |
| 1327 | cleanupOnce: sync.Once{}, |
| 1328 | cleanupStopped: false, |
| 1329 | closeAndRemoveFilesWithRetry: func(_ Connection) error { |
| 1330 | return nil |
| 1331 | }, |
| 1332 | } |
| 1333 | |
| 1334 | ctx := context.Background() |
| 1335 | tmpDir := t.TempDir() |
| 1336 | config := map[string]interface{}{ |
| 1337 | "path": tmpDir, |
| 1338 | "maxAuditResults": 5.0, |
| 1339 | } |
| 1340 | |
| 1341 | err := writer.CreateConnection(ctx, "test-conn", config) |
| 1342 | if err != nil { |
| 1343 | t.Fatalf("CreateConnection failed: %v", err) |
| 1344 | } |
| 1345 | |
| 1346 | time.Sleep(100 * time.Millisecond) |
| 1347 | |
| 1348 | writer.mu.RLock() |
| 1349 | _, exists := writer.openConnections["test-conn"] |
| 1350 | cleanupStopped := writer.cleanupStopped |
| 1351 | writer.mu.RUnlock() |
| 1352 | |
| 1353 | if !exists { |
| 1354 | t.Error("Connection was not created") |
| 1355 | } |
| 1356 | |
| 1357 | if cleanupStopped { |
| 1358 | t.Error("Expected cleanup to be running, but it's stopped") |
| 1359 | } |
| 1360 | |
| 1361 | writer.mu.Lock() |
| 1362 | if !writer.cleanupStopped { |
| 1363 | writer.cleanupStopped = true |
| 1364 | close(writer.cleanupDone) |
| 1365 | } |
| 1366 | writer.mu.Unlock() |
| 1367 | }) |
| 1368 | |
| 1369 | t.Run("Background cleanup stops when all connections are closed", func(t *testing.T) { |
| 1370 | writer := &Writer{ |
| 1371 | mu: sync.RWMutex{}, |
| 1372 | openConnections: make(map[string]Connection), |
| 1373 | closedConnections: make(map[string]FailedConnection), |
| 1374 | cleanupDone: make(chan struct{}), |
| 1375 | cleanupOnce: sync.Once{}, |
| 1376 | cleanupStopped: false, |
| 1377 | closeAndRemoveFilesWithRetry: func(_ Connection) error { |
nothing calls this directly
no test coverage detected
searching dependent graphs…