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

Commit fc6cc47

Browse files
committed
Merge pull request #793 from couchbase/feature/issue_1047_push_thread
Fixed #1047 - Push replication skipped 20 documents
2 parents 075bde8 + 7061d48 commit fc6cc47

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

src/androidTest/assets/test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# values in that file will override these
44
# and it will be ignored by git automatically
55
replicationUrl=http://10.0.2.2:4984/db
6+
adminReplicationUrl=http://10.0.2.2:4985/db
67
syncgatewayTestsEnabled=false
78

89
# SQLite library options: 0:default, 1:custom, 2:sqlcipher

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ protected URL getReplicationURL() throws MalformedURLException {
268268
return new URL(System.getProperty("replicationUrl"));
269269
}
270270

271+
protected URL getAdminReplicationURL() throws MalformedURLException {
272+
return new URL(System.getProperty("adminReplicationUrl"));
273+
}
274+
271275
protected boolean isTestingAgainstSyncGateway() {
272276
try {
273277
URL url = getReplicationURL();
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.couchbase.lite.syncgateway;
2+
3+
4+
import com.couchbase.lite.CouchbaseLiteException;
5+
import com.couchbase.lite.Document;
6+
import com.couchbase.lite.LiteTestCaseWithDB;
7+
import com.couchbase.lite.Manager;
8+
import com.couchbase.lite.replicator.Replication;
9+
import com.couchbase.lite.util.Log;
10+
11+
import java.io.IOException;
12+
import java.io.InputStream;
13+
import java.net.HttpURLConnection;
14+
import java.net.URL;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import java.util.concurrent.CountDownLatch;
18+
19+
/**
20+
* Created by hideki on 2/25/16.
21+
*/
22+
public class PushReplTest extends LiteTestCaseWithDB {
23+
24+
public static final String TAG = "PushReplTest";
25+
26+
@Override
27+
protected void setUp() throws Exception {
28+
if (!syncgatewayTestsEnabled()) {
29+
return;
30+
}
31+
32+
super.setUp();
33+
}
34+
35+
/**
36+
* Note: For test, needs to restart sync gateway. Default sync gateway does not allow
37+
* to access admin port from non-local to delete db.
38+
*/
39+
public void testPushRepl() throws Exception{
40+
if (!syncgatewayTestsEnabled()) {
41+
return;
42+
}
43+
44+
final int INIT_DOCS = 100;
45+
final int TOTAL_DOCS = 400;
46+
47+
// initial 100 docs
48+
for (int i = 0; i < INIT_DOCS; i++) {
49+
Document doc = database.getDocument("doc-" + String.format("%03d", i));
50+
Map<String, Object> props = new HashMap<>();
51+
props.put("key", i);
52+
try {
53+
doc.putProperties(props);
54+
} catch (CouchbaseLiteException e) {
55+
Log.e(TAG, "Failed to create new doc", e);
56+
fail(e.getMessage());
57+
}
58+
}
59+
60+
final CountDownLatch latch = new CountDownLatch(1);
61+
62+
// thread creates documents during replication.
63+
Thread thread = new Thread(new Runnable() {
64+
@Override
65+
public void run() {
66+
for (int i = INIT_DOCS; i < TOTAL_DOCS; i++) {
67+
Document doc = database.getDocument("doc-" + String.format("%03d", i));
68+
Map<String, Object> props = new HashMap<>();
69+
props.put("key", i);
70+
try {
71+
doc.putProperties(props);
72+
} catch (CouchbaseLiteException e) {
73+
Log.e(TAG, "Failed to create new doc", e);
74+
fail(e.getMessage());
75+
76+
}
77+
}
78+
latch.countDown();
79+
}
80+
});
81+
thread.start();
82+
83+
// start push replicator
84+
final CountDownLatch idle = new CountDownLatch(1);
85+
final CountDownLatch stop = new CountDownLatch(1);
86+
ReplicationIdleObserver idleObserver = new ReplicationIdleObserver(idle);
87+
ReplicationFinishedObserver stopObserver = new ReplicationFinishedObserver(stop);
88+
Replication push = database.createPushReplication(getReplicationURL());
89+
push.setContinuous(true);
90+
push.addChangeListener(idleObserver);
91+
push.addChangeListener(stopObserver);
92+
push.start();
93+
// wait till thread finishes to create all docs.
94+
latch.await();
95+
// wait till push become idle state
96+
idle.await();
97+
// stop push
98+
push.stop();
99+
// stop till push become stopped state
100+
stop.await();
101+
102+
// give sync gateway to process all.
103+
Thread.sleep(2*1000);
104+
105+
// verify total document number on remote
106+
assertEquals(TOTAL_DOCS, ((Number) getAllDocs().get("total_rows")).intValue());
107+
}
108+
109+
// GET /{db}/_all_docs
110+
Map<String, Object> getAllDocs() throws IOException {
111+
HttpURLConnection connection = null;
112+
try {
113+
URL url = new URL(System.getProperty("replicationUrl") + "/_all_docs");
114+
connection = (HttpURLConnection) url.openConnection();
115+
connection.setRequestMethod("GET");
116+
InputStream in = connection.getInputStream();
117+
try {
118+
return Manager.getObjectMapper().readValue(in, Map.class);
119+
} finally {
120+
if (in != null)
121+
in.close();
122+
}
123+
} finally {
124+
if (connection != null)
125+
connection.disconnect();
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)