Skip to content

Commit

Permalink
C4CollectionDocObserver Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenNguyen14 committed Jun 3, 2022
1 parent 7d48ac9 commit d0c71b1
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public interface NativeImpl {

private static final NativeRefPeerBinding<C4CollectionDocObserver> BOUND_OBSERVERS = new NativeRefPeerBinding<>();


//-------------------------------------------------------------------------
// JNI callback methods
//-------------------------------------------------------------------------
Expand All @@ -38,10 +37,10 @@ static void callback(long peer, @Nullable String docID, long sequence) {

final C4CollectionDocObserver observer = BOUND_OBSERVERS.getBinding(peer);
if (observer == null) { return; }

observer.listener.run();
}


//-------------------------------------------------------------------------
// Static factory methods
//-------------------------------------------------------------------------
Expand All @@ -58,7 +57,10 @@ static C4CollectionDocObserver newObserver(
long c4Coll,
@NonNull String id,
@NonNull Runnable listener) {
final C4CollectionDocObserver observer = new C4CollectionDocObserver(impl, impl.nCreate(c4Coll, id), listener);
final C4CollectionDocObserver observer = new C4CollectionDocObserver(
impl,
impl.nCreate(c4Coll, id),
listener);
BOUND_OBSERVERS.bind(observer.getPeer(), observer);
return observer;
}
Expand All @@ -76,7 +78,11 @@ static C4CollectionDocObserver newObserver(
// Constructor
//-------------------------------------------------------------------------

private C4CollectionDocObserver(@NonNull NativeImpl impl, long collection, @NonNull Runnable listener) {

private C4CollectionDocObserver(
@NonNull NativeImpl impl,
long collection,
@NonNull Runnable listener) {
super(collection);
this.impl = impl;
this.listener = listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class C4BaseTest extends BaseTest {

protected byte[] fleeceBody;

// map docID and revIDs
private Map<String, String> ids = new HashMap<>();

@Before
public final void setUpC4BaseTest() throws CouchbaseLiteException {
final String testDirName = getUniqueName("c4_test");
Expand Down Expand Up @@ -115,6 +118,30 @@ protected void createRev(C4Database db, String docID, String revID, byte[] body)
createRev(db, docID, revID, body, 0);
}


// This method is a mock for c4Collection_putDoc. It checks whether a document is updated with a new revision id,
// if it is, we trigger observer callback
protected void createRevInCollection(
C4Collection collection,
String docID,
String revID,
byte[] body,
C4CollectionDocObserver observer)
throws LiteCoreException {
C4Document curDoc = collection.getDocument(docID);
assertNotNull(curDoc);

if (!ids.containsKey(curDoc.getDocID())) {
ids.put(curDoc.getDocID(), curDoc.getRevID());
return;
}
else if (ids.get(curDoc.getDocID()) == curDoc.getRevID()) { return; }

//if a doc is updated, trigger observer call back
C4CollectionDocObserver.callback(observer.getPeer(),curDoc.getDocID(), curDoc.getSequence());
curDoc.close();
}

protected long loadJsonAsset(String name) throws LiteCoreException, IOException {
return loadJsonAsset(name, "");
}
Expand Down Expand Up @@ -217,7 +244,16 @@ private long loadJsonAsset(InputStream is, String idPrefix, double timeout, bool
while ((l = br.readLine()) != null) {
try (FLSliceResult body = c4Database.encodeJSON(l)) {
String docID = String.format(Locale.ENGLISH, "%s%07d", idPrefix, numDocs + 1);
try (C4Document doc = c4Database.putDocument(body, docID, 0, false, false, new String[0], true, 0, 0)) {
try (C4Document doc = c4Database.putDocument(
body,
docID,
0,
false,
false,
new String[0],
true,
0,
0)) {
assertNotNull(doc);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.couchbase.lite.internal.core


import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNotNull
import org.junit.Ignore
import org.junit.Test

class C4CollectionDocObserverTest : C4BaseTest() {
private val mockCollectionDocObserver = object : C4CollectionDocObserver.NativeImpl {
override fun nCreate(coll: Long, docId: String?): Long = 0xd8597341L
override fun nFree(peer: Long) = Unit
}

// Test creating a doc observer with mock native implementation
@Test
fun testCreateC4CollectionDocObserver() {
val coll = C4Collection.create(c4Database, "_default", "_default").peer
C4CollectionDocObserver.newObserver(mockCollectionDocObserver, coll, "test") {}.use { obs ->
assertNotNull(obs)
obs.close()
}
}

// Test mock callback
@Test
fun testDocumentChanged() {
var i = 0
var obs: C4CollectionDocObserver? = null
createRev("A", "1-aa", fleeceBody)
try {
val coll = C4Collection.create(c4Database, "default", "_default").peer
obs = C4CollectionDocObserver.newObserver(mockCollectionDocObserver, coll, "A") { i++ }
assertEquals(0, i)

C4CollectionDocObserver.callback(obs.peer, "A", 0L)
C4CollectionDocObserver.callback(obs.peer, "A", 0L)
C4CollectionDocObserver.callback(obs.peer, "A", 0L)

assertEquals(3, i)

} finally {
obs?.close()
}
}

/**
* Functional Tests
*/

// This tests that a revision to a document in a database will be listened as a document revision in _default collection
@Test
@Ignore("This test should pass when we wire db methods to call collection methods")
fun testDbObserverWithCoreCallback() {
var i = 0
var obs: C4CollectionDocObserver? = null

createRev("A", "1-aa", fleeceBody)

try {
val coll = C4Collection.create(c4Database, "_default", "_default").peer
obs = C4CollectionDocObserver.newObserver(coll, "A") { i++ }

assertEquals(0, i)

createRev("A", "2-bb", fleeceBody)
createRev("B", "1-bb", fleeceBody)
assertEquals(1, i)
} finally {
obs?.close()
}
}

@Test
@Ignore("Can't create a C4Document at the moment, getFromCollection always return 0L")
fun testCollObserverWithCoreCallback() {
var i = 0
var obs: C4CollectionDocObserver? = null

try {
val coll = C4Collection.create(c4Database, "_default", "_default")
obs = C4CollectionDocObserver.newObserver(coll.peer, "A") { i++ }

createRevInCollection(coll, "A", "1-aa", fleeceBody, obs)

assertEquals(0, i)

createRevInCollection(coll, "A", "2-bb", fleeceBody, obs)
createRevInCollection(coll, "B", "1-bb", fleeceBody, obs)
assertEquals(1, i)
} finally {
obs?.close()
}
}
}

0 comments on commit d0c71b1

Please sign in to comment.