-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
options_test.go
170 lines (151 loc) · 4.3 KB
/
options_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
package centrifuge
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestWithHistory(t *testing.T) {
opt := WithHistory(10, time.Second)
opts := &PublishOptions{}
opt(opts)
require.Equal(t, 10, opts.HistorySize)
require.Equal(t, time.Second, opts.HistoryTTL)
}
func TestWithIdempotencyKey(t *testing.T) {
opt := WithIdempotencyKey("ik")
opts := &PublishOptions{}
opt(opts)
require.Equal(t, "ik", opts.IdempotencyKey)
}
func TestWithDelta(t *testing.T) {
opt := WithDelta(true)
opts := &PublishOptions{}
opt(opts)
require.True(t, opts.UseDelta)
}
func TestWithIdempotentResultTTL(t *testing.T) {
opt := WithIdempotentResultTTL(time.Minute)
opts := &PublishOptions{}
opt(opts)
require.Equal(t, time.Minute, opts.IdempotentResultTTL)
}
func TestWithMeta(t *testing.T) {
opt := WithTags(map[string]string{"test": "value"})
opts := &PublishOptions{}
opt(opts)
require.Equal(t, "value", opts.Tags["test"])
}
func TestSubscribeOptions(t *testing.T) {
subscribeOpts := []SubscribeOption{
WithExpireAt(1),
WithEmitPresence(true),
WithEmitJoinLeave(true),
WithPushJoinLeave(true),
WithPositioning(true),
WithRecovery(true),
WithChannelInfo([]byte(`test`)),
WithSubscribeSession("session"),
WithSubscribeClient("test"),
WithSubscribeSource(4),
WithRecoveryMode(RecoveryModeCache),
WithSubscribeHistoryMetaTTL(24 * time.Hour),
}
opts := &SubscribeOptions{}
for _, opt := range subscribeOpts {
opt(opts)
}
require.Equal(t, int64(1), opts.ExpireAt)
require.True(t, opts.EmitPresence)
require.True(t, opts.EmitJoinLeave)
require.True(t, opts.PushJoinLeave)
require.True(t, opts.EnablePositioning)
require.True(t, opts.EnableRecovery)
require.Equal(t, []byte(`test`), opts.ChannelInfo)
require.Equal(t, "test", opts.clientID)
require.Equal(t, "session", opts.sessionID)
require.Equal(t, uint8(4), opts.Source)
require.Equal(t, RecoveryModeCache, opts.RecoveryMode)
require.Equal(t, 24*time.Hour, opts.HistoryMetaTTL)
}
func TestWithDisconnect(t *testing.T) {
opt := WithCustomDisconnect(DisconnectConnectionLimit)
opts := &DisconnectOptions{}
opt(opts)
require.Equal(t, DisconnectConnectionLimit.Code, opts.Disconnect.Code)
}
func TestWithClientWhitelist(t *testing.T) {
opt := WithDisconnectClientWhitelist([]string{"client"})
opts := &DisconnectOptions{}
opt(opts)
require.Equal(t, []string{"client"}, opts.ClientWhitelist)
}
func TestWithLimit(t *testing.T) {
opt := WithLimit(NoLimit)
opts := &HistoryOptions{}
opt(opts)
require.Equal(t, NoLimit, opts.Filter.Limit)
}
func TestWithSubscribeClient(t *testing.T) {
opt := WithSubscribeClient("client")
opts := &SubscribeOptions{}
opt(opts)
require.Equal(t, "client", opts.clientID)
}
func TestWithSubscribeData(t *testing.T) {
opt := WithSubscribeData([]byte("test"))
opts := &SubscribeOptions{}
opt(opts)
require.Equal(t, []byte("test"), opts.Data)
}
func TestWithUnsubscribeClient(t *testing.T) {
opt := WithUnsubscribeClient("client")
opts := &UnsubscribeOptions{}
opt(opts)
require.Equal(t, "client", opts.clientID)
}
func TestWithUnsubscribeSession(t *testing.T) {
opt := WithUnsubscribeSession("session")
opts := &UnsubscribeOptions{}
opt(opts)
require.Equal(t, "session", opts.sessionID)
}
func TestWithCustomUnsubscribe(t *testing.T) {
opt := WithCustomUnsubscribe(Unsubscribe{
Code: 2200,
Reason: "x",
})
opts := &UnsubscribeOptions{}
opt(opts)
require.Equal(t, uint32(2200), opts.unsubscribe.Code)
require.Equal(t, "x", opts.unsubscribe.Reason)
}
func TestWithDisconnectClient(t *testing.T) {
opt := WithDisconnectClient("client")
opts := &DisconnectOptions{}
opt(opts)
require.Equal(t, "client", opts.clientID)
}
func TestWithDisconnectSession(t *testing.T) {
opt := WithDisconnectSession("session")
opts := &DisconnectOptions{}
opt(opts)
require.Equal(t, "session", opts.sessionID)
}
func TestRefreshOptions(t *testing.T) {
refreshOpts := []RefreshOption{
WithRefreshClient("client"),
WithRefreshExpireAt(12),
WithRefreshExpired(true),
WithRefreshInfo([]byte(`test`)),
WithRefreshSession("session"),
}
opts := &RefreshOptions{}
for _, opt := range refreshOpts {
opt(opts)
}
require.Equal(t, int64(12), opts.ExpireAt)
require.True(t, opts.Expired)
require.Equal(t, "client", opts.clientID)
require.Equal(t, []byte(`test`), opts.Info)
require.Equal(t, "session", opts.sessionID)
}