-
-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathsubscriber_test.go
More file actions
98 lines (72 loc) · 3.14 KB
/
subscriber_test.go
File metadata and controls
98 lines (72 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mercure
import (
"bytes"
"log/slog"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDispatch(t *testing.T) {
t.Parallel()
ctx := t.Context()
s := NewLocalSubscriber("1", slog.Default(), &TopicSelectorStore{})
s.SubscribedTopics = []string{"https://example.com"}
s.SubscribedTopics = []string{"https://example.com"}
defer s.Disconnect()
// Dispatch must be non-blocking
// Messages coming from the history can be sent after live messages, but must be received first
s.Dispatch(ctx, &Update{Topics: s.SubscribedTopics, Event: Event{ID: "3"}}, false)
s.Dispatch(ctx, &Update{Topics: s.SubscribedTopics, Event: Event{ID: "1"}}, true)
s.Dispatch(ctx, &Update{Topics: s.SubscribedTopics, Event: Event{ID: "4"}}, false)
s.Dispatch(ctx, &Update{Topics: s.SubscribedTopics, Event: Event{ID: "2"}}, true)
s.HistoryDispatched("")
s.Ready(ctx)
for i := 1; i <= 4; i++ {
if u, ok := <-s.Receive(); ok && u != nil {
assert.Equal(t, strconv.Itoa(i), u.ID)
}
}
}
func TestDisconnect(t *testing.T) {
t.Parallel()
s := NewLocalSubscriber("", slog.Default(), &TopicSelectorStore{})
s.Disconnect()
// can be called two times without crashing
s.Disconnect()
assert.False(t, s.Dispatch(t.Context(), &Update{}, false))
}
func TestLogSubscriber(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
logger := slog.New(slog.NewJSONHandler(&buf, nil))
s := NewLocalSubscriber("123", logger, &TopicSelectorStore{})
s.SetTopics([]string{"https://example.com/bar"}, []string{"https://example.com/foo"})
logger.Info("test", slog.Any("subscriber", s))
log := buf.String()
assert.Contains(t, log, `"last_event_id":"123"`)
assert.Contains(t, log, `"topic_selectors":["https://example.com/foo"]`)
assert.Contains(t, log, `"topics":["https://example.com/bar"]`)
}
func TestMatchTopic(t *testing.T) {
t.Parallel()
s := NewLocalSubscriber("", slog.Default(), &TopicSelectorStore{})
s.SetTopics([]string{"https://example.com/no-match", "https://example.com/books/{id}"}, []string{"https://example.com/users/foo/{?topic}"})
assert.False(t, s.Match(&Update{Topics: []string{"https://example.com/not-subscribed"}}))
assert.False(t, s.Match(&Update{Topics: []string{"https://example.com/not-subscribed"}, Private: true}))
assert.False(t, s.Match(&Update{Topics: []string{"https://example.com/no-match"}, Private: true}))
assert.False(t, s.Match(&Update{Topics: []string{"https://example.com/books/1"}, Private: true}))
assert.False(t, s.Match(&Update{Topics: []string{"https://example.com/books/1", "https://example.com/users/bar/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1"}, Private: true}))
assert.True(t, s.Match(&Update{Topics: []string{"https://example.com/books/1"}}))
assert.True(t, s.Match(&Update{Topics: []string{"https://example.com/books/1", "https://example.com/users/foo/?topic=https%3A%2F%2Fexample.com%2Fbooks%2F1"}, Private: true}))
}
func TestSubscriberDoesNotBlockWhenChanIsFull(t *testing.T) {
t.Parallel()
ctx := t.Context()
s := NewLocalSubscriber("", slog.Default(), &TopicSelectorStore{})
s.Ready(ctx)
for i := 0; i <= outBufferLength; i++ {
s.Dispatch(ctx, &Update{}, false)
}
for range s.Receive() { //nolint:revive
}
}