6
6
"errors"
7
7
"fmt"
8
8
"testing"
9
+ "time"
9
10
10
11
"github.com/openslides/openslides-autoupdate-service/internal/autoupdate"
11
12
"github.com/openslides/openslides-autoupdate-service/internal/test"
@@ -86,7 +87,7 @@ func TestConnectionEmptyData(t *testing.T) {
86
87
closed := make (chan struct {})
87
88
defer close (closed )
88
89
89
- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
90
+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
90
91
91
92
kb := test.KeysBuilder {K : test .Str (doesExistKey , doesNotExistKey )}
92
93
@@ -189,7 +190,7 @@ func TestConnectionFilterData(t *testing.T) {
189
190
190
191
closed := make (chan struct {})
191
192
defer close (closed )
192
- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
193
+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
193
194
kb := test.KeysBuilder {K : test .Str ("user/1/name" )}
194
195
c := s .Connect (1 , kb )
195
196
if _ , err := c .Next (context .Background ()); err != nil {
@@ -214,7 +215,7 @@ func TestConntectionFilterOnlyOneKey(t *testing.T) {
214
215
datastore := new (test.MockDatastore )
215
216
closed := make (chan struct {})
216
217
close (closed )
217
- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
218
+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
218
219
kb := test.KeysBuilder {K : test .Str ("user/1/name" )}
219
220
c := s .Connect (1 , kb )
220
221
if _ , err := c .Next (context .Background ()); err != nil {
@@ -239,12 +240,102 @@ func TestConntectionFilterOnlyOneKey(t *testing.T) {
239
240
}
240
241
}
241
242
243
+ func TestFullUpdate (t * testing.T ) {
244
+ datastore := new (test.MockDatastore )
245
+ closed := make (chan struct {})
246
+ defer close (closed )
247
+ userUpdater := new (mockUserUpdater )
248
+ s := autoupdate .New (datastore , new (test.MockRestricter ), userUpdater , closed )
249
+ kb := test.KeysBuilder {K : test .Str ("user/1/name" )}
250
+
251
+ t .Run ("other user" , func (t * testing.T ) {
252
+ c := s .Connect (1 , kb )
253
+ if _ , err := c .Next (context .Background ()); err != nil {
254
+ t .Errorf ("c.Next() returned an error: %v" , err )
255
+ }
256
+
257
+ // send fulldata for other user
258
+ userUpdater .userIDs = []int {2 }
259
+ datastore .Send (test .Str ("some/5/data" ))
260
+
261
+ ctx , cancel := context .WithCancel (context .Background ())
262
+ defer cancel ()
263
+
264
+ var data map [string ]json.RawMessage
265
+ var err error
266
+ isBlocking := blocking (func () {
267
+ data , err = c .Next (ctx )
268
+ })
269
+
270
+ if ! isBlocking {
271
+ t .Fatalf ("fulldataupdate did not block" )
272
+ }
273
+
274
+ if err != nil {
275
+ t .Errorf ("Got unexpected error: %v" , err )
276
+ }
277
+
278
+ if len (data ) != 0 {
279
+ t .Errorf ("Got %v, expected no key update" , data )
280
+ }
281
+ })
282
+
283
+ t .Run ("same user" , func (t * testing.T ) {
284
+ c := s .Connect (1 , kb )
285
+ if _ , err := c .Next (context .Background ()); err != nil {
286
+ t .Errorf ("c.Next() returned an error: %v" , err )
287
+ }
288
+
289
+ // Send fulldata for same user.
290
+ userUpdater .userIDs = []int {1 }
291
+ datastore .Send (test .Str ("some/5/data" ))
292
+
293
+ ctx , cancel := context .WithCancel (context .Background ())
294
+ defer cancel ()
295
+
296
+ var data map [string ]json.RawMessage
297
+ var err error
298
+ isBlocking := blocking (func () {
299
+ data , err = c .Next (ctx )
300
+ })
301
+
302
+ if isBlocking {
303
+ t .Fatalf ("fulldataupdate did block" )
304
+ }
305
+
306
+ if err != nil {
307
+ t .Errorf ("Got unexpected error: %v" , err )
308
+ }
309
+
310
+ if len (data ) != 1 || string (data ["user/1/name" ]) != `"Hello World"` {
311
+ t .Errorf ("Got %v, expected [user/1/name: Hello World]" , data )
312
+ }
313
+ })
314
+ }
315
+
316
+ func blocking (f func ()) bool {
317
+ done := make (chan struct {})
318
+ go func () {
319
+ f ()
320
+ close (done )
321
+ }()
322
+
323
+ timer := time .NewTimer (time .Millisecond )
324
+ defer timer .Stop ()
325
+ select {
326
+ case <- done :
327
+ return false
328
+ case <- timer .C :
329
+ return true
330
+ }
331
+ }
332
+
242
333
func BenchmarkFilterChanging (b * testing.B ) {
243
334
const keyCount = 100
244
335
datastore := new (test.MockDatastore )
245
336
closed := make (chan struct {})
246
337
defer close (closed )
247
- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
338
+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
248
339
249
340
keys := make ([]string , 0 , keyCount )
250
341
for i := 0 ; i < keyCount ; i ++ {
@@ -271,7 +362,7 @@ func BenchmarkFilterNotChanging(b *testing.B) {
271
362
datastore := new (test.MockDatastore )
272
363
closed := make (chan struct {})
273
364
defer close (closed )
274
- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
365
+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
275
366
276
367
keys := make ([]string , 0 , keyCount )
277
368
for i := 0 ; i < keyCount ; i ++ {
0 commit comments