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

Commit

Permalink
test: replicator change listener (#1870)
Browse files Browse the repository at this point in the history
* check for change listener is being returned when added to an offline replicator.
* check for network retry.
  • Loading branch information
jayahariv committed Feb 8, 2019
1 parent 597ffbf commit 37c8790
Showing 1 changed file with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.couchbase.lite;

import android.support.annotation.NonNull;

import org.junit.Test;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class ReplicatorOfflineTest extends BaseReplicatorTest {
Expand Down Expand Up @@ -78,6 +80,28 @@ public void changed(ReplicatorChange change) {
repl.removeChangeListener(token);
}

@Test
public void testDocumentChangeListenerToken() throws Exception {
Endpoint endpoint = getRemoteTargetEndpoint();
Replicator repl = new Replicator(makeConfig(
true,
false,
false,
endpoint
));
ListenerToken token = repl.addDocumentReplicationListener(new DocumentReplicationListener() {
@Override
public void replication(@NonNull DocumentReplication replication) { }
});
assertNotNull(token);

thrown.expect(IllegalArgumentException.class);
repl.addDocumentReplicationListener(null);

thrown.expect(IllegalArgumentException.class);
repl.addDocumentReplicationListener(executor, null);
}

@Test
public void testChangeListenerEmptyArg() throws Exception {
Endpoint endpoint = getRemoteTargetEndpoint();
Expand All @@ -95,6 +119,36 @@ public void testChangeListenerEmptyArg() throws Exception {
repl.addChangeListener(executor, null);
}

@Test
public void testNetworkRetry() throws URISyntaxException, InterruptedException {
Endpoint target = getRemoteTargetEndpoint();
ReplicatorConfiguration config = makeConfig(false, true, true, db, target);
Replicator repl = new Replicator(config);
final CountDownLatch offline = new CountDownLatch(2);
final CountDownLatch stopped = new CountDownLatch(1);
ListenerToken token = repl.addChangeListener(executor, new ReplicatorChangeListener() {
@Override
public void changed(ReplicatorChange change) {
Replicator.Status status = change.getStatus();
if (status.getActivityLevel() == Replicator.ActivityLevel.OFFLINE) {
offline.countDown();
if (offline.getCount() == 0) {
change.getReplicator().stop();
} else {
change.getReplicator().networkReachable();
}
}
if (status.getActivityLevel() == Replicator.ActivityLevel.STOPPED) {
stopped.countDown();
}
}
});
repl.start();
assertTrue(offline.await(10, TimeUnit.SECONDS));
assertTrue(stopped.await(10, TimeUnit.SECONDS));
repl.removeChangeListener(token);
}

private URLEndpoint getRemoteTargetEndpoint() throws URISyntaxException {
return new URLEndpoint(new URI("ws://foo.couchbase.com/db"));
}
Expand Down

0 comments on commit 37c8790

Please sign in to comment.