forked from dunglas/mercure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolt_transport_test.go
255 lines (206 loc) · 5.6 KB
/
bolt_transport_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package hub
import (
"context"
"net/url"
"os"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
bolt "go.etcd.io/bbolt"
)
func TestBoltTransportHistory(t *testing.T) {
u, _ := url.Parse("bolt://test.db")
transport, _ := NewBoltTransport(u)
defer transport.Close()
defer os.Remove("test.db")
for i := 1; i <= 10; i++ {
transport.Write(&Update{Event: Event{ID: strconv.Itoa(i)}})
}
pipe, err := transport.CreatePipe("8")
assert.Nil(t, err)
require.NotNil(t, pipe)
var wg sync.WaitGroup
go func() {
var count int
for {
u, err := pipe.Read(context.Background())
if err == ErrClosedPipe {
return
}
// the reading loop must read the #9 and #10 messages
assert.Equal(t, strconv.Itoa(9+count), u.ID)
count++
if count == 2 {
wg.Done()
return
}
}
}()
wg.Wait()
pipe.Close()
}
func TestBoltTransportHistoryAndLive(t *testing.T) {
u, _ := url.Parse("bolt://test.db")
transport, _ := NewBoltTransport(u)
defer transport.Close()
defer os.Remove("test.db")
for i := 1; i <= 10; i++ {
transport.Write(&Update{Event: Event{ID: strconv.Itoa(i)}})
}
pipe, err := transport.CreatePipe("8")
assert.Nil(t, err)
require.NotNil(t, pipe)
var wg sync.WaitGroup
wg.Add(1)
go func() {
var count int
for {
u, err := pipe.Read(context.Background())
if err == ErrClosedPipe {
return
}
// the reading loop must read the #9, #10 and #11 messages
assert.Equal(t, strconv.Itoa(9+count), u.ID)
count++
if count == 3 {
wg.Done()
return
}
}
}()
transport.Write(&Update{Event: Event{ID: "11"}})
wg.Wait()
pipe.Close()
}
func TestBoltTransportPurgeHistory(t *testing.T) {
u, _ := url.Parse("bolt://test.db?size=5&cleanup_frequency=1")
transport, _ := NewBoltTransport(u)
defer transport.Close()
defer os.Remove("test.db")
for i := 0; i < 12; i++ {
transport.Write(&Update{Event: Event{ID: strconv.Itoa(i)}})
}
transport.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("updates"))
assert.Equal(t, 5, b.Stats().KeyN)
return nil
})
}
func TestNewBoltTransport(t *testing.T) {
u, _ := url.Parse("bolt://test.db?bucket_name=demo")
transport, err := NewBoltTransport(u)
assert.Nil(t, err)
require.NotNil(t, transport)
transport.Close()
u, _ = url.Parse("bolt://")
_, err = NewBoltTransport(u)
assert.EqualError(t, err, `invalid bolt DSN "bolt:": missing path`)
u, _ = url.Parse("bolt:///test.db")
_, err = NewBoltTransport(u)
assert.EqualError(t, err, `invalid bolt DSN "bolt:///test.db": open /test.db: permission denied`)
u, _ = url.Parse("bolt://test.db?cleanup_frequency=invalid")
_, err = NewBoltTransport(u)
assert.EqualError(t, err, `invalid bolt "bolt://test.db?cleanup_frequency=invalid" dsn: parameter cleanup_frequency: strconv.ParseFloat: parsing "invalid": invalid syntax`)
u, _ = url.Parse("bolt://test.db?size=invalid")
_, err = NewBoltTransport(u)
assert.EqualError(t, err, `invalid bolt "bolt://test.db?size=invalid" dsn: parameter size: strconv.ParseUint: parsing "invalid": invalid syntax`)
}
func TestBoltTransportWriteIsNotDispatchedUntilListen(t *testing.T) {
u, _ := url.Parse("bolt://test.db")
transport, _ := NewBoltTransport(u)
defer transport.Close()
defer os.Remove("test.db")
assert.Implements(t, (*Transport)(nil), transport)
pipe, err := transport.CreatePipe("")
assert.Nil(t, err)
require.NotNil(t, pipe)
var (
readUpdate *Update
readError error
m sync.Mutex
wg sync.WaitGroup
)
wg.Add(1)
go func() {
m.Lock()
defer m.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
go wg.Done()
readUpdate, readError = pipe.Read(ctx)
}()
wg.Wait()
pipe.Close()
m.Lock()
defer m.Unlock()
assert.Nil(t, readUpdate)
assert.Equal(t, ErrClosedPipe, readError)
}
func TestBoltTransportWriteIsDispatched(t *testing.T) {
u, _ := url.Parse("bolt://test.db")
transport, _ := NewBoltTransport(u)
defer transport.Close()
defer os.Remove("test.db")
assert.Implements(t, (*Transport)(nil), transport)
pipe, err := transport.CreatePipe("")
assert.Nil(t, err)
require.NotNil(t, pipe)
defer pipe.Close()
var (
readUpdate *Update
readError error
m sync.Mutex
wg sync.WaitGroup
)
wg.Add(1)
go func() {
m.Lock()
defer m.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
go wg.Done()
readUpdate, readError = pipe.Read(ctx)
}()
wg.Wait()
err = transport.Write(&Update{})
assert.Nil(t, err)
m.Lock()
defer m.Unlock()
assert.Nil(t, readError)
assert.NotNil(t, readUpdate)
}
func TestBoltTransportClosed(t *testing.T) {
u, _ := url.Parse("bolt://test.db")
transport, _ := NewBoltTransport(u)
require.NotNil(t, transport)
defer transport.Close()
defer os.Remove("test.db")
assert.Implements(t, (*Transport)(nil), transport)
pipe, _ := transport.CreatePipe("")
require.NotNil(t, pipe)
err := transport.Close()
assert.Nil(t, err)
_, err = transport.CreatePipe("")
assert.Equal(t, err, ErrClosedTransport)
err = transport.Write(&Update{})
assert.Equal(t, err, ErrClosedTransport)
_, err = pipe.Read(context.Background())
assert.Equal(t, err, ErrClosedPipe)
}
func TestBoltCleanClosedPipes(t *testing.T) {
u, _ := url.Parse("bolt://test.db")
transport, _ := NewBoltTransport(u)
require.NotNil(t, transport)
defer transport.Close()
defer os.Remove("test.db")
pipe, _ := transport.CreatePipe("")
require.NotNil(t, pipe)
assert.Len(t, transport.pipes, 1)
pipe.Close()
assert.Len(t, transport.pipes, 1)
transport.Write(&Update{})
assert.Len(t, transport.pipes, 0)
}