|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + main "semyi" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCleanupWorker(t *testing.T) { |
| 12 | + // Insert test data |
| 13 | + now := time.Now() |
| 14 | + oldDate := now.AddDate(0, 0, -5) // 5 days old |
| 15 | + recentDate := now.AddDate(0, 0, -1) // 1 day old |
| 16 | + |
| 17 | + // Use unique monitor IDs for the test |
| 18 | + monitorID1 := "cleanup_test1" |
| 19 | + monitorID2 := "cleanup_test2" |
| 20 | + |
| 21 | + // Insert data into raw historical table |
| 22 | + _, err := database.Exec(` |
| 23 | + INSERT INTO monitor_historical (timestamp, monitor_id, status, latency) VALUES |
| 24 | + (?, ?, 1, 100), |
| 25 | + (?, ?, 1, 100), |
| 26 | + (?, ?, 1, 200), |
| 27 | + (?, ?, 1, 200) |
| 28 | + `, oldDate, monitorID1, recentDate, monitorID1, oldDate, monitorID2, recentDate, monitorID2) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to insert test data into monitor_historical: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + // Insert data into hourly aggregate table |
| 34 | + _, err = database.Exec(` |
| 35 | + INSERT INTO monitor_historical_hourly_aggregate (timestamp, monitor_id, status, latency) VALUES |
| 36 | + (?, ?, 1, 100), |
| 37 | + (?, ?, 1, 100), |
| 38 | + (?, ?, 1, 200), |
| 39 | + (?, ?, 1, 200) |
| 40 | + `, oldDate, monitorID1, recentDate, monitorID1, oldDate, monitorID2, recentDate, monitorID2) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("Failed to insert test data into monitor_historical_hourly_aggregate: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + // Insert data into daily aggregate table |
| 46 | + _, err = database.Exec(` |
| 47 | + INSERT INTO monitor_historical_daily_aggregate (timestamp, monitor_id, status, latency) VALUES |
| 48 | + (?, ?, 1, 100), |
| 49 | + (?, ?, 1, 100), |
| 50 | + (?, ?, 1, 200), |
| 51 | + (?, ?, 1, 200) |
| 52 | + `, oldDate, monitorID1, recentDate, monitorID1, oldDate, monitorID2, recentDate, monitorID2) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Failed to insert test data into monitor_historical_daily_aggregate: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + // Register cleanup function to remove test data |
| 58 | + t.Cleanup(func() { |
| 59 | + // Clean up monitor_historical |
| 60 | + _, err := database.Exec("DELETE FROM monitor_historical WHERE monitor_id IN (?, ?)", monitorID1, monitorID2) |
| 61 | + if err != nil { |
| 62 | + t.Logf("Warning: failed to clean up monitor_historical: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Clean up monitor_historical_hourly_aggregate |
| 66 | + _, err = database.Exec("DELETE FROM monitor_historical_hourly_aggregate WHERE monitor_id IN (?, ?)", monitorID1, monitorID2) |
| 67 | + if err != nil { |
| 68 | + t.Logf("Warning: failed to clean up monitor_historical_hourly_aggregate: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + // Clean up monitor_historical_daily_aggregate |
| 72 | + _, err = database.Exec("DELETE FROM monitor_historical_daily_aggregate WHERE monitor_id IN (?, ?)", monitorID1, monitorID2) |
| 73 | + if err != nil { |
| 74 | + t.Logf("Warning: failed to clean up monitor_historical_daily_aggregate: %v", err) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + // Create cleanup worker with 3 days retention period |
| 79 | + worker := main.NewCleanupWorker(database, 3) |
| 80 | + |
| 81 | + // Run cleanup |
| 82 | + err = worker.Cleanup(context.Background()) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Cleanup failed: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Verify that old data is deleted |
| 88 | + var count int |
| 89 | + err = database.QueryRow("SELECT COUNT(*) FROM monitor_historical WHERE timestamp < ? AND monitor_id IN (?, ?)", now.AddDate(0, 0, -3), monitorID1, monitorID2).Scan(&count) |
| 90 | + if err != nil { |
| 91 | + t.Fatalf("Failed to query monitor_historical: %v", err) |
| 92 | + } |
| 93 | + if count != 0 { |
| 94 | + t.Errorf("Expected 0 old records in monitor_historical, got %d", count) |
| 95 | + } |
| 96 | + |
| 97 | + err = database.QueryRow("SELECT COUNT(*) FROM monitor_historical_hourly_aggregate WHERE timestamp < ? AND monitor_id IN (?, ?)", now.AddDate(0, 0, -3), monitorID1, monitorID2).Scan(&count) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Failed to query monitor_historical_hourly_aggregate: %v", err) |
| 100 | + } |
| 101 | + if count != 0 { |
| 102 | + t.Errorf("Expected 0 old records in monitor_historical_hourly_aggregate, got %d", count) |
| 103 | + } |
| 104 | + |
| 105 | + err = database.QueryRow("SELECT COUNT(*) FROM monitor_historical_daily_aggregate WHERE timestamp < ? AND monitor_id IN (?, ?)", now.AddDate(0, 0, -3), monitorID1, monitorID2).Scan(&count) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("Failed to query monitor_historical_daily_aggregate: %v", err) |
| 108 | + } |
| 109 | + if count != 0 { |
| 110 | + t.Errorf("Expected 0 old records in monitor_historical_daily_aggregate, got %d", count) |
| 111 | + } |
| 112 | + |
| 113 | + // Verify that recent data is preserved |
| 114 | + err = database.QueryRow("SELECT COUNT(*) FROM monitor_historical WHERE timestamp >= ? AND monitor_id IN (?, ?)", now.AddDate(0, 0, -3), monitorID1, monitorID2).Scan(&count) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("Failed to query monitor_historical: %v", err) |
| 117 | + } |
| 118 | + if count != 2 { |
| 119 | + t.Errorf("Expected 2 recent records in monitor_historical, got %d", count) |
| 120 | + } |
| 121 | + |
| 122 | + err = database.QueryRow("SELECT COUNT(*) FROM monitor_historical_hourly_aggregate WHERE timestamp >= ? AND monitor_id IN (?, ?)", now.AddDate(0, 0, -3), monitorID1, monitorID2).Scan(&count) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("Failed to query monitor_historical_hourly_aggregate: %v", err) |
| 125 | + } |
| 126 | + if count != 2 { |
| 127 | + t.Errorf("Expected 2 recent records in monitor_historical_hourly_aggregate, got %d", count) |
| 128 | + } |
| 129 | + |
| 130 | + err = database.QueryRow("SELECT COUNT(*) FROM monitor_historical_daily_aggregate WHERE timestamp >= ? AND monitor_id IN (?, ?)", now.AddDate(0, 0, -3), monitorID1, monitorID2).Scan(&count) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("Failed to query monitor_historical_daily_aggregate: %v", err) |
| 133 | + } |
| 134 | + if count != 2 { |
| 135 | + t.Errorf("Expected 2 recent records in monitor_historical_daily_aggregate, got %d", count) |
| 136 | + } |
| 137 | +} |
0 commit comments