Repository navigation
Expand file tree
/
Copy pathDataTestIT.java
More file actions
2917 lines (2418 loc) · 106 KB
/
Copy pathDataTestIT.java
File metadata and controls
2917 lines (2418 loc) · 106 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.database.integration;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseException;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.EventRecord;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.MapBuilder;
import com.google.firebase.database.MutableData;
import com.google.firebase.database.ServerValue;
import com.google.firebase.database.TestFailure;
import com.google.firebase.database.TestHelpers;
import com.google.firebase.database.Transaction;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.core.DatabaseConfig;
import com.google.firebase.database.core.Path;
import com.google.firebase.database.core.RepoManager;
import com.google.firebase.database.core.view.Event;
import com.google.firebase.database.future.ReadFuture;
import com.google.firebase.database.future.WriteFuture;
import com.google.firebase.testing.IntegrationTestUtils;
import com.google.firebase.testing.TestUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class DataTestIT {
private static FirebaseApp masterApp;
@BeforeClass
public static void setUpClass() {
masterApp = IntegrationTestUtils.ensureDefaultApp();
}
@Before
public void prepareApp() {
TestHelpers.wrapForErrorHandling(masterApp);
}
@After
public void checkAndCleanupApp() {
TestHelpers.assertAndUnwrapErrorHandlers(masterApp);
}
@Test
public void testBasicInstantiation() {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
assertTrue(ref != null);
}
@Test
public void testWriteData() {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
// just make sure it doesn't throw
ref.setValueAsync(42);
assertTrue(true);
}
@Test
public void testReadAndWrite() throws InterruptedException, TimeoutException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ReadFuture future = ReadFuture.untilNonNull(ref);
ref.setValueAsync(42);
List<EventRecord> events = future.timedGet();
assertEquals(42L, events.get(events.size() - 1).getSnapshot().getValue());
}
@Test
public void testValueReturnsJsonForNodesWithChildren()
throws TimeoutException, InterruptedException, TestFailure {
Map<String, Object> expected = new HashMap<>();
Map<String, Object> innerExpected = new HashMap<>();
innerExpected.put("bar", 5L);
expected.put("foo", innerExpected);
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ReadFuture future = ReadFuture.untilNonNull(ref);
ref.setValueAsync(expected);
List<EventRecord> events = future.timedGet();
EventRecord eventRecord = events.get(events.size() - 1);
Object result = eventRecord.getSnapshot().getValue();
TestHelpers.assertDeepEquals(expected, result);
}
@Test
public void testWriteAndWaitForServerConfirmation()
throws TimeoutException, InterruptedException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ref.setValueAsync(42);
ReadFuture future = new ReadFuture(ref);
EventRecord eventRecord = future.timedGet().get(0);
assertEquals(42L, eventRecord.getSnapshot().getValue());
}
@Test
public void testWriteValueReconnectRead()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference reader = refs.get(0);
DatabaseReference writer = refs.get(1);
WriteFuture writeFuture = new WriteFuture(writer, 42);
writeFuture.timedGet();
ReadFuture future = new ReadFuture(reader);
EventRecord eventRecord = future.timedGet().get(0);
long result = (Long) eventRecord.getSnapshot().getValue();
assertEquals(42L, result);
assertEquals(42, (int) eventRecord.getSnapshot().getValue(Integer.class));
}
@Test
public void testMultipleWriteValuesReconnectRead()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
final DatabaseReference writer = refs.get(0);
final DatabaseReference reader = refs.get(1);
writer.child("a").child("b").child("c").setValueAsync(1);
writer.child("a").child("d").child("e").setValueAsync(2);
writer.child("a").child("d").child("f").setValueAsync(3);
WriteFuture writeFuture = new WriteFuture(writer.child("g"), 4);
writeFuture.timedGet();
Map<String, Object> expected = new MapBuilder()
.put("a",
new MapBuilder().put("b", new MapBuilder().put("c", 1L).build())
.put("d", new MapBuilder().put("e", 2L).put("f", 3L).build()).build())
.put("g", 4L).build();
ReadFuture readFuture = new ReadFuture(reader);
GenericTypeIndicator<Map<String, Object>> t = new GenericTypeIndicator<Map<String, Object>>() {
};
Map<String, Object> result = readFuture.timedGet().get(0).getSnapshot().getValue(t);
TestHelpers.assertDeepEquals(expected, result);
}
@Test
public void testWriteLeafNodeOverwriteParentNodeWaitForEvents() throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
EventHelper helper = new EventHelper().addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(ref.child("a")).addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_CHANGED, "aa")
.addValueExpectation(ref.child("a")).startListening(true);
ref.child("a/aa").setValueAsync(1);
ref.child("a").setValueAsync(new MapBuilder().put("aa", 2).build());
assertTrue(helper.waitForEvents());
helper.cleanup();
}
@Test
public void testWriteLeafNodeOverwriteAtParentMultipleTimesWaitForExpectedEvents()
throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
final AtomicInteger bbCount = new AtomicInteger(0);
final ValueEventListener listener = ref.child("a/bb")
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
assertNull(snapshot.getValue());
assertEquals(1, bbCount.incrementAndGet());
}
@Override
public void onCancelled(DatabaseError error) {
}
});
final EventHelper helper = new EventHelper().addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(ref.child("a")).addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_CHANGED, "aa")
.addValueExpectation(ref.child("a")).addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_CHANGED, "aa")
.addValueExpectation(ref.child("a")).startListening(true);
ref.child("a/aa").setValueAsync(1);
ref.child("a").setValueAsync(MapBuilder.of("aa", 2));
ref.child("a").setValueAsync(MapBuilder.of("aa", 3));
ref.child("a").setValueAsync(MapBuilder.of("aa", 3));
assertTrue(helper.waitForEvents());
helper.cleanup();
assertEquals(1, bbCount.get());
ref.child("a/bb").removeEventListener(listener);
}
@Test
public void testWriteParentNodeOverwriteAtLeafNodeWaitForEvents() throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
EventHelper helper = new EventHelper().addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(ref.child("a")).addValueExpectation(ref.child("a/aa"))
.addChildExpectation(ref.child("a"), Event.EventType.CHILD_CHANGED, "aa")
.addValueExpectation(ref.child("a")).startListening(true);
ref.child("a").setValueAsync(new MapBuilder().put("aa", 2).build());
ref.child("a/aa").setValueAsync(1);
assertTrue(helper.waitForEvents());
helper.cleanup();
}
@Test
@Ignore
public void testWriteLeafNodeRemoveParentWaitForEvents()
throws InterruptedException, TimeoutException, TestFailure, ExecutionException {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference reader = refs.get(0);
DatabaseReference writer = refs.get(1);
EventHelper writeHelper = new EventHelper().addValueExpectation(writer.child("a/aa"))
.addChildExpectation(writer.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(writer.child("a"))
.addChildExpectation(writer, Event.EventType.CHILD_ADDED, "a").addValueExpectation(writer)
.startListening(true);
WriteFuture w = new WriteFuture(writer.child("a/aa"), 42);
assertTrue(writeHelper.waitForEvents());
w.timedGet();
EventHelper readHelper = new EventHelper().addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_ADDED, "a").addValueExpectation(reader)
.startListening();
assertTrue(readHelper.waitForEvents());
readHelper.addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_REMOVED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_REMOVED, "a").addValueExpectation(reader)
.startListening();
writeHelper.addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_REMOVED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_REMOVED, "a").addValueExpectation(reader)
.startListening();
writer.child("a").removeValueAsync();
assertTrue(writeHelper.waitForEvents());
assertTrue(readHelper.waitForEvents());
writeHelper.cleanup();
readHelper.cleanup();
// Make sure we actually have null there now
assertNull(TestHelpers.getSnap(reader).getValue());
assertNull(TestHelpers.getSnap(writer).getValue());
ReadFuture readFuture = ReadFuture.untilNonNull(reader);
ReadFuture writeFuture = ReadFuture.untilNonNull(writer);
writer.child("a/aa").setValueAsync(3.1415);
List<EventRecord> readerEvents = readFuture.timedGet();
List<EventRecord> writerEvents = writeFuture.timedGet();
DataSnapshot readerSnap = readerEvents.get(readerEvents.size() - 1).getSnapshot();
readerSnap = readerSnap.child("a/aa");
assertEquals(3.1415, readerSnap.getValue());
DataSnapshot writerSnap = writerEvents.get(writerEvents.size() - 1).getSnapshot();
writerSnap = writerSnap.child("a/aa");
assertEquals(3.1415, writerSnap.getValue());
}
@Test
@Ignore
public void testWriteLeafNodeRemoveLeafNodeWaitForEvents()
throws InterruptedException, TimeoutException, TestFailure, ExecutionException {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference reader = refs.get(0);
DatabaseReference writer = refs.get(1);
EventHelper writeHelper = new EventHelper().addValueExpectation(writer.child("a/aa"))
.addChildExpectation(writer.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(writer.child("a"))
.addChildExpectation(writer, Event.EventType.CHILD_ADDED, "a").addValueExpectation(writer)
.startListening(true);
WriteFuture w = new WriteFuture(writer.child("a/aa"), 42);
assertTrue(writeHelper.waitForEvents());
w.timedGet();
EventHelper readHelper = new EventHelper().addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_ADDED, "a").addValueExpectation(reader)
.startListening();
assertTrue(readHelper.waitForEvents());
readHelper.addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_REMOVED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_REMOVED, "a").addValueExpectation(reader)
.startListening();
writeHelper.addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_REMOVED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_REMOVED, "a").addValueExpectation(reader)
.startListening();
writer.child("a/aa").removeValueAsync();
assertTrue(writeHelper.waitForEvents());
assertTrue(readHelper.waitForEvents());
writeHelper.cleanup();
readHelper.cleanup();
DataSnapshot readerSnap = TestHelpers.getSnap(reader);
assertNull(readerSnap.getValue());
DataSnapshot writerSnap = TestHelpers.getSnap(writer);
assertNull(writerSnap.getValue());
readerSnap = TestHelpers.getSnap(reader.child("a/aa"));
assertNull(readerSnap.getValue());
writerSnap = TestHelpers.getSnap(writer.child("a/aa"));
assertNull(writerSnap.getValue());
ReadFuture readFuture = ReadFuture.untilNonNull(reader);
ReadFuture writeFuture = ReadFuture.untilNonNull(writer);
writer.child("a/aa").setValueAsync(3.1415);
final List<EventRecord> readerEvents = readFuture.timedGet();
final List<EventRecord> writerEvents = writeFuture.timedGet();
readerSnap = readerEvents.get(readerEvents.size() - 1).getSnapshot();
readerSnap = readerSnap.child("a/aa");
assertEquals(3.1415, readerSnap.getValue());
writerSnap = writerEvents.get(writerEvents.size() - 1).getSnapshot();
writerSnap = writerSnap.child("a/aa");
assertEquals(3.1415, writerSnap.getValue());
}
@Test
public void testWriteMultipleLeafNodesRemoveOneLeafNodeWaitForEvents()
throws InterruptedException {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference reader = refs.get(0);
DatabaseReference writer = refs.get(1);
final EventHelper writeHelper = new EventHelper().addValueExpectation(writer.child("a/aa"))
.addChildExpectation(writer.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(writer.child("a"))
.addChildExpectation(writer, Event.EventType.CHILD_ADDED, "a").addValueExpectation(writer)
.addChildExpectation(writer.child("a"), Event.EventType.CHILD_ADDED, "bb")
.addValueExpectation(writer.child("a"))
.addChildExpectation(writer, Event.EventType.CHILD_CHANGED, "a").addValueExpectation(writer)
.startListening(true);
final EventHelper readHelper = new EventHelper().addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_ADDED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_ADDED, "a").addValueExpectation(reader)
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_ADDED, "bb")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_CHANGED, "a").addValueExpectation(reader)
.startListening(true);
writer.child("a/aa").setValueAsync(42);
writer.child("a/bb").setValueAsync(24);
assertTrue(writeHelper.waitForEvents());
assertTrue(readHelper.waitForEvents());
readHelper.addValueExpectation(reader.child("a/aa"))
.addChildExpectation(reader.child("a"), Event.EventType.CHILD_REMOVED, "aa")
.addValueExpectation(reader.child("a"))
.addChildExpectation(reader, Event.EventType.CHILD_CHANGED, "a").addValueExpectation(reader)
.startListening();
writeHelper.addValueExpectation(writer.child("a/aa"))
.addChildExpectation(writer.child("a"), Event.EventType.CHILD_REMOVED, "aa")
.addValueExpectation(writer.child("a"))
.addChildExpectation(writer, Event.EventType.CHILD_CHANGED, "a").addValueExpectation(writer)
.startListening();
writer.child("a/aa").removeValueAsync();
assertTrue(writeHelper.waitForEvents());
assertTrue(readHelper.waitForEvents());
DataSnapshot readerSnap = TestHelpers.getSnap(reader);
DataSnapshot writerSnap = TestHelpers.getSnap(writer);
Map<String, Object> expected = new MapBuilder()
.put("a", new MapBuilder().put("bb", 24L).build()).build();
TestHelpers.assertDeepEquals(expected, readerSnap.getValue());
TestHelpers.assertDeepEquals(expected, writerSnap.getValue());
readHelper.cleanup();
writeHelper.cleanup();
}
@Test
public void testVerifyNodesStartingWithPeriod() {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
try {
ref.child(".foo");
fail("Should fail");
} catch (DatabaseException e) {
// No-op
}
try {
ref.child("foo/.foo");
fail("Should fail");
} catch (DatabaseException e) {
// No-op
}
}
// NOTE: skipping test re: writing .keys and .length. Those features will
// require a new client
// anyways
@Test
public void testNumericKeysGetTurnedIntoArrays() throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ref.child("0").setValueAsync("alpha");
ref.child("1").setValueAsync("bravo");
ref.child("2").setValueAsync("charlie");
ref.child("3").setValueAsync("delta");
ref.child("4").setValueAsync("echo");
DataSnapshot snap = TestHelpers.getSnap(ref);
List<Object> expected = ImmutableList.of((Object) "alpha", "bravo", "charlie",
"delta", "echo");
TestHelpers.assertDeepEquals(expected, snap.getValue());
}
@Test
public void testWriteFullJsonObjectsWithSetAndGetThemBack()
throws TimeoutException, InterruptedException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
Map<String, Object> expected = new MapBuilder()
.put("a", new MapBuilder().put("aa", 5L).put("ab", 3L).build())
.put("b",
new MapBuilder().put("ba", "hey there!")
.put("bb", MapBuilder.of("bba", false)).build())
.put("c", ImmutableList.of(0L, new MapBuilder().put("c_1", 4L).build(), "hey", true, false,
"dude"))
.build();
ReadFuture readFuture = ReadFuture.untilNonNull(ref);
ref.setValueAsync(expected);
List<EventRecord> events = readFuture.timedGet();
Object result = events.get(events.size() - 1).getSnapshot().getValue();
TestHelpers.assertDeepEquals(expected, result);
}
// NOTE: skipping test for value in callback. DataSnapshot is same instance in
// callback as after
// future is complete
// NOTE: skipping test for passing a value to push. Not applicable.
@Test
public void testRemoveCallbackIsHit()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
final DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
WriteFuture writeFuture = new WriteFuture(ref, 42);
writeFuture.timedGet();
ReadFuture readFuture = new ReadFuture(ref, new ReadFuture.CompletionCondition() {
@Override
public boolean isComplete(List<EventRecord> events) {
return events.get(events.size() - 1).getSnapshot().getValue() == null;
}
});
final Semaphore callbackHit = new Semaphore(0);
ref.removeValue(new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference callbackRef) {
assertEquals(ref, callbackRef);
callbackHit.release(1);
}
});
readFuture.timedGet();
assertTrue(callbackHit.tryAcquire(1, TestUtils.TEST_TIMEOUT_MILLIS, MILLISECONDS));
// We test the value in the completion condition
}
@Test
public void testRemoveCallbackIsHitForAlreadyRemovedNodes() throws InterruptedException {
final DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
final Semaphore callbackHit = new Semaphore(0);
ref.removeValue(new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference callbackRef) {
assertEquals(ref, callbackRef);
callbackHit.release(1);
}
});
ref.removeValue(new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference callbackRef) {
assertEquals(ref, callbackRef);
callbackHit.release(1);
}
});
assertTrue(
callbackHit.tryAcquire(2, TestUtils.TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
@Test
public void testUsingNumbersAsKeys()
throws TimeoutException, InterruptedException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ref.child("3024").setValueAsync(5);
ReadFuture future = new ReadFuture(ref);
List<EventRecord> events = future.timedGet();
assertFalse(events.get(0).getSnapshot().getValue() instanceof List);
}
@Test
public void testOnceWithCallbackHitsServerToGetData()
throws InterruptedException, ExecutionException, TimeoutException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 3);
DatabaseReference writer = refs.get(0);
DatabaseReference reader = refs.get(1);
DatabaseReference reader2 = refs.get(2);
final Semaphore semaphore = new Semaphore(0);
reader.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
assertEquals(null, snapshot.getValue());
semaphore.release(1);
}
@Override
public void onCancelled(DatabaseError error) {
fail("Shouldn't happen");
}
});
TestHelpers.waitFor(semaphore);
new WriteFuture(writer, 42).timedGet();
reader2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
assertEquals(42L, snapshot.getValue());
semaphore.release(1);
}
@Override
public void onCancelled(DatabaseError error) {
fail("Shouldn't happen");
}
});
TestHelpers.waitFor(semaphore);
}
// NOTE: skipping forEach abort test. Not relevant, we return an iterable that
// can be stopped
// any
// time.
@Test
public void testSetAndThenListenForValueEvents()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
new WriteFuture(ref, "cabbage").timedGet();
EventRecord event = new ReadFuture(ref).timedGet().get(0);
assertEquals("cabbage", event.getSnapshot().getValue());
}
@Test
public void testHasChildren()
throws TimeoutException, InterruptedException, TestFailure {
final DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ref.setValueAsync(new MapBuilder()
.put("one", 42)
.put("two", new MapBuilder().put("a", 5).build())
.put("three", new MapBuilder().put("a", 5).put("b", 6).build()).build());
final AtomicBoolean removedTwo = new AtomicBoolean(false);
ReadFuture readFuture = new ReadFuture(ref, new ReadFuture.CompletionCondition() {
@Override
public boolean isComplete(List<EventRecord> events) {
if (removedTwo.compareAndSet(false, true)) {
// removedTwo did equal false, now equals true
try {
ref.child("two").removeValueAsync();
} catch (DatabaseException e) {
fail("Should not fail");
}
}
return events.size() == 2;
}
});
List<EventRecord> events = readFuture.timedGet();
DataSnapshot firstSnap = events.get(0).getSnapshot();
assertEquals(3, firstSnap.getChildrenCount());
assertEquals(0, firstSnap.child("one").getChildrenCount());
assertEquals(1, firstSnap.child("two").getChildrenCount());
assertEquals(2, firstSnap.child("three").getChildrenCount());
assertEquals(0, firstSnap.child("four").getChildrenCount());
DataSnapshot secondSnap = events.get(1).getSnapshot();
assertEquals(2, secondSnap.getChildrenCount());
assertEquals(0, secondSnap.child("two").getChildrenCount());
}
@Test
public void testSetNodeWithChildrenToPrimitiveValueAndBack()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference reader = refs.get(0);
final DatabaseReference writer = refs.get(1);
final Map<String, Object> json = new MapBuilder().put("a", 5L).put("b", 6L).build();
final long primitive = 76L;
new WriteFuture(writer, json).timedGet();
final AtomicBoolean sawJson = new AtomicBoolean(false);
final AtomicBoolean sawPrimitive = new AtomicBoolean(false);
ReadFuture readFuture = new ReadFuture(reader, new ReadFuture.CompletionCondition() {
@Override
public boolean isComplete(List<EventRecord> events) {
if (sawJson.compareAndSet(false, true)) {
try {
writer.setValueAsync(primitive);
} catch (DatabaseException e) {
fail("Shouldn't happen: " + e.toString());
}
} else {
// Saw the json already
if (sawPrimitive.compareAndSet(false, true)) {
try {
writer.setValueAsync(json);
} catch (DatabaseException e) {
fail("Shouldn't happen: " + e.toString());
}
}
}
return events.size() == 3;
}
});
List<EventRecord> events = readFuture.timedGet();
DataSnapshot readSnap = events.get(0).getSnapshot();
assertTrue(readSnap.hasChildren());
TestHelpers.assertDeepEquals(json, readSnap.getValue());
readSnap = events.get(1).getSnapshot();
assertFalse(readSnap.hasChildren());
assertEquals(primitive, readSnap.getValue());
readSnap = events.get(2).getSnapshot();
assertTrue(readSnap.hasChildren());
TestHelpers.assertDeepEquals(json, readSnap.getValue());
}
@Test
public void testWriteLeafNodeRemoveItTryToAddChildToRemovedNode()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
final DatabaseReference reader = refs.get(0);
final DatabaseReference writer = refs.get(1);
writer.setValueAsync(5);
writer.removeValueAsync();
new WriteFuture(writer.child("abc"), 5).timedGet();
DataSnapshot snap = new ReadFuture(reader).timedGet().get(0).getSnapshot();
assertEquals(5L, ((Map) snap.getValue()).get("abc"));
}
@Test
public void testListenForValueThenWriteOnNodeWithExistingData()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference reader = refs.get(0);
DatabaseReference writer = refs.get(1);
new WriteFuture(writer, new MapBuilder().put("a", 5).put("b", 2).build()).timedGet();
ReadFuture readFuture = new ReadFuture(reader);
// Slight race condition. We're banking on this local set being processed
// before the network catches up with the writer's broadcast.
reader.child("a").setValueAsync(10);
EventRecord event = readFuture.timedGet().get(0);
assertEquals(10L, event.getSnapshot().child("a").getValue());
}
@Test
public void testSetPriorityOnNonexistentNode() throws InterruptedException {
final DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
final Semaphore semaphore = new Semaphore(0);
ref.setPriority(1, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference callbackRef) {
assertEquals(ref, callbackRef);
assertNotNull(error);
semaphore.release(1);
}
});
assertTrue(semaphore.tryAcquire(1, TestUtils.TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
@Test
public void testSetPriority() throws InterruptedException {
final DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ref.setValueAsync("hello!");
final Semaphore semaphore = new Semaphore(0);
ref.setPriority(10, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference callbackRef) {
assertEquals(ref, callbackRef);
assertNull(error);
semaphore.release(1);
}
});
assertTrue(semaphore.tryAcquire(1, TestUtils.TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
@Test
public void testSetWithPriority()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
final DatabaseReference ref1 = refs.get(0);
final DatabaseReference ref2 = refs.get(1);
ReadFuture readFuture = ReadFuture.untilNonNull(ref1);
new WriteFuture(ref1, "hello", 5).timedGet();
List<EventRecord> result = readFuture.timedGet();
DataSnapshot snap = result.get(result.size() - 1).getSnapshot();
assertEquals(5.0, snap.getPriority());
assertEquals("hello", snap.getValue());
result = ReadFuture.untilNonNull(ref2).timedGet();
snap = result.get(result.size() - 1).getSnapshot();
assertEquals(5.0, snap.getPriority());
assertEquals("hello", snap.getValue());
}
// NOTE: skipped test of getPriority on snapshot. Tested above
// NOTE: skipped test of immediately visible priority changes. Tested above
@Test
public void testSetOverwritesPriorityOfTopLevelNodesAndChildren()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
final DatabaseReference ref1 = refs.get(0);
final DatabaseReference ref2 = refs.get(1);
ref1.setValueAsync(new MapBuilder().put("a", 5).build());
ref1.setPriorityAsync(10);
ref1.child("a").setPriorityAsync(18);
new WriteFuture(ref1, new MapBuilder().put("a", 7).build()).timedGet();
DataSnapshot snap = new ReadFuture(ref2).timedGet().get(0).getSnapshot();
assertNull(snap.getPriority());
assertNull(snap.child("a").getPriority());
}
@Test
public void testSetWithPriorityOfLeafNodes()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference ref1 = refs.get(0);
DatabaseReference ref2 = refs.get(1);
new WriteFuture(ref1, "testleaf", "992").timedGet();
DataSnapshot snap = new ReadFuture(ref2).timedGet().get(0).getSnapshot();
assertEquals("992", snap.getPriority());
}
@Test
public void testSetPriorityOfAnObject()
throws ExecutionException, TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference ref1 = refs.get(0);
DatabaseReference ref2 = refs.get(1);
new WriteFuture(ref1, new MapBuilder().put("a", 5).build(), "991").timedGet();
DataSnapshot snap = new ReadFuture(ref2).timedGet().get(0).getSnapshot();
assertEquals("991", snap.getPriority());
}
// NOTE: skipping a test about following setPriority with set, it's tested
// above
@Test
public void testGetPriority()
throws TimeoutException, InterruptedException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ReadFuture readFuture = ReadFuture.untilCountAfterNull(ref, 7);
ref.setValueAsync("a");
ref.setValueAsync("b", 5);
ref.setValueAsync("c", "6");
ref.setValueAsync("d", 7);
ref.setValueAsync(new MapBuilder().put(".value", "e").put(".priority", 8).build());
ref.setValueAsync(new MapBuilder().put(".value", "f").put(".priority", "8").build());
ref.setValueAsync(new MapBuilder().put(".value", "g").put(".priority", null).build());
List<EventRecord> events = readFuture.timedGet();
assertNull(events.get(0).getSnapshot().getPriority());
assertEquals(5.0, events.get(1).getSnapshot().getPriority());
assertEquals("6", events.get(2).getSnapshot().getPriority());
assertEquals(7.0, events.get(3).getSnapshot().getPriority());
assertEquals(8.0, events.get(4).getSnapshot().getPriority());
assertEquals("8", events.get(5).getSnapshot().getPriority());
assertNull(events.get(6).getSnapshot().getPriority());
}
@Test
public void testNormalizeDifferentIntegerAndDoubleValues()
throws DatabaseException, InterruptedException, TimeoutException, TestFailure {
final long intMaxPlusOne = 2147483648L;
DatabaseReference node = IntegrationTestUtils.getRandomNode(masterApp);
Object[] writtenValues = {
intMaxPlusOne,
(double) intMaxPlusOne,
-intMaxPlusOne,
(double) -intMaxPlusOne,
Integer.MAX_VALUE,
0L,
0.0,
-0.0f,
0
};
Object[] readValues = {intMaxPlusOne, -intMaxPlusOne, (long) Integer.MAX_VALUE, 0L};
ReadFuture readFuture = ReadFuture.untilCountAfterNull(node, readValues.length);
for (Object value : writtenValues) {
node.setValueAsync(value);
}
List<EventRecord> events = readFuture.timedGet();
for (int i = 0; i < readValues.length; ++i) {
assertEquals(readValues[i], events.get(i).getSnapshot().getValue());
}
}
@Test
public void testExportFormatIncludesPriorities()
throws TimeoutException, InterruptedException, TestFailure {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
Map<String, Object> expected = new MapBuilder().put("foo",
new MapBuilder()
.put("bar", new MapBuilder().put(".priority", 7.0).put(".value", 5L).build())
.put(".priority", "hi").build())
.build();
ReadFuture readFuture = ReadFuture.untilNonNull(ref);
ref.setValueAsync(expected);
DataSnapshot snap = readFuture.timedGet().get(0).getSnapshot();
Object result = snap.getValue(true);
TestHelpers.assertDeepEquals(expected, result);
}
@Test
public void testPriorityIsOverwrittenByServerPriority()
throws TimeoutException, InterruptedException, TestFailure {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference ref1 = refs.get(0);
final DatabaseReference ref2 = refs.get(1);
ReadFuture readFuture = ReadFuture.untilCountAfterNull(ref1, 2);
ref1.setValueAsync("hi", 100);
new ReadFuture(ref2, new ReadFuture.CompletionCondition() {
@Override
public boolean isComplete(List<EventRecord> events) {
DataSnapshot snap = events.get(events.size() - 1).getSnapshot();
Object priority = snap.getPriority();
if (priority != null && priority.equals(100.0)) {
try {
ref2.setValueAsync("whatever");
} catch (DatabaseException e) {
fail("Shouldn't happen: " + e.toString());
}
return true;
}
return false;
}
}).timedGet();
List<EventRecord> events = readFuture.timedGet();
assertEquals(100.0, events.get(0).getSnapshot().getPriority());
assertNull(events.get(1).getSnapshot().getPriority());
}
@Test
public void testLargeNumericPriorities()
throws TestFailure, TimeoutException, InterruptedException, ExecutionException {
List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2);
DatabaseReference ref1 = refs.get(0);
DatabaseReference ref2 = refs.get(1);
double priority = 1356721306842.0;
new WriteFuture(ref1, 5, priority).timedGet();
DataSnapshot snap = new ReadFuture(ref2).timedGet().get(0).getSnapshot();
assertEquals(priority, snap.getPriority());
}