Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save schmidt-sebastian/46e02b4662e8c8c80ca6c260a5317970 to your computer and use it in GitHub Desktop.
Save schmidt-sebastian/46e02b4662e8c8c80ca6c260a5317970 to your computer and use it in GitHub Desktop.
@Test
public void testTransactionIsCanceledAfterServerUpdate()
throws InterruptedException, ExecutionException {
// This test reproduces https://github.com/firebase/firebase-admin-java/issues/178:
//
// If we run two transactions (one at "path/nested1" and one at "path"), the SDK did not
// correctly re-run the "path" transaction if it got an update from the server for
// "path/nested2". This caused the "path" transaction to succeed on the server
// (our hash included the update from the server), merging stale data.
final DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ref.getDatabase().goOffline();
// Write data to the backend from a secondary instance. The primary instance receives this data
// when it goes online during the first transaction.
FirebaseApp secondaryApp = IntegrationTestUtils.initApp("secondaryApp");
DatabaseReference initialData =
FirebaseDatabase.getInstance(secondaryApp).getReference(ref.getKey());
initialData.setValueAsync(new MapBuilder().put("a", "a").build()).get();
secondaryApp.delete();
final Semaphore semaphore = new Semaphore(0);
ref.child("b").runTransaction(new Handler() {
@Override
public Result doTransaction(MutableData currentData) {
ref.getDatabase().goOnline();
currentData.setValue("b");
return Transaction.success(currentData);
}
@Override
public void onComplete(DatabaseError error, boolean committed, DataSnapshot currentData) {
Assert.assertTrue(committed);
semaphore.release();
}
});
ref.runTransaction(new Handler() {
@Override
public Result doTransaction(MutableData currentData) {
currentData.child("c").setValue("c");
return Transaction.success(currentData);
}
@Override
public void onComplete(DatabaseError error, boolean committed, DataSnapshot currentData) {
Assert.assertTrue(committed);
semaphore.release();
}
});
TestHelpers.waitFor(semaphore, 2);
DataSnapshot dataSnapshot = TestHelpers.getSnap(ref);
assertEquals(3, dataSnapshot.getChildrenCount());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment