|
1
|
package topology |
|
2
|
|
|
3
|
import ( |
|
4
|
"io" |
|
5
|
"log/slog" |
|
6
|
"testing" |
|
7
|
"time" |
|
8
|
|
|
9
|
"github.com/conflicthq/scuttlebot/internal/config" |
|
10
|
) |
|
11
|
|
|
12
|
// reapDry runs the reaper's expiry check without calling ChanServ. |
|
13
|
// It returns the names of channels that would be reaped. |
|
14
|
func reapDry(m *Manager, now time.Time) []string { |
|
15
|
if m.policy == nil { |
|
16
|
return nil |
|
17
|
} |
|
18
|
m.mu.Lock() |
|
19
|
defer m.mu.Unlock() |
|
20
|
var out []string |
|
21
|
for _, rec := range m.channels { |
|
22
|
ttl := m.policy.TTLFor(rec.name) |
|
23
|
if ttl > 0 && m.policy.IsEphemeral(rec.name) && now.Sub(rec.provisionedAt) > ttl { |
|
24
|
out = append(out, rec.name) |
|
25
|
} |
|
26
|
} |
|
27
|
return out |
|
28
|
} |
|
29
|
|
|
30
|
func TestReaperExpiry(t *testing.T) { |
|
31
|
pol := NewPolicy(config.TopologyConfig{ |
|
32
|
Types: []config.ChannelTypeConfig{ |
|
33
|
{ |
|
34
|
Name: "task", |
|
35
|
Prefix: "task.", |
|
36
|
Ephemeral: true, |
|
37
|
TTL: config.Duration{Duration: 72 * time.Hour}, |
|
38
|
}, |
|
39
|
{ |
|
40
|
Name: "sprint", |
|
41
|
Prefix: "sprint.", |
|
42
|
}, |
|
43
|
}, |
|
44
|
}) |
|
45
|
log := slog.New(slog.NewTextHandler(io.Discard, nil)) |
|
46
|
m := NewManager("localhost:6667", "topology", "pass", "", pol, log) |
|
47
|
|
|
48
|
// Simulate that channels were provisioned at different times. |
|
49
|
m.mu.Lock() |
|
50
|
m.channels["#task.old"] = channelRecord{name: "#task.old", provisionedAt: time.Now().Add(-80 * time.Hour)} |
|
51
|
m.channels["#task.fresh"] = channelRecord{name: "#task.fresh", provisionedAt: time.Now().Add(-10 * time.Hour)} |
|
52
|
m.channels["#sprint.2026-q2"] = channelRecord{name: "#sprint.2026-q2", provisionedAt: time.Now().Add(-200 * time.Hour)} |
|
53
|
m.mu.Unlock() |
|
54
|
|
|
55
|
expired := reapDry(m, time.Now()) |
|
56
|
if len(expired) != 1 || expired[0] != "#task.old" { |
|
57
|
t.Errorf("expected [#task.old] to be expired, got %v", expired) |
|
58
|
} |
|
59
|
} |
|
60
|
|