|
| 1 | +package concurrent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/surge-downloader/surge/internal/engine/types" |
| 9 | +) |
| 10 | + |
| 11 | +func TestHealthCheck_StallDetection(t *testing.T) { |
| 12 | + // Setup |
| 13 | + runtime := &types.RuntimeConfig{ |
| 14 | + StallTimeout: 100 * time.Millisecond, |
| 15 | + SlowWorkerGracePeriod: 0, |
| 16 | + } |
| 17 | + d := &ConcurrentDownloader{ |
| 18 | + Runtime: runtime, |
| 19 | + activeTasks: make(map[int]*ActiveTask), |
| 20 | + } |
| 21 | + |
| 22 | + // Create a "stalled" task |
| 23 | + ctx, cancel := context.WithCancel(context.Background()) |
| 24 | + now := time.Now() |
| 25 | + stalledTask := &ActiveTask{ |
| 26 | + StartTime: now.Add(-1 * time.Second), |
| 27 | + LastActivity: now.Add(-500 * time.Millisecond).UnixNano(), // Past timeout (100ms) |
| 28 | + Cancel: cancel, |
| 29 | + } |
| 30 | + // Speed is 0, but check 2 doesn't matter for this test |
| 31 | + stalledTask.Speed = 0 |
| 32 | + |
| 33 | + d.activeMu.Lock() |
| 34 | + d.activeTasks[1] = stalledTask |
| 35 | + d.activeMu.Unlock() |
| 36 | + |
| 37 | + // Run Health Check |
| 38 | + d.checkWorkerHealth() |
| 39 | + |
| 40 | + // Verify cancellation |
| 41 | + select { |
| 42 | + case <-ctx.Done(): |
| 43 | + // Success: context was cancelled |
| 44 | + default: |
| 45 | + t.Fatal("Stalled worker was not cancelled") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestHealthCheck_StragglerDetection(t *testing.T) { |
| 50 | + // Setup |
| 51 | + runtime := &types.RuntimeConfig{ |
| 52 | + StallTimeout: 10 * time.Second, // Long timeout so stall check passes |
| 53 | + SlowWorkerGracePeriod: 0, |
| 54 | + SlowWorkerThreshold: 0.5, |
| 55 | + } |
| 56 | + d := &ConcurrentDownloader{ |
| 57 | + Runtime: runtime, |
| 58 | + activeTasks: make(map[int]*ActiveTask), |
| 59 | + } |
| 60 | + |
| 61 | + ctx1, cancel1 := context.WithCancel(context.Background()) |
| 62 | + ctx2, cancel2 := context.WithCancel(context.Background()) |
| 63 | + now := time.Now() |
| 64 | + |
| 65 | + // Task 1: Fast (Baseline) |
| 66 | + fastTask := &ActiveTask{ |
| 67 | + StartTime: now.Add(-5 * time.Second), |
| 68 | + LastActivity: now.UnixNano(), |
| 69 | + Cancel: cancel1, |
| 70 | + Speed: 1000, |
| 71 | + } |
| 72 | + |
| 73 | + // Task 2: Slow (Straggler) |
| 74 | + // Speed 100 is < 50% of mean ((1000+100)/2 = 550) |
| 75 | + slowTask := &ActiveTask{ |
| 76 | + StartTime: now.Add(-5 * time.Second), |
| 77 | + LastActivity: now.UnixNano(), |
| 78 | + Cancel: cancel2, |
| 79 | + Speed: 100, |
| 80 | + } |
| 81 | + |
| 82 | + d.activeMu.Lock() |
| 83 | + d.activeTasks[1] = fastTask |
| 84 | + d.activeTasks[2] = slowTask |
| 85 | + d.activeMu.Unlock() |
| 86 | + |
| 87 | + // Run Health Check |
| 88 | + d.checkWorkerHealth() |
| 89 | + |
| 90 | + // Verify cancellation |
| 91 | + select { |
| 92 | + case <-ctx1.Done(): |
| 93 | + t.Fatal("Fast worker was incorrectly cancelled") |
| 94 | + default: |
| 95 | + // OK |
| 96 | + } |
| 97 | + |
| 98 | + select { |
| 99 | + case <-ctx2.Done(): |
| 100 | + // OK |
| 101 | + default: |
| 102 | + t.Fatal("Slow worker was NOT cancelled") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestHealthCheck_ZeroSpeedDetection(t *testing.T) { |
| 107 | + // Setup |
| 108 | + runtime := &types.RuntimeConfig{ |
| 109 | + StallTimeout: 10 * time.Second, |
| 110 | + SlowWorkerGracePeriod: 0, |
| 111 | + SlowWorkerThreshold: 0.5, |
| 112 | + } |
| 113 | + d := &ConcurrentDownloader{ |
| 114 | + Runtime: runtime, |
| 115 | + activeTasks: make(map[int]*ActiveTask), |
| 116 | + } |
| 117 | + |
| 118 | + _, cancel1 := context.WithCancel(context.Background()) |
| 119 | + ctx2, cancel2 := context.WithCancel(context.Background()) |
| 120 | + now := time.Now() |
| 121 | + |
| 122 | + // Task 1: Fast |
| 123 | + fastTask := &ActiveTask{ |
| 124 | + StartTime: now.Add(-5 * time.Second), |
| 125 | + LastActivity: now.UnixNano(), |
| 126 | + Cancel: cancel1, |
| 127 | + Speed: 1000, |
| 128 | + } |
| 129 | + |
| 130 | + // Task 2: Zero Speed (but not stalled by time yet) |
| 131 | + // Should be caught by Check 2 because 0 < threshold * mean |
| 132 | + zeroTask := &ActiveTask{ |
| 133 | + StartTime: now.Add(-5 * time.Second), |
| 134 | + LastActivity: now.UnixNano(), |
| 135 | + Cancel: cancel2, |
| 136 | + Speed: 0, |
| 137 | + } |
| 138 | + |
| 139 | + d.activeMu.Lock() |
| 140 | + d.activeTasks[1] = fastTask |
| 141 | + d.activeTasks[2] = zeroTask |
| 142 | + d.activeMu.Unlock() |
| 143 | + |
| 144 | + // Run Health Check |
| 145 | + d.checkWorkerHealth() |
| 146 | + |
| 147 | + // Verify cancellation |
| 148 | + select { |
| 149 | + case <-ctx2.Done(): |
| 150 | + // Success |
| 151 | + default: |
| 152 | + t.Fatal("Zero speed worker was NOT cancelled by relative check") |
| 153 | + } |
| 154 | +} |
0 commit comments