This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer_test.go
176 lines (155 loc) · 3.87 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
168
169
170
171
172
173
174
175
176
package quark
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type stubHandler struct{}
func (h stubHandler) ServeEvent(EventWriter, *Event) bool {
return true
}
var consumerAddTopicsTestingSuite = []struct {
n []string // input
expected int // expected result (length)
}{
{[]string{"chat.0"}, 1},
{[]string{"chat.0", "chat.1"}, 2},
{[]string{}, 0},
{[]string{"chat.0", "chat.1", "chat.2"}, 3},
}
var consumerAddTopicTestingSuite = []struct {
t string // input
expected int // expected result (length)
}{
{"chat.0", 1},
{"", 0},
}
func TestConsumer(t *testing.T) {
t.Run("Consumer add topics", func(t *testing.T) {
for _, tt := range consumerAddTopicsTestingSuite {
c := Consumer{}
c.Topics(tt.n...)
assert.Equal(t, tt.expected, len(c.topics))
}
})
t.Run("Consumer add topic", func(t *testing.T) {
for _, tt := range consumerAddTopicTestingSuite {
c := Consumer{}
c.Topic(tt.t)
assert.Equal(t, tt.expected, len(c.topics))
}
})
t.Run("Consumer public operations", func(t *testing.T) {
c := Consumer{}
c.Topic("chat.1")
assert.Contains(t, c.topics, "chat.1")
c.PoolSize(10)
assert.Equal(t, 10, c.poolSize)
c.Address("localhost")
assert.Contains(t, c.cluster, "localhost")
})
}
func TestConsumer_Group(t *testing.T) {
t.Run("Consumer group mutation", func(t *testing.T) {
c := Consumer{}
c.Group("foo-group")
assert.Equal(t, "foo-group", c.group)
})
}
func TestConsumer_MaxRetries(t *testing.T) {
t.Run("Consumer max retries mutation", func(t *testing.T) {
c := Consumer{}
c.MaxRetries(5)
assert.Equal(t, 5, c.maxRetries)
})
}
func TestConsumer_RetryBackoff(t *testing.T) {
t.Run("Consumer retry backoff mutation", func(t *testing.T) {
c := Consumer{}
c.RetryBackoff(time.Second * 5)
assert.Equal(t, time.Second*5, c.retryBackoff)
})
}
type stubProviderConfig struct {
Foo string
}
func TestConsumer_GetProviderConfig(t *testing.T) {
t.Run("Consumer provider config mutation", func(t *testing.T) {
c := Consumer{}
cfg := &stubProviderConfig{}
c.ProviderConfig(cfg)
assert.Same(t, cfg, c.GetProviderConfig())
})
}
func TestConsumer_Handle(t *testing.T) {
t.Run("Consumer handle mutation", func(t *testing.T) {
c := Consumer{}
h := &stubHandler{}
c.Handle(h)
assert.Same(t, h, c.handler)
})
}
func TestConsumer_HandleFunc(t *testing.T) {
t.Run("Consumer handle function mutation", func(t *testing.T) {
c := Consumer{}
h := func(EventWriter, *Event) bool { return true }
c.HandleFunc(h)
assert.NotEmpty(t, c.handlerFunc)
})
}
var consumerTopicStringTestingSuite = []struct {
topics []string
expected string
}{
{[]string{"chat.0", "chat.1", "chat.2"}, "chat.0,chat.1,chat.2"},
{[]string{"chat.0"}, "chat.0"},
{[]string{}, ""},
}
func TestConsumer_TopicString(t *testing.T) {
for _, tt := range consumerTopicStringTestingSuite {
t.Run("Consumer topics string", func(t *testing.T) {
c := Consumer{}
c.Topics(tt.topics...)
assert.Equal(t, tt.expected, c.TopicString())
})
}
}
type stubPublisher struct {
fail bool
}
var errStubPublisher = errors.New("generic stub publisher error")
func (p stubPublisher) Publish(context.Context, ...*Message) error {
if p.fail {
return errStubPublisher
}
return nil
}
func TestConsumer_Publisher(t *testing.T) {
t.Run("Consumer add publisher", func(t *testing.T) {
c := Consumer{}
p := new(stubPublisher)
c.Publisher(p)
assert.Equal(t, p, c.publisher)
})
}
func BenchmarkConsumer(b *testing.B) {
topics := []string{"chat.0", "chat.1", "chat.2", "chat.3"}
b.Run("Consumer add topics", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c := Consumer{}
c.Topics(topics...)
}
})
b.Run("Consumer public operations", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c := Consumer{}
c.Topic("chat.1")
c.PoolSize(10)
c.Address("localhost")
}
})
}