-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
167 lines (139 loc) · 3.97 KB
/
consumer_test.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package saramajetstream_test
import (
"reflect"
"testing"
"time"
sjs "github.com/S7evinK/saramajetstream"
"github.com/Shopify/sarama"
)
func TestJetStreamConsumer_Topics(t *testing.T) {
s := startServer(t)
nc, js := connectServer(t, s.ClientURL())
t.Cleanup(cleanup(t, nc, s))
consumer := sjs.NewJetStreamConsumer(nc, js, "")
if consumer == nil {
t.Fatalf("expected consumer to be not nil")
}
topics, err := consumer.Topics()
if err != nil {
t.Fatalf("expected no error")
}
if len(topics) == 0 {
t.Fatalf("expected some topics, but got 0")
}
t.Logf("Topics: %+v", topics)
if err := consumer.Close(); err != nil {
t.Fatalf("failed to close consumer: %+v", err)
}
}
func TestJetStreamConsumer_Partitions(t *testing.T) {
s := startServer(t)
nc, js := connectServer(t, s.ClientURL())
t.Cleanup(cleanup(t, nc, s))
consumer := sjs.NewJetStreamConsumer(nc, js, "")
if consumer == nil {
t.Fatalf("expected consumer to be not nil")
}
partitions, err := consumer.Partitions("test")
if err != nil {
t.Fatalf("expected no error")
}
if len(partitions) == 0 {
t.Fatalf("expected some partitions, but got 0")
}
t.Logf("Partitions: %+v", partitions)
if err := consumer.Close(); err != nil {
t.Fatalf("failed to close consumer: %+v", err)
}
}
func TestJetStreamConsumer_HighWaterMarks(t *testing.T) {
s := startServer(t)
nc, js := connectServer(t, s.ClientURL())
t.Cleanup(cleanup(t, nc, s))
consumer := sjs.NewJetStreamConsumer(nc, js, "")
if consumer == nil {
t.Fatalf("expected consumer to be not nil")
}
info, err := js.StreamInfo("test")
if err != nil {
t.Fatalf("unable to get stream info: %+v", err)
}
want := map[string]map[int32]int64{
"test": {0: int64(info.State.LastSeq + 1)},
}
got := consumer.HighWaterMarks()
if len(got) == 0 {
t.Fatalf("expected some watermarks, but got 0")
}
t.Logf("HighWaterMarks: %+v", got)
if err := consumer.Close(); err != nil {
t.Fatalf("failed to close consumer: %+v", err)
}
if !reflect.DeepEqual(want, got) {
t.Fatalf(`expected "%+v", but got "%+v"`, want, got)
}
}
func TestJetStreamConsumer_PartitionConsumer(t *testing.T) {
s := startServer(t)
nc, js := connectServer(t, s.ClientURL())
t.Cleanup(cleanup(t, nc, s))
consumer := sjs.NewJetStreamConsumer(nc, js, "")
if consumer == nil {
t.Fatalf("expected consumer to be not nil")
}
producer := sjs.NewJetStreamProducer(nc, js, "")
hwm := consumer.HighWaterMarks()
pc, err := consumer.ConsumePartition("test", 0, hwm["test"][0])
if err != nil {
t.Fatalf("unable to consume partition: %+v", err)
}
go func() {
if pcErr := <-pc.Errors(); pcErr != nil {
t.Errorf("unexpected error: %+v", pcErr)
return
}
}()
t.Cleanup(func() {
if errs := pc.Close(); errs != nil {
t.Fatalf("errors after closing: %+v", errs)
}
})
pcHwm := pc.HighWaterMarkOffset()
if pcHwm != hwm["test"][0] {
t.Fatalf("expected PartitionConsumer HighWaterMarkOffset (%d) to equal HighWatermarks (%d)", pcHwm, hwm["test"][0])
}
_, offset, err := producer.SendMessage(&sarama.ProducerMessage{
Topic: "test",
Timestamp: time.Now(),
Value: sarama.StringEncoder("hello world"),
})
if err != nil {
t.Fatalf("unable to send message: %+v", err)
}
msg := <-pc.Messages()
want := "hello world"
if string(msg.Value) != want {
t.Fatalf(`expected message to be "%s" but got "%s"`, want, msg.Value)
}
if msg.Offset != offset || msg.Offset != hwm["test"][0] {
t.Fatalf("unexpected offset, got %d, want %d", msg.Offset, offset)
}
}
func TestJetStreamConsumer_Close(t *testing.T) {
s := startServer(t)
nc, js := connectServer(t, s.ClientURL())
t.Cleanup(cleanup(t, nc, s))
consumer := sjs.NewJetStreamConsumer(nc, js, "")
if consumer == nil {
t.Fatalf("expected consumer to be not nil")
}
_, err := consumer.ConsumePartition("test", 0, sarama.OffsetNewest)
if err != nil {
t.Fatalf("unable to consume partition: %+v", err)
}
t.Cleanup(func() {
if err := consumer.Close(); err != nil {
t.Fatalf("unable to close consumer: %+v", err)
}
})
}