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

Commit 5c92cb8

Browse files
author
hideki
committed
Add test case for multiple change trackers
1 parent 244e6f1 commit 5c92cb8

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.couchbase.lite.replicator;
2+
3+
import com.couchbase.lite.LiteTestCaseWithDB;
4+
import com.couchbase.lite.mockserver.MockChangesFeedNoResponse;
5+
import com.couchbase.lite.mockserver.MockCheckpointPut;
6+
import com.couchbase.lite.mockserver.MockDispatcher;
7+
import com.couchbase.lite.mockserver.MockHelper;
8+
import com.couchbase.lite.util.Log;
9+
import com.squareup.okhttp.mockwebserver.MockWebServer;
10+
11+
import java.util.Set;
12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.TimeUnit;
14+
15+
public class ManualReplicationTest extends LiteTestCaseWithDB {
16+
17+
// https://github.com/couchbase/couchbase-lite-java-core/issues/1116
18+
// Multiple ChangeTrackers are started (Scenario 3)
19+
//
20+
// This test is not solid test. I set this as manual test. Not run on Jenkins.
21+
public void manualTestMultipleChangeTrackerScenario3() throws Exception {
22+
Log.d(Log.TAG, "testMultipleChangeTrackerScenario3");
23+
24+
Replication pullReplication = null;
25+
// create mock server
26+
MockWebServer server = new MockWebServer();
27+
try {
28+
MockDispatcher dispatcher = new MockDispatcher();
29+
dispatcher.setServerType(MockDispatcher.ServerType.SYNC_GW);
30+
server.setDispatcher(dispatcher);
31+
server.play();
32+
33+
// checkpoint PUT or GET response (sticky)
34+
MockCheckpointPut mockCheckpointPut = new MockCheckpointPut();
35+
mockCheckpointPut.setSticky(true);
36+
mockCheckpointPut.setDelayMs(10 * 1000); // 10sec
37+
dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT, mockCheckpointPut);
38+
39+
// long poll changes feed no response balock 5 min
40+
MockChangesFeedNoResponse mockChangesFeedNoResponse = new MockChangesFeedNoResponse();
41+
mockChangesFeedNoResponse.setSticky(true);
42+
mockChangesFeedNoResponse.setDelayMs(300 * 1000); // 60 sec
43+
dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHANGES, mockChangesFeedNoResponse);
44+
45+
// create and start replication
46+
pullReplication = database.createPullReplication(server.getUrl("/db"));
47+
pullReplication.setContinuous(true);
48+
pullReplication.start();
49+
Log.d(Log.TAG, "Started pullReplication: %s", pullReplication);
50+
51+
// offline
52+
putReplicationOffline(pullReplication);
53+
54+
// online
55+
putReplicationOnline(pullReplication);
56+
57+
// wait
58+
try {
59+
Thread.sleep(30 * 1000);
60+
} catch (Exception e) {
61+
}
62+
63+
// all threads which are associated with replicators should be terminated.
64+
int numChangeTracker = 0;
65+
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
66+
for (Thread t : threadSet) {
67+
if (t.isAlive()) {
68+
if (t.getName().indexOf("ChangeTracker-") != -1) {
69+
numChangeTracker++;
70+
}
71+
}
72+
}
73+
assertEquals(1, numChangeTracker);
74+
75+
} finally {
76+
stopReplication(pullReplication);
77+
server.shutdown();
78+
}
79+
}
80+
81+
private void putReplicationOffline(Replication replication) throws InterruptedException {
82+
Log.d(Log.TAG, "putReplicationOffline: %s", replication);
83+
84+
// this was a useless test, the replication wasn't even started
85+
final CountDownLatch wentOffline = new CountDownLatch(1);
86+
Replication.ChangeListener changeListener = new ReplicationOfflineObserver(wentOffline);
87+
replication.addChangeListener(changeListener);
88+
89+
replication.goOffline();
90+
boolean succeeded = wentOffline.await(30, TimeUnit.SECONDS);
91+
assertTrue(succeeded);
92+
93+
replication.removeChangeListener(changeListener);
94+
95+
Log.d(Log.TAG, "/putReplicationOffline: %s", replication);
96+
}
97+
98+
private void putReplicationOnline(Replication replication) throws InterruptedException {
99+
100+
Log.d(Log.TAG, "putReplicationOnline: %s", replication);
101+
102+
// this was a useless test, the replication wasn't even started
103+
final CountDownLatch wentOnline = new CountDownLatch(1);
104+
Replication.ChangeListener changeListener = new ReplicationActiveObserver(wentOnline);
105+
replication.addChangeListener(changeListener);
106+
107+
replication.goOnline();
108+
109+
boolean succeeded = wentOnline.await(30, TimeUnit.SECONDS);
110+
assertTrue(succeeded);
111+
112+
replication.removeChangeListener(changeListener);
113+
114+
Log.d(Log.TAG, "/putReplicationOnline: %s", replication);
115+
}
116+
}

0 commit comments

Comments
 (0)