Skip to content

Commit 33308dd

Browse files
committed
more review feedback
1 parent f551977 commit 33308dd

File tree

3 files changed

+33
-56
lines changed

3 files changed

+33
-56
lines changed

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3419,15 +3419,11 @@ private static FirebaseApp appForDatabaseUrl(String url, String name) {
34193419
}
34203420

34213421
@Test
3422-
public void emptyQueryGet() throws DatabaseException, InterruptedException {
3422+
public void emptyQueryGet() throws DatabaseException, InterruptedException, ExecutionException {
34233423
FirebaseApp app =
34243424
appForDatabaseUrl(IntegrationTestValues.getNamespace(), UUID.randomUUID().toString());
34253425
FirebaseDatabase db = FirebaseDatabase.getInstance(app);
3426-
try {
3427-
assertNull(Tasks.await(db.getReference(UUID.randomUUID().toString()).get()).getValue());
3428-
} catch (ExecutionException e) {
3429-
fail(e.getMessage());
3430-
}
3426+
assertNull(Tasks.await(db.getReference(UUID.randomUUID().toString()).get()).getValue());
34313427
}
34323428

34333429
@Test
@@ -3524,11 +3520,7 @@ public void onCancelled(@NonNull DatabaseError error) {}
35243520
write = new WriteFuture(writer, 43L);
35253521
assertNull(write.timedGet());
35263522

3527-
try {
3528-
assertEquals(43L, Tasks.await(reader.get()).getValue());
3529-
} catch (ExecutionException e) {
3530-
fail(e.getMessage());
3531-
}
3523+
assertEquals(43L, Tasks.await(reader.get()).getValue());
35323524
}
35333525

35343526
@Test
@@ -3551,19 +3543,7 @@ public void getUpdatesPersistenceCacheWhenEnabled()
35513543
readerDb.goOffline();
35523544

35533545
Semaphore semaphore = new Semaphore(0);
3554-
reader.addListenerForSingleValueEvent(
3555-
new ValueEventListener() {
3556-
@Override
3557-
public void onDataChange(@NonNull DataSnapshot snapshot) {
3558-
if (snapshot.getValue() != null && snapshot.getValue().equals(42L)) {
3559-
semaphore.release();
3560-
}
3561-
}
3562-
3563-
@Override
3564-
public void onCancelled(@NonNull DatabaseError error) {}
3565-
});
3566-
IntegrationTestHelpers.waitFor(semaphore);
3546+
assertNotNull(ReadFuture.untilEquals(reader, 42L).timedGet());
35673547
}
35683548

35693549
@Test

firebase-database/src/main/java/com/google/firebase/database/connection/PersistentConnectionImpl.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.concurrent.ScheduledExecutorService;
3434
import java.util.concurrent.ScheduledFuture;
3535
import java.util.concurrent.TimeUnit;
36-
import java.util.concurrent.atomic.AtomicBoolean;
3736

3837
public class PersistentConnectionImpl implements Connection.Delegate, PersistentConnection {
3938

@@ -114,17 +113,17 @@ public String toString() {
114113
}
115114

116115
private static class OutstandingGet {
117-
private Map<String, Object> request;
118-
private ConnectionRequestCallback onComplete;
119-
private String action;
120-
private AtomicBoolean sent;
116+
private final Map<String, Object> request;
117+
private final ConnectionRequestCallback onComplete;
118+
private final String action;
119+
private boolean sent;
121120

122121
private OutstandingGet(
123122
String action, Map<String, Object> request, ConnectionRequestCallback onComplete) {
124123
this.action = action;
125124
this.request = request;
126125
this.onComplete = onComplete;
127-
this.sent = new AtomicBoolean(false);
126+
this.sent = false;
128127
}
129128

130129
private String getAction() {
@@ -140,11 +139,12 @@ private Map<String, Object> getRequest() {
140139
}
141140

142141
private boolean markSent() {
143-
return sent.compareAndSet(false, true);
144-
}
145-
146-
private boolean wasSent() {
147-
return sent.get();
142+
boolean prev = sent;
143+
if (prev) {
144+
return false;
145+
}
146+
this.sent = true;
147+
return true;
148148
}
149149
}
150150

@@ -399,8 +399,7 @@ public Task<Object> get(List<String> path, Map<String, Object> queryParams) {
399399
request.put(REQUEST_PATH, ConnectionUtils.pathToString(query.path));
400400
request.put(REQUEST_QUERIES, query.queryParams);
401401

402-
outstandingGets.put(
403-
readId,
402+
OutstandingGet outstandingGet =
404403
new OutstandingGet(
405404
REQUEST_ACTION_GET,
406405
request,
@@ -417,15 +416,16 @@ public void onResponse(Map<String, Object> response) {
417416
new Exception((String) response.get(SERVER_DATA_UPDATE_BODY)));
418417
}
419418
}
420-
}));
419+
});
420+
outstandingGets.put(readId, outstandingGet);
421421

