Skip to content

Commit 23bde0b

Browse files
committed
Fixed tests
1 parent d9a97f2 commit 23bde0b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

firebase-database/src/androidTest/java/com/google/firebase/database/QueryTest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4594,6 +4594,7 @@ public void testGetResolvesToPersistentCacheWhenOfflineAndNoListeners()
45944594
db.setPersistenceEnabled(true);
45954595
DatabaseReference node = db.getReference().push();
45964596
long val = 34;
4597+
Thread.currentThread().getId();
45974598

45984599
try {
45994600
await(node.setValue(val));
@@ -4602,6 +4603,7 @@ public void testGetResolvesToPersistentCacheWhenOfflineAndNoListeners()
46024603
DataSnapshot snapshot =
46034604
await(node.get()); // This does not time out. We need to get it to do so.
46044605
assertEquals(val, snapshot.getValue());
4606+
db.goOnline();
46054607
} catch (ExecutionException e) {
46064608
fail("get threw an exception: " + e);
46074609
}
@@ -4615,38 +4617,39 @@ public void testGetResolvesToCacheWhenOnlineAndParentListener()
46154617
// To ensure that we don't read the cached results, we need a separate app.
46164618
FirebaseApp readApp =
46174619
appForDatabaseUrl(IntegrationTestValues.getDatabaseUrl(), UUID.randomUUID().toString());
4620+
// String topLevelKey = UUID.randomUUID().toString();
46184621
FirebaseDatabase writeDb = FirebaseDatabase.getInstance(writeApp);
46194622
FirebaseDatabase readDb = FirebaseDatabase.getInstance(readApp);
46204623
long val = 34;
4621-
DatabaseReference node = writeDb.getReference().push();
4624+
DatabaseReference writeNode = writeDb.getReference().push();
4625+
String writeKey = writeNode.getKey();
4626+
DatabaseReference readNode = readDb.getReference().child(writeKey);
46224627

46234628
try {
4624-
await(node.setValue(val));
4629+
await(writeNode.setValue(val));
46254630
DatabaseReference topLevelNode = readDb.getReference();
4626-
new ReadFuture(
4631+
ReadFuture readFuture =
4632+
new ReadFuture(
46274633
topLevelNode,
46284634
events -> {
46294635
assertEquals(1, events.size());
4630-
DataSnapshot childNode = events.get(0).getSnapshot().child(node.getKey());
4636+
DataSnapshot childNode = events.get(0).getSnapshot().child(writeKey);
46314637
assertEquals(34L, childNode.getValue());
4632-
DataSnapshot snapshot = null;
4633-
try {
4634-
snapshot = await(node.get());
4635-
} catch (ExecutionException | InterruptedException e) {
4636-
e.printStackTrace();
4637-
}
4638-
assertEquals(val, Objects.requireNonNull(snapshot.getValue()));
4638+
46394639
return true;
4640-
})
4641-
.timedGet();
4640+
});
4641+
readFuture.timedGet();
4642+
DataSnapshot snapshot = null;
4643+
snapshot = await(readNode.get());
4644+
assertEquals(val, Objects.requireNonNull(snapshot.getValue()));
46424645
} catch (TestFailure | TimeoutException | ExecutionException e) {
46434646
e.printStackTrace();
46444647
}
46454648
}
46464649

46474650
@Test
46484651
public void testGetResolvesToCacheWhenOnlineAndSameLevelListener()
4649-
throws DatabaseException, InterruptedException, ExecutionException {
4652+
throws DatabaseException, InterruptedException, ExecutionException {
46504653
FirebaseApp writeApp =
46514654
appForDatabaseUrl(IntegrationTestValues.getDatabaseUrl(), UUID.randomUUID().toString());
46524655
// To ensure that we don't read the cached results, we need a separate app.
@@ -4720,7 +4723,7 @@ public void onCancelled(@NonNull DatabaseError error) {
47204723
db.goOffline();
47214724
DataSnapshot snapshot = await(node.get());
47224725
assertEquals(val, snapshot.getValue());
4723-
// need to rewrite it
4726+
db.goOnline();
47244727
} catch (ExecutionException e) {
47254728
fail("get threw an exception: " + e);
47264729
}

firebase-database/src/androidTest/java/com/google/firebase/database/future/ReadFuture.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
public class ReadFuture implements Future<List<EventRecord>> {
3535

3636
public interface CompletionCondition {
37-
public boolean isComplete(List<EventRecord> events);
37+
public boolean isComplete(List<EventRecord> events)
38+
throws ExecutionException, InterruptedException;
3839
}
3940

4041
private List<EventRecord> events = new ArrayList<EventRecord>();
@@ -66,13 +67,15 @@ public void onDataChange(DataSnapshot snapshot) {
6667
}
6768
} catch (Exception e) {
6869
exception = e;
70+
ref.removeEventListener(valueEventListener);
6971
finish();
7072
}
7173
}
7274

7375
@Override
7476
public void onCancelled(DatabaseError error) {
7577
wasCancelled = true;
78+
ref.removeEventListener(valueEventListener);
7679
finish();
7780
}
7881
};

0 commit comments

Comments
 (0)