Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 16bf09e

Browse files
committed
Merge pull request #600 from couchbase/feature/issue_560_multiple_updates_in_transaction
Fixed #563 multiple updates in transaction
2 parents 6052511 + a08c08b commit 16bf09e

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

src/androidTest/java/com/couchbase/lite/DocumentTest.java

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testPutDeletedDocument() throws CouchbaseLiteException {
4848
properties.put("mykey", "myval");
4949
SavedRevision newRev = document.putProperties(properties);
5050
newRev.loadProperties();
51-
assertTrue( newRev.getProperties().containsKey("mykey") );
51+
assertTrue(newRev.getProperties().containsKey("mykey"));
5252

5353
Assert.assertTrue(document.isDeleted());
5454
Document fetchedDoc = database.getExistingDocument(docId);
@@ -458,7 +458,7 @@ public void disabledTestNonsensicalConflictExceptionOnUnsavedRevision() throws C
458458
testNonsensicalConflict.setMap(new Mapper() {
459459
@Override
460460
public void map(Map<String, Object> document, Emitter emitter) {
461-
emitter.emit(null,null);
461+
emitter.emit(null, null);
462462
}
463463
}, "");
464464
Query query = testNonsensicalConflict.createQuery();
@@ -468,7 +468,7 @@ public void map(Map<String, Object> document, Emitter emitter) {
468468
@Override
469469
public void changed(LiveQuery.ChangeEvent event) {
470470
QueryEnumerator rows = event.getRows();
471-
while(rows.hasNext()){
471+
while (rows.hasNext()) {
472472
QueryRow next = rows.next();
473473
next.getDocument();
474474
}
@@ -527,4 +527,94 @@ public void changed(Document.ChangeEvent event) {
527527
boolean success = documentChanged.await(30, TimeUnit.SECONDS);
528528
assertTrue(success);
529529
}
530+
531+
/**
532+
* https://github.com/couchbase/couchbase-lite-android/issues/563
533+
* Updating a document in a transaction block twice using Document.DocumentUpdater results in an infinite loop
534+
* <p/>
535+
* NOTE: Use Document.update()
536+
*/
537+
public void testMultipleUpdatesInTransactionWithUpdate() throws CouchbaseLiteException {
538+
Log.e(TAG, "testMultipleUpdatesInTransactionWithUpdate() START");
539+
final Document doc = database.createDocument();
540+
HashMap<String, Object> properties = new HashMap<String, Object>();
541+
properties.put("key", "value1");
542+
doc.putProperties(properties);
543+
database.runInTransaction(
544+
new TransactionalTask() {
545+
@Override
546+
public boolean run() {
547+
try {
548+
doc.update(new Document.DocumentUpdater() {
549+
@Override
550+
public boolean update(UnsavedRevision newRevision) {
551+
Log.e(TAG, "Trying to update key to value 2");
552+
Map<String, Object> properties = newRevision.getUserProperties();
553+
properties.put("key", "value2");
554+
newRevision.setUserProperties(properties);
555+
return true;
556+
}
557+
});
558+
doc.update(new Document.DocumentUpdater() {
559+
@Override
560+
public boolean update(UnsavedRevision newRevision) {
561+
Log.e(TAG, "Trying to update key to value 3");
562+
Map<String, Object> properties = newRevision.getUserProperties();
563+
properties.put("key", "value3");
564+
newRevision.setUserProperties(properties);
565+
return true;
566+
}
567+
});
568+
} catch (CouchbaseLiteException e) {
569+
Log.e(TAG, "Trying to update key to value 2 or 3");
570+
fail("Trying to update key to value 2 or 3, but failed.");
571+
return false;
572+
}
573+
return true;
574+
}
575+
});
576+
Map<String, Object> properties4 = doc.getProperties();
577+
Log.e(TAG, "properties4 = " + properties4);
578+
Log.e(TAG, "testMultipleUpdatesInTransactionWithUpdate() END");
579+
}
580+
581+
/**
582+
* NOTE: This is variation of testMultipleUpdatesInTransactionWithUpdate() test with using Document.putProperties()
583+
*/
584+
public void testMultipleUpdatesInTransactionWithPutProperties() throws CouchbaseLiteException {
585+
Log.e(TAG, "testMultipleUpdatesInTransactionWithPutProperties() START");
586+
final Document doc = database.createDocument();
587+
HashMap<String, Object> properties1 = new HashMap<String, Object>();
588+
properties1.put("key", "value1");
589+
doc.putProperties(properties1);
590+
database.runInTransaction(
591+
new TransactionalTask() {
592+
@Override
593+
public boolean run() {
594+
try {
595+
Map<String, Object> properties2 = new HashMap<String, Object>(doc.getProperties());
596+
properties2.put("key", "value2");
597+
doc.putProperties(properties2);
598+
} catch (CouchbaseLiteException e) {
599+
Log.e(TAG, "Trying to update key to value 2");
600+
fail("Trying to update key to value 2, but failed.");
601+
return false;
602+
}
603+
try {
604+
Map<String, Object> properties3 = new HashMap<String, Object>(doc.getProperties());
605+
properties3.put("key", "value3");
606+
doc.putProperties(properties3);
607+
} catch (CouchbaseLiteException e) {
608+
Log.e(TAG, "Trying to update key to value 3");
609+
fail("Trying to update key to value 3, but failed.");
610+
return false;
611+
}
612+
return true;
613+
}
614+
});
615+
Map<String, Object> properties4 = doc.getProperties();
616+
Log.e(TAG, "properties4 = " + properties4);
617+
assertEquals("value3", properties4.get("key"));
618+
Log.e(TAG, "testMultipleUpdatesInTransactionWithPutProperties() END");
619+
}
530620
}

0 commit comments

Comments
 (0)