|
1
|
package herald_test |
|
2
|
|
|
3
|
import ( |
|
4
|
"testing" |
|
5
|
"time" |
|
6
|
|
|
7
|
"github.com/conflicthq/scuttlebot/internal/bots/herald" |
|
8
|
) |
|
9
|
|
|
10
|
func newBot(routes herald.RouteConfig) *herald.Bot { |
|
11
|
return herald.New("localhost:6667", "pass", nil, routes, 100, 100, nil) |
|
12
|
} |
|
13
|
|
|
14
|
func TestBotName(t *testing.T) { |
|
15
|
b := newBot(herald.RouteConfig{}) |
|
16
|
if b.Name() != "herald" { |
|
17
|
t.Errorf("Name(): got %q", b.Name()) |
|
18
|
} |
|
19
|
} |
|
20
|
|
|
21
|
func TestEmitNonBlocking(t *testing.T) { |
|
22
|
b := newBot(herald.RouteConfig{DefaultChannel: "#fleet"}) |
|
23
|
// Fill queue past capacity — should not block. |
|
24
|
for i := 0; i < 300; i++ { |
|
25
|
b.Emit(herald.Event{Type: "ci.build", Message: "build done"}) |
|
26
|
} |
|
27
|
} |
|
28
|
|
|
29
|
func TestRateLimiterAllows(t *testing.T) { |
|
30
|
// High rate + high burst: all should be allowed immediately. |
|
31
|
b := newBot(herald.RouteConfig{DefaultChannel: "#fleet"}) |
|
32
|
// Emit() just queues; actual rate limiting happens in deliver(). |
|
33
|
// We test that Emit is non-blocking and the bot is constructible. |
|
34
|
b.Emit(herald.Event{Type: "ci.build", Message: "ok"}) |
|
35
|
} |
|
36
|
|
|
37
|
func TestRouteConfig(t *testing.T) { |
|
38
|
// Verify routing logic by checking bot construction accepts route maps. |
|
39
|
routes := herald.RouteConfig{ |
|
40
|
Routes: map[string]string{ |
|
41
|
"ci.": "#builds", |
|
42
|
"ci.build.": "#builds", |
|
43
|
"deploy.": "#deploys", |
|
44
|
}, |
|
45
|
DefaultChannel: "#alerts", |
|
46
|
} |
|
47
|
b := herald.New("localhost:6667", "pass", nil, routes, 5, 20, nil) |
|
48
|
if b == nil { |
|
49
|
t.Fatal("expected non-nil bot") |
|
50
|
} |
|
51
|
} |
|
52
|
|
|
53
|
func TestEmitDropsWhenQueueFull(t *testing.T) { |
|
54
|
b := newBot(herald.RouteConfig{}) |
|
55
|
// Emit 1000 events — excess should be dropped without panic or block. |
|
56
|
done := make(chan struct{}) |
|
57
|
go func() { |
|
58
|
for i := 0; i < 1000; i++ { |
|
59
|
b.Emit(herald.Event{Type: "x", Message: "y"}) |
|
60
|
} |
|
61
|
close(done) |
|
62
|
}() |
|
63
|
select { |
|
64
|
case <-done: |
|
65
|
case <-time.After(2 * time.Second): |
|
66
|
t.Error("Emit blocked or deadlocked") |
|
67
|
} |
|
68
|
} |
|
69
|
|