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

Commit aa725b7

Browse files
committed
Merge pull request #831 from couchbase/feature/issue_1108_push_many_attachments
Unit Test for push replication with too many attachments
2 parents 4ac71c0 + b80a9f3 commit aa725b7

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

src/androidTest/assets/attach.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
2+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
3+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
4+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
6+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
7+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
8+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
9+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
10+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
11+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
12+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
13+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
14+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
15+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
16+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
17+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
18+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
19+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
20+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
21+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
22+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
23+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
24+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
25+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
26+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
27+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
28+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
29+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
30+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
31+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
32+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
33+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
34+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
35+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
36+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
37+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
38+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
39+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
40+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
41+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
42+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
43+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
44+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
45+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
46+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
47+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
48+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
49+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
50+
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

src/androidTest/java/com/couchbase/lite/replicator/PushReplicationTest.java

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,70 @@
2525
*/
2626
public class PushReplicationTest extends LiteTestCaseWithDB {
2727

28+
/**
29+
* This test is too slow to run with ARM Emulator API 19 on Jenkins.
30+
* As default, test is disabled.
31+
* https://github.com/couchbase/couchbase-lite-java-core/issues/1108
32+
*/
33+
public void manualTestPushWithManyAttachment() throws Exception {
34+
// add document
35+
String docId = "doc1";
36+
String docPathRegex = String.format("/db/%s.*", docId);
37+
String docAttachName = "attachment_%d.txt";
38+
String assetAttachName = "attach.txt";
39+
String contentType = "application/octet-stream";
40+
int numAttachments = 1025;
41+
Document doc = createDocumentForPushReplication(docId, assetAttachName, docAttachName, numAttachments, contentType);
42+
43+
// create mockwebserver and custom dispatcher
44+
MockDispatcher dispatcher = new MockDispatcher();
45+
MockWebServer server = MockHelper.getMockWebServer(dispatcher);
46+
dispatcher.setServerType(MockDispatcher.ServerType.SYNC_GW);
47+
try {
48+
server.play();
49+
50+
// checkpoint GET response w/ 404 + respond to all PUT Checkpoint requests
51+
MockCheckpointPut mockCheckpointPut = new MockCheckpointPut();
52+
mockCheckpointPut.setSticky(true);
53+
mockCheckpointPut.setDelayMs(50);
54+
dispatcher.enqueueResponse(MockHelper.PATH_REGEX_CHECKPOINT, mockCheckpointPut);
55+
56+
// _revs_diff response -- everything missing
57+
MockRevsDiff mockRevsDiff = new MockRevsDiff();
58+
dispatcher.enqueueResponse(MockHelper.PATH_REGEX_REVS_DIFF, mockRevsDiff);
59+
60+
// _bulk_docs response -- everything stored
61+
MockBulkDocs mockBulkDocs = new MockBulkDocs();
62+
dispatcher.enqueueResponse(MockHelper.PATH_REGEX_BULK_DOCS, mockBulkDocs);
63+
64+
// doc PUT responses for docs with attachments
65+
MockDocumentPut mockDocPut = new MockDocumentPut().setDocId(docId).setRev(doc.getCurrentRevisionId());
66+
dispatcher.enqueueResponse(docPathRegex, mockDocPut.generateMockResponse());
67+
68+
// run replication
69+
Replication replication = database.createPushReplication(server.getUrl("/db"));
70+
replication.setContinuous(false);
71+
runReplication(replication);
72+
73+
// make assertions about outgoing requests from replicator -> mock
74+
RecordedRequest getCheckpointRequest = dispatcher.takeRequest(MockHelper.PATH_REGEX_CHECKPOINT);
75+
assertTrue(getCheckpointRequest.getMethod().equals("GET"));
76+
assertTrue(getCheckpointRequest.getPath().matches(MockHelper.PATH_REGEX_CHECKPOINT));
77+
78+
RecordedRequest revsDiffRequest = dispatcher.takeRequest(MockHelper.PATH_REGEX_REVS_DIFF);
79+
assertTrue(MockHelper.getUtf8Body(revsDiffRequest).contains(docId));
80+
81+
RecordedRequest docPutRequest = dispatcher.takeRequest(docPathRegex);
82+
assertNotNull(docPutRequest);
83+
CustomMultipartReaderDelegate delegate = new CustomMultipartReaderDelegate();
84+
MultipartReader reader = new MultipartReader(docPutRequest.getHeader("Content-Type"), delegate);
85+
reader.appendData(docPutRequest.getBody());
86+
assertTrue(delegate.json.contains(docId));
87+
} finally {
88+
server.shutdown();
89+
}
90+
}
91+
2892
/**
2993
* https://github.com/couchbase/couchbase-lite-java-core/issues/614
3094
* <p/>
@@ -77,10 +141,10 @@ public void testPushSmallDocWithAttachment() throws Exception {
77141
RecordedRequest revsDiffRequest = dispatcher.takeRequest(MockHelper.PATH_REGEX_REVS_DIFF);
78142
assertTrue(MockHelper.getUtf8Body(revsDiffRequest).contains(docId));
79143

80-
RecordedRequest docputRequest = dispatcher.takeRequest(docPathRegex);
144+
RecordedRequest docPutRequest = dispatcher.takeRequest(docPathRegex);
81145
CustomMultipartReaderDelegate delegate = new CustomMultipartReaderDelegate();
82-
MultipartReader reader = new MultipartReader(docputRequest.getHeader("Content-Type"), delegate);
83-
reader.appendData(docputRequest.getBody());
146+
MultipartReader reader = new MultipartReader(docPutRequest.getHeader("Content-Type"), delegate);
147+
reader.appendData(docPutRequest.getBody());
84148
assertTrue(delegate.json.contains(docId));
85149
byte[] attachmentBytes = MockDocumentGet.getAssetByteArray(docAttachName);
86150
assertTrue(Arrays.equals(attachmentBytes, delegate.attachment));
@@ -103,6 +167,23 @@ protected Document createDocumentForPushReplication(String docId, String attachm
103167
return document;
104168
}
105169

170+
protected Document createDocumentForPushReplication(String docId, String assetAttachmentFileName, String attachmentFileNameTemplate, int numAttachments, String attachmentContentType) throws CouchbaseLiteException {
171+
Document document = database.getDocument(docId);
172+
UnsavedRevision revision = document.createRevision();
173+
if (attachmentFileNameTemplate != null) {
174+
for(int i = 0; i < numAttachments; i++) {
175+
String attachmentFileName = String.format(attachmentFileNameTemplate, i);
176+
revision.setAttachment(
177+
attachmentFileName,
178+
attachmentContentType,
179+
getAsset(assetAttachmentFileName)
180+
);
181+
}
182+
}
183+
revision.save();
184+
return document;
185+
}
186+
106187
class CustomMultipartReaderDelegate implements MultipartReaderDelegate {
107188
public Map<String, String> headers = null;
108189
public byte[] attachment = null;

0 commit comments

Comments
 (0)