422422
if (!connected()) {
423423
executorService.schedule(
424424
new Runnable() {
425425
@Override
426426
public void run() {
427427
OutstandingGet get = outstandingGets.get(readId);
428-
if (get == null || !get.markSent()) {
428+
if (get == null || get != outstandingGet || !get.markSent()) {
429429
return;
430430
}
431431
if (logger.logsDebug()) {
@@ -1067,13 +1067,6 @@ private void restoreState() {
10671067
sendPut(put);
10681068
}
10691069

1070-
if (logger.logsDebug()) logger.debug("Restoring reads.");
1071-
ArrayList<Long> outstandingGetKeys = new ArrayList<Long>(outstandingGets.keySet());
1072-
Collections.sort(outstandingGetKeys);
1073-
for (Long getId : outstandingGetKeys) {
1074-
sendGet(getId);
1075-
}
1076-
10771070
// Restore disconnect operations
10781071
for (OutstandingDisconnect disconnect : onDisconnectRequestQueue) {
10791072
sendOnDisconnect(
@@ -1083,6 +1076,13 @@ private void restoreState() {
10831076
disconnect.getOnComplete());
10841077
}
10851078
onDisconnectRequestQueue.clear();
1079+
1080+
if (logger.logsDebug()) logger.debug("Restoring reads.");
1081+
ArrayList<Long> outstandingGetKeys = new ArrayList<Long>(outstandingGets.keySet());
1082+
Collections.sort(outstandingGetKeys);
1083+
for (Long getId : outstandingGetKeys) {
1084+
sendGet(getId);
1085+
}
10861086
}
10871087

10881088
private void handleTimestamp(long timestamp) {
@@ -1167,7 +1167,7 @@ private void sendGet(final Long readId) {
11671167
OutstandingGet get = outstandingGets.get(readId);
11681168
if (!get.markSent()) {
11691169
if (logger.logsDebug()) {
1170-
logger.debug("get" + readId + " already sent or cancelled, ignoring.");
1170+
logger.debug("get" + readId + " cancelled, ignoring.");
11711171
return;
11721172
}
11731173
}
@@ -1181,10 +1181,9 @@ public void onResponse(Map<String, Object> response) {
11811181
if (currentGet == get) {
11821182
outstandingGets.remove(readId);
11831183
get.getOnComplete().onResponse(response);
1184-
} else {
1185-
if (logger.logsDebug())
1186-
logger.debug(
1187-
"Ignoring on complete for get " + readId + " because it was removed already.");
1184+
} else if (logger.logsDebug()) {
1185+
logger.debug(
1186+
"Ignoring on complete for get " + readId + " because it was removed already.");
11881187
}
11891188
}
11901189
});

firebase-database/src/main/java/com/google/firebase/database/core/Repo.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static com.google.firebase.database.core.utilities.Utilities.hardAssert;
1818

1919
import androidx.annotation.NonNull;
20-
import com.google.android.gms.tasks.Continuation;
2120
import com.google.android.gms.tasks.OnCompleteListener;
2221
import com.google.android.gms.tasks.Task;
2322
import com.google.android.gms.tasks.TaskCompletionSource;
@@ -513,12 +512,11 @@ public void onComplete(@NonNull Task<Object> task) {
513512
});
514513
return source
515514
.getTask()
516-
.continueWithTask(
517-
new Continuation<DataSnapshot, Task<DataSnapshot>>() {
515+
.addOnCompleteListener(
516+
new OnCompleteListener<DataSnapshot>() {
518517
@Override
519-
public Task<DataSnapshot> then(@NonNull Task<DataSnapshot> task) throws Exception {
518+
public void onComplete(@NonNull Task<DataSnapshot> task) {
520519
serverSyncTree.setQueryInactive(query.getSpec());
521-
return task;
522520
}
523521
});
524522
}

0 commit comments

Comments
 (0)