|
| 1 | +package poll |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// TestCalculateInterval verifies the exponential backoff algorithm produces reasonable intervals. |
| 9 | +func TestCalculateInterval(t *testing.T) { |
| 10 | + now := time.Now() |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + lastPostTime time.Time |
| 14 | + wantMin time.Duration |
| 15 | + wantMax time.Duration |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "very recent post (5 minutes ago)", |
| 19 | + lastPostTime: now.Add(-5 * time.Minute), |
| 20 | + wantMin: 5 * time.Minute, |
| 21 | + wantMax: 6 * time.Minute, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "recent post (1 hour ago)", |
| 25 | + lastPostTime: now.Add(-1 * time.Hour), |
| 26 | + wantMin: 6 * time.Minute, |
| 27 | + wantMax: 8 * time.Minute, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "active thread (3 hours ago)", |
| 31 | + lastPostTime: now.Add(-3 * time.Hour), |
| 32 | + wantMin: 9 * time.Minute, |
| 33 | + wantMax: 11 * time.Minute, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "moderately active (6 hours ago)", |
| 37 | + lastPostTime: now.Add(-6 * time.Hour), |
| 38 | + wantMin: 18 * time.Minute, |
| 39 | + wantMax: 22 * time.Minute, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "daily active (12 hours ago)", |
| 43 | + lastPostTime: now.Add(-12 * time.Hour), |
| 44 | + wantMin: 75 * time.Minute, |
| 45 | + wantMax: 85 * time.Minute, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "daily thread (24 hours ago)", |
| 49 | + lastPostTime: now.Add(-24 * time.Hour), |
| 50 | + wantMin: 3*time.Hour + 50*time.Minute, |
| 51 | + wantMax: 4 * time.Hour, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "inactive thread (7 days ago)", |
| 55 | + lastPostTime: now.Add(-7 * 24 * time.Hour), |
| 56 | + wantMin: 4 * time.Hour, |
| 57 | + wantMax: 4 * time.Hour, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "zero last polled (error case)", |
| 61 | + lastPostTime: now.Add(-1 * time.Hour), |
| 62 | + wantMin: 4 * time.Hour, |
| 63 | + wantMax: 4 * time.Hour, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + lastPolledAt := now |
| 70 | + if tt.name == "zero last polled (error case)" { |
| 71 | + lastPolledAt = time.Time{} |
| 72 | + } |
| 73 | + |
| 74 | + interval, reason := CalculateInterval(tt.lastPostTime, lastPolledAt) |
| 75 | + |
| 76 | + if interval < tt.wantMin || interval > tt.wantMax { |
| 77 | + t.Errorf("CalculateInterval() interval = %v, want between %v and %v", interval, tt.wantMin, tt.wantMax) |
| 78 | + } |
| 79 | + |
| 80 | + if reason == "" { |
| 81 | + t.Error("CalculateInterval() reason should not be empty") |
| 82 | + } |
| 83 | + |
| 84 | + t.Logf("Post age: %v → Interval: %v (reason: %s)", time.Since(tt.lastPostTime).Round(time.Minute), interval.Round(time.Minute), reason) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestCalculateIntervalNeverReturnsZero ensures we never return a zero interval. |
| 90 | +func TestCalculateIntervalNeverReturnsZero(t *testing.T) { |
| 91 | + now := time.Now() |
| 92 | + |
| 93 | + // Test various edge cases |
| 94 | + testCases := []struct { |
| 95 | + name string |
| 96 | + lastPostTime time.Time |
| 97 | + lastPolledAt time.Time |
| 98 | + }{ |
| 99 | + {"current time", now, now}, |
| 100 | + {"1 second ago", now.Add(-1 * time.Second), now}, |
| 101 | + {"zero post time", time.Time{}, now}, |
| 102 | + {"zero polled time", now, time.Time{}}, |
| 103 | + {"both zero", time.Time{}, time.Time{}}, |
| 104 | + {"far future (should never happen)", now.Add(24 * time.Hour), now}, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tc := range testCases { |
| 108 | + t.Run(tc.name, func(t *testing.T) { |
| 109 | + interval, _ := CalculateInterval(tc.lastPostTime, tc.lastPolledAt) |
| 110 | + if interval == 0 { |
| 111 | + t.Errorf("CalculateInterval() returned 0 for %s", tc.name) |
| 112 | + } |
| 113 | + if interval < 5*time.Minute { |
| 114 | + t.Errorf("CalculateInterval() returned %v, which is less than minimum (5min) for %s", interval, tc.name) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// TestCalculateIntervalExponentialBehavior verifies the interval grows exponentially. |
| 121 | +func TestCalculateIntervalExponentialBehavior(t *testing.T) { |
| 122 | + now := time.Now() |
| 123 | + |
| 124 | + // Test that interval approximately doubles every 3 hours |
| 125 | + interval3h, _ := CalculateInterval(now.Add(-3*time.Hour), now) |
| 126 | + interval6h, _ := CalculateInterval(now.Add(-6*time.Hour), now) |
| 127 | + |
| 128 | + ratio := float64(interval6h) / float64(interval3h) |
| 129 | + |
| 130 | + // Should be approximately 2x (allow 10% tolerance for rounding) |
| 131 | + if ratio < 1.8 || ratio > 2.2 { |
| 132 | + t.Errorf("Expected interval to double every 3 hours, got ratio %.2f (3h=%v, 6h=%v)", ratio, interval3h, interval6h) |
| 133 | + } |
| 134 | + |
| 135 | + t.Logf("3h interval: %v, 6h interval: %v, ratio: %.2fx", interval3h, interval6h, ratio) |
| 136 | +} |
0 commit comments