66 "errors"
77 "fmt"
88 "testing"
9+ "time"
910
1011 "github.com/openslides/openslides-autoupdate-service/internal/autoupdate"
1112 "github.com/openslides/openslides-autoupdate-service/internal/test"
@@ -86,7 +87,7 @@ func TestConnectionEmptyData(t *testing.T) {
8687 closed := make (chan struct {})
8788 defer close (closed )
8889
89- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
90+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
9091
9192 kb := test.KeysBuilder {K : test .Str (doesExistKey , doesNotExistKey )}
9293
@@ -189,7 +190,7 @@ func TestConnectionFilterData(t *testing.T) {
189190
190191 closed := make (chan struct {})
191192 defer close (closed )
192- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
193+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
193194 kb := test.KeysBuilder {K : test .Str ("user/1/name" )}
194195 c := s .Connect (1 , kb )
195196 if _ , err := c .Next (context .Background ()); err != nil {
@@ -214,7 +215,7 @@ func TestConntectionFilterOnlyOneKey(t *testing.T) {
214215 datastore := new (test.MockDatastore )
215216 closed := make (chan struct {})
216217 close (closed )
217- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
218+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
218219 kb := test.KeysBuilder {K : test .Str ("user/1/name" )}
219220 c := s .Connect (1 , kb )
220221 if _ , err := c .Next (context .Background ()); err != nil {
@@ -239,12 +240,102 @@ func TestConntectionFilterOnlyOneKey(t *testing.T) {
239240 }
240241}
241242
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+
242333func BenchmarkFilterChanging (b * testing.B ) {
243334 const keyCount = 100
244335 datastore := new (test.MockDatastore )
245336 closed := make (chan struct {})
246337 defer close (closed )
247- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
338+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
248339
249340 keys := make ([]string , 0 , keyCount )
250341 for i := 0 ; i < keyCount ; i ++ {
@@ -271,7 +362,7 @@ func BenchmarkFilterNotChanging(b *testing.B) {
271362 datastore := new (test.MockDatastore )
272363 closed := make (chan struct {})
273364 defer close (closed )
274- s := autoupdate .New (datastore , new (test.MockRestricter ), closed )
365+ s := autoupdate .New (datastore , new (test.MockRestricter ), mockUserUpdater {}, closed )
275366
276367 keys := make ([]string , 0 , keyCount )
277368 for i := 0 ; i < keyCount ; i ++ {
0 commit comments