-
Notifications
You must be signed in to change notification settings - Fork 11
/
metrics_test.go
116 lines (93 loc) · 2.43 KB
/
metrics_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
package courier
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockEmitter struct {
mock.Mock
}
func newMockEmitter(t *testing.T) *mockEmitter {
m := &mockEmitter{}
m.Test(t)
return m
}
func (m *mockEmitter) Emit(ctx context.Context, meta ClientMeta) { m.Called(ctx, meta) }
func TestClient_ClientInfoEmitter(t *testing.T) {
tests := []struct {
name string
mock func(*sync.WaitGroup, *mock.Mock)
opts func(*testing.T) []ClientOption
}{
{
name: "single connection mode",
opts: func(t *testing.T) []ClientOption { return nil },
mock: func(wg *sync.WaitGroup, m *mock.Mock) {
wg.Add(1)
m.On("Emit", mock.Anything, mock.Anything).Return().Run(func(args mock.Arguments) {
wg.Done()
}).Once()
},
},
{
name: "multi connection mode",
opts: func(t *testing.T) []ClientOption {
ch := make(chan []TCPAddress, 1)
dCh := make(chan struct{})
mr := newMockResolver(t)
mr.On("UpdateChan").Return(ch)
mr.On("Done").Return(dCh)
go func() {
ch <- []TCPAddress{testBrokerAddress, testBrokerAddress}
<-time.After(time.Second + 500*time.Millisecond)
close(ch)
dCh <- struct{}{}
}()
return []ClientOption{
WithResolver(mr),
UseMultiConnectionMode,
}
},
mock: func(wg *sync.WaitGroup, m *mock.Mock) {
wg.Add(1)
m.On("Emit", mock.Anything, mock.Anything).Return().Run(func(args mock.Arguments) {
wg.Done()
}).Once()
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
eOpts := tt.opts(t)
mc := newMockEmitter(t)
eOpts = append(eOpts, &ClientInfoEmitterConfig{
Interval: time.Second,
Emitter: mc,
})
wg := &sync.WaitGroup{}
if tt.mock != nil {
tt.mock(wg, &mc.Mock)
}
c, err := NewClient(append(defOpts, eOpts...)...)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
go func() {
_ = c.Run(ctx)
}()
assert.True(t, WaitForConnection(c, 2*time.Second, 100*time.Millisecond))
wg.Wait()
cancel()
mc.AssertExpectations(t)
})
}
t.Run("NewClientWithLessThanOneSecondEmitterIntervalError", func(t *testing.T) {
_, err := NewClient(append(defOpts, &ClientInfoEmitterConfig{
Interval: 100 * time.Millisecond,
Emitter: newMockEmitter(t),
})...)
assert.EqualError(t, err, "client info emitter interval must be greater than or equal to 1s")
})
}