@@ -4594,7 +4594,6 @@ public void testGetResolvesToPersistentCacheWhenOfflineAndNoListeners()
4594
4594
db .setPersistenceEnabled (true );
4595
4595
DatabaseReference node = db .getReference ().push ();
4596
4596
long val = 34 ;
4597
- Thread .currentThread ().getId ();
4598
4597
4599
4598
try {
4600
4599
await (node .setValue (val ));
@@ -4617,7 +4616,6 @@ public void testGetResolvesToCacheWhenOnlineAndParentListener()
4617
4616
// To ensure that we don't read the cached results, we need a separate app.
4618
4617
FirebaseApp readApp =
4619
4618
appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4620
- // String topLevelKey = UUID.randomUUID().toString();
4621
4619
FirebaseDatabase writeDb = FirebaseDatabase .getInstance (writeApp );
4622
4620
FirebaseDatabase readDb = FirebaseDatabase .getInstance (readApp );
4623
4621
long val = 34 ;
@@ -4635,7 +4633,6 @@ public void testGetResolvesToCacheWhenOnlineAndParentListener()
4635
4633
assertEquals (1 , events .size ());
4636
4634
DataSnapshot childNode = events .get (0 ).getSnapshot ().child (writeKey );
4637
4635
assertEquals (34L , childNode .getValue ());
4638
-
4639
4636
return true ;
4640
4637
});
4641
4638
readFuture .timedGet ();
@@ -4649,42 +4646,32 @@ public void testGetResolvesToCacheWhenOnlineAndParentListener()
4649
4646
4650
4647
@ Test
4651
4648
public void testGetResolvesToCacheWhenOnlineAndSameLevelListener ()
4652
- throws DatabaseException , InterruptedException , ExecutionException {
4649
+ throws DatabaseException , InterruptedException , ExecutionException , TestFailure ,
4650
+ TimeoutException {
4653
4651
FirebaseApp writeApp =
4654
4652
appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4655
4653
// To ensure that we don't read the cached results, we need a separate app.
4656
4654
FirebaseApp readApp =
4657
4655
appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4658
4656
FirebaseDatabase writeDb = FirebaseDatabase .getInstance (writeApp );
4659
4657
FirebaseDatabase readDb = FirebaseDatabase .getInstance (readApp );
4660
- Semaphore semaphore = new Semaphore (0 );
4661
4658
long val = 34 ;
4662
4659
String writeKey = Objects .requireNonNull (writeDb .getReference ().push ().getKey ());
4663
4660
DatabaseReference writeNode = writeDb .getReference ().child (writeKey );
4664
4661
await (writeNode .setValue (val ));
4665
4662
DatabaseReference readNode = readDb .getReference ().child (writeKey );
4666
- final int testArr [] = {0 };
4667
- ValueEventListener listener =
4668
- new ValueEventListener () {
4669
- @ Override
4670
- public void onDataChange (@ NonNull DataSnapshot snapshot ) {
4671
- assertEquals (0 , testArr [0 ]++);
4672
- assertEquals (34L , snapshot .getValue ());
4673
- semaphore .release ();
4674
- }
4675
-
4676
- @ Override
4677
- public void onCancelled (@ NonNull DatabaseError error ) {
4678
- // no-op
4679
- }
4680
- };
4681
- readNode .addValueEventListener (listener );
4663
+ new ReadFuture (
4664
+ readNode ,
4665
+ events -> {
4666
+ assertEquals (1 , events .size ());
4667
+ assertEquals (val , events .get (0 ).getSnapshot ().getValue ());
4668
+ return true ;
4669
+ })
4670
+ .timedGet ();
4682
4671
4683
4672
try {
4684
- IntegrationTestHelpers .waitFor (semaphore );
4685
4673
DataSnapshot snapshot = await (readNode .get ());
4686
4674
assertEquals (val , snapshot .getValue ());
4687
- readNode .removeEventListener (listener );
4688
4675
// need to rewrite it
4689
4676
} catch (ExecutionException e ) {
4690
4677
fail ("get threw an exception: " + e );
@@ -4698,31 +4685,30 @@ public void testGetResolvesToServerCacheWhenListenerIsAvailable()
4698
4685
FirebaseApp app =
4699
4686
appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4700
4687
FirebaseDatabase db = FirebaseDatabase .getInstance (app );
4701
- Semaphore semaphore = new Semaphore ( 0 );
4688
+ db . setPersistenceEnabled ( false );
4702
4689
long val = 34 ;
4703
4690
DatabaseReference node = db .getReference ().push ();
4704
- node .setValue (val );
4705
-
4706
- ValueEventListener listener =
4707
- new ValueEventListener () {
4708
- @ Override
4709
- public void onDataChange (@ NonNull DataSnapshot snapshot ) {
4710
- assertEquals (34L , snapshot .getValue ());
4711
- semaphore .release ();
4712
- }
4713
-
4714
- @ Override
4715
- public void onCancelled (@ NonNull DatabaseError error ) {
4716
- // no-op
4717
- }
4718
- };
4719
- node .addValueEventListener (listener );
4720
4691
4721
4692
try {
4693
+ await (node .setValue (val ));
4694
+ Semaphore semaphore = new Semaphore (0 );
4695
+ ValueEventListener listener =
4696
+ new ValueEventListener () {
4697
+ @ Override
4698
+ public void onDataChange (@ NonNull DataSnapshot snapshot ) {
4699
+ assertEquals (val , snapshot .getValue ());
4700
+ semaphore .release ();
4701
+ }
4702
+
4703
+ @ Override
4704
+ public void onCancelled (@ NonNull DatabaseError error ) {}
4705
+ };
4706
+ node .addValueEventListener (listener );
4722
4707
IntegrationTestHelpers .waitFor (semaphore );
4723
4708
db .goOffline ();
4724
4709
DataSnapshot snapshot = await (node .get ());
4725
4710
assertEquals (val , snapshot .getValue ());
4711
+ node .removeEventListener (listener );
4726
4712
db .goOnline ();
4727
4713
} catch (ExecutionException e ) {
4728
4714
fail ("get threw an exception: " + e );
@@ -4742,15 +4728,14 @@ public void testGetResolvesToCacheWhenOnlineAndChildLevelListener()
4742
4728
long val = 34 ;
4743
4729
String parentNodeKey = Objects .requireNonNull (writeDb .getReference ().push ().getKey ());
4744
4730
DatabaseReference parentWriteNode = writeDb .getReference ().child (parentNodeKey );
4745
- String childReadKey = Objects .requireNonNull (parentWriteNode .push ().getKey ());
4731
+ String childNodeKey = Objects .requireNonNull (parentWriteNode .push ().getKey ());
4746
4732
DatabaseReference childReadNode =
4747
- readDb .getReference ().child (parentNodeKey ).child (childReadKey );
4748
- DatabaseReference childWriteNode = parentWriteNode .child (childReadKey );
4733
+ readDb .getReference ().child (parentNodeKey ).child (childNodeKey );
4734
+ DatabaseReference childWriteNode = parentWriteNode .child (childNodeKey );
4749
4735
ValueEventListener listener =
4750
4736
new ValueEventListener () {
4751
4737
@ Override
4752
4738
public void onDataChange (@ NonNull DataSnapshot snapshot ) {
4753
- assertEquals (val , snapshot .getValue ());
4754
4739
semaphore .release ();
4755
4740
}
4756
4741
@@ -4762,24 +4747,31 @@ public void onCancelled(@NonNull DatabaseError error) {
4762
4747
4763
4748
try {
4764
4749
await (childWriteNode .setValue (val ));
4750
+ new ReadFuture (
4751
+ childReadNode ,
4752
+ events -> {
4753
+ assertEquals (1 , events .size ());
4754
+ assertEquals (val , events .get (0 ).getSnapshot ().getValue ());
4755
+ return true ;
4756
+ })
4757
+ .timedGet ();
4765
4758
childReadNode .addValueEventListener (listener );
4766
4759
DatabaseReference parentReadNode = readDb .getReference ().child (parentNodeKey );
4767
4760
IntegrationTestHelpers .waitFor (semaphore );
4768
4761
DataSnapshot snapshot = await (parentReadNode .get ());
4769
- assertEquals (snapshot .child (childReadKey ).getValue (), val );
4770
- parentWriteNode .setValue (new MapBuilder ().put (childReadKey , val ).build ());
4762
+ assertEquals (snapshot .child (childNodeKey ).getValue (), val );
4763
+ await ( parentWriteNode .setValue (new MapBuilder ().put (childNodeKey , val ).build () ));
4771
4764
DataSnapshot parentSnapshot = await (childReadNode .get ());
4772
4765
assertEquals (parentSnapshot .getValue (), val );
4773
4766
childReadNode .removeEventListener (listener );
4774
- } catch (ExecutionException e ) {
4767
+ } catch (ExecutionException | TimeoutException | TestFailure e ) {
4775
4768
fail ("get threw an exception: " + e );
4776
4769
}
4777
4770
}
4778
4771
4779
4772
@ Test
4780
4773
public void testLimitToGetWithListenerOnlyFiresOnce ()
4781
- throws DatabaseException , InterruptedException , ExecutionException , TestFailure ,
4782
- TimeoutException {
4774
+ throws DatabaseException , InterruptedException , ExecutionException {
4783
4775
FirebaseApp readApp =
4784
4776
appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4785
4777
FirebaseApp writeApp =
@@ -4789,30 +4781,55 @@ public void testLimitToGetWithListenerOnlyFiresOnce()
4789
4781
String refKey = UUID .randomUUID ().toString ();
4790
4782
DatabaseReference writeRef = writeDb .getReference ().child (refKey );
4791
4783
DatabaseReference readRef = readDb .getReference ().child (refKey );
4792
- await (
4793
- writeRef .setValue (new MapBuilder ().put ("child1" , "test1" ).put ("child2" , "test2" ).build ()));
4794
- ReadFuture readFuture =
4795
- new ReadFuture (
4796
- readRef ,
4797
- events -> {
4798
- assertEquals (1 , events .size ());
4799
- DataSnapshot snapshot = events .get (0 ).getSnapshot ();
4800
- assertEquals (2 , snapshot .getChildrenCount ());
4801
- assertEquals ("test1" , snapshot .child ("child1" ).getValue ());
4802
- assertEquals ("test2" , snapshot .child ("child2" ).getValue ());
4803
- return true ;
4804
- });
4805
- readFuture .timedGet ();
4784
+ Map <String , Object > expected =
4785
+ new MapBuilder ().put ("child1" , "test1" ).put ("child2" , "test2" ).build ();
4786
+ try {
4787
+ await (writeRef .setValue (expected ));
4788
+ ReadFuture .untilEquals (readRef , expected ).timedGet ();
4789
+ } catch (Exception e ) {
4790
+ fail ("Test failed with exception" + e );
4791
+ }
4792
+
4806
4793
Query query = readRef .limitToFirst (1 );
4807
4794
DataSnapshot snapshot = await (query .get ());
4808
4795
Map <String , Object > map = new MapBuilder ().put ("child1" , "test1" ).build ();
4809
4796
assertEquals (map , snapshot .getValue ());
4810
4797
}
4811
4798
4799
+ @ Test
4800
+ public void testDisjointedListenAndGet () {
4801
+ FirebaseApp readApp =
4802
+ appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4803
+ FirebaseApp writeApp =
4804
+ appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4805
+ FirebaseDatabase readDb = FirebaseDatabase .getInstance (readApp );
4806
+ FirebaseDatabase writeDb = FirebaseDatabase .getInstance (writeApp );
4807
+ String parentKey = UUID .randomUUID ().toString ();
4808
+ DatabaseReference writeRef = writeDb .getReference ().child (parentKey );
4809
+ DatabaseReference child1Ref = readDb .getReference ().child (parentKey ).child ("child1" );
4810
+ Query otherChildrenQuery =
4811
+ readDb .getReference ().child (parentKey ).orderByKey ().startAt ("child2" );
4812
+ Map <String , Object > topLevelValues =
4813
+ new MapBuilder ()
4814
+ .put ("child1" , "test1" )
4815
+ .put ("child2" , "test2" )
4816
+ .put ("child3" , "test3" )
4817
+ .build ();
4818
+ try {
4819
+ await (writeRef .setValue (topLevelValues ));
4820
+ ReadFuture .untilEquals (child1Ref , "test1" );
4821
+ DataSnapshot snapshot = await (otherChildrenQuery .get ());
4822
+ assertEquals (
4823
+ new MapBuilder ().put ("child2" , "test2" ).put ("child3" , "test3" ).build (),
4824
+ snapshot .getValue ());
4825
+ } catch (Exception e ) {
4826
+ fail ("Test failed with exception" + e );
4827
+ }
4828
+ }
4829
+
4812
4830
@ Test
4813
4831
public void testGetWithLimitToListenerOnlyFiresOnce ()
4814
- throws DatabaseException , InterruptedException , ExecutionException , TestFailure ,
4815
- TimeoutException {
4832
+ throws DatabaseException , InterruptedException , ExecutionException {
4816
4833
FirebaseApp readApp =
4817
4834
appForDatabaseUrl (IntegrationTestValues .getDatabaseUrl (), UUID .randomUUID ().toString ());
4818
4835
FirebaseApp writeApp =
@@ -4823,18 +4840,14 @@ public void testGetWithLimitToListenerOnlyFiresOnce()
4823
4840
DatabaseReference writeRef = writeDb .getReference ().child (refKey );
4824
4841
DatabaseReference defaultRef = readDb .getReference ().child (refKey );
4825
4842
Query readQuery = defaultRef .limitToFirst (1 );
4826
- await (
4827
- writeRef .setValue (new MapBuilder ().put ("child1" , "test1" ).put ("child2" , "test2" ).build ()));
4828
- ReadFuture readFuture =
4829
- new ReadFuture (
4830
- readQuery ,
4831
- events -> {
4832
- assertEquals (1 , events .size ());
4833
- DataSnapshot snapshot = events .get (0 ).getSnapshot ();
4834
- assertEquals ("test1" , snapshot .child ("child1" ).getValue ());
4835
- return true ;
4836
- });
4837
- readFuture .timedGet ();
4843
+ Map <String , Object > expected =
4844
+ new MapBuilder ().put ("child1" , "test1" ).put ("child2" , "test2" ).build ();
4845
+ try {
4846
+ await (writeRef .setValue (expected ));
4847
+ ReadFuture .untilEquals (readQuery , new MapBuilder ().put ("child1" , "test1" ).build ()).timedGet ();
4848
+ } catch (Exception e ) {
4849
+ fail ("Test failed with exception" + e );
4850
+ }
4838
4851
DataSnapshot snapshot = await (defaultRef .get ());
4839
4852
Map <String , Object > map =
4840
4853
new MapBuilder ().put ("child1" , "test1" ).put ("child2" , "test2" ).build ();
@@ -4854,26 +4867,19 @@ public void testStartWithGetWithListenerOnlyFiresOnce()
4854
4867
String refKey = UUID .randomUUID ().toString ();
4855
4868
DatabaseReference writeRef = writeDb .getReference ().child (refKey );
4856
4869
DatabaseReference readRef = readDb .getReference ().child (refKey );
4857
- await (
4858
- writeRef .setValue (
4859
- new MapBuilder ()
4860
- .put ("child1" , "test1" )
4861
- .put ("child2" , "test2" )
4862
- .put ("child3" , "test3" )
4863
- .build ()));
4864
- ReadFuture readFuture =
4865
- new ReadFuture (
4866
- readRef ,
4867
- events -> {
4868
- assertEquals (1 , events .size ());
4869
- DataSnapshot snapshot = events .get (0 ).getSnapshot ();
4870
- assertEquals (3 , snapshot .getChildrenCount ());
4871
- assertEquals ("test1" , snapshot .child ("child1" ).getValue ());
4872
- assertEquals ("test2" , snapshot .child ("child2" ).getValue ());
4873
- assertEquals ("test3" , snapshot .child ("child3" ).getValue ());
4874
- return true ;
4875
- });
4876
- readFuture .timedGet ();
4870
+ Map <String , Object > expected =
4871
+ new MapBuilder ()
4872
+ .put ("child1" , "test1" )
4873
+ .put ("child2" , "test2" )
4874
+ .put ("child3" , "test3" )
4875
+ .build ();
4876
+ try {
4877
+ await (writeRef .setValue (expected ));
4878
+ ReadFuture .untilEquals (readRef , expected ).timedGet ();
4879
+ } catch (Exception e ) {
4880
+ fail ("Test failed with exception" + e );
4881
+ }
4882
+
4877
4883
Query query = readRef .orderByKey ().startAt ("child2" );
4878
4884
DataSnapshot snapshot = await (query .get ());
4879
4885
Map <String , Object > map =
0 commit comments