|
1 | 1 | /* |
2 | | - * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 2 | + * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
3 | 3 | */ |
4 | 4 | package com.marklogic.client.test.dataservices; |
5 | 5 |
|
|
12 | 12 | import com.marklogic.client.io.InputStreamHandle; |
13 | 13 | import com.marklogic.client.io.JacksonHandle; |
14 | 14 | import com.marklogic.client.io.StringHandle; |
| 15 | +import com.marklogic.client.test.AbstractClientTest; |
15 | 16 | import org.junit.jupiter.api.AfterAll; |
16 | 17 | import org.junit.jupiter.api.BeforeAll; |
17 | | -import org.junit.jupiter.api.Disabled; |
18 | 18 | import org.junit.jupiter.api.Test; |
19 | 19 | import static org.junit.jupiter.api.Assertions.*; |
20 | 20 |
|
21 | 21 | import java.io.InputStream; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collections; |
22 | 24 | import java.util.HashMap; |
| 25 | +import java.util.List; |
23 | 26 | import java.util.Map; |
24 | 27 | import java.util.Set; |
| 28 | +import java.util.TreeSet; |
25 | 29 | import java.util.stream.Stream; |
26 | 30 |
|
27 | | -public class BulkIOInputCallerTest { |
| 31 | +public class BulkIOInputCallerTest extends AbstractClientTest { |
28 | 32 |
|
29 | 33 | static ObjectNode[] apiObj = new ObjectNode[2]; |
30 | 34 | static String[] apiNames = new String[]{"bulkIOInputCaller.api", "bulkIOAnyDocumentInputCaller.api"}; |
@@ -84,58 +88,52 @@ public void bulkInputEndpointTestWithMultipleCallContexts() { |
84 | 88 | map.get("bulkInputTest_2") >= 1); |
85 | 89 | } |
86 | 90 |
|
87 | | - @Disabled("Fails intermittently on Jenkins, never locally. Will revisit if we make enhancements to Bulk DS.") |
88 | 91 | @Test |
89 | 92 | public void bulkInputEndpointTestWithAnyDocument() { |
90 | | - |
91 | | - String endpointState = "{\"next\":"+1+"}"; |
92 | | - String endpointConstants = "{\"max\":6,\"collection\":\"bulkInputTest_1\"}"; |
93 | | - |
94 | | - String endpointState1 = "{\"next\":"+1+"}"; |
95 | | - String endpointConstants1 = "{\"max\":6,\"collection\":\"bulkInputTest_2\"}"; |
| 93 | + final String collection1 = "bulkInputAnyDocTest_1"; |
| 94 | + final String collection2 = "bulkInputAnyDocTest_2"; |
| 95 | + final String endpointState = "{\"next\":1}"; |
96 | 96 |
|
97 | 97 | InputCaller<StringHandle> loadEndpt = InputCaller.onHandles(IOTestUtil.db, new JacksonHandle(apiObj[1]), new StringHandle()); |
98 | | - IOEndpoint.CallContext[] callContextArray = {loadEndpt.newCallContext() |
| 98 | + IOEndpoint.CallContext[] callContextArray = { |
| 99 | + loadEndpt.newCallContext() |
99 | 100 | .withEndpointStateAs(endpointState) |
100 | | - .withEndpointConstantsAs(endpointConstants), loadEndpt.newCallContext() |
101 | | - .withEndpointStateAs(endpointState1) |
102 | | - .withEndpointConstantsAs(endpointConstants1)}; |
| 101 | + .withEndpointConstantsAs("{\"max\":6,\"collection\":\"" + collection1 + "\"}"), |
| 102 | + loadEndpt.newCallContext() |
| 103 | + .withEndpointStateAs(endpointState) |
| 104 | + .withEndpointConstantsAs("{\"max\":6,\"collection\":\"" + collection2 + "\"}") |
| 105 | + }; |
103 | 106 | InputCaller.BulkInputCaller<StringHandle> loader = loadEndpt.bulkCaller(callContextArray); |
104 | 107 |
|
105 | | - Set<String> input = IOTestUtil.setOf( // Set.of( |
106 | | - "{\"docNum\":1, \"docName\":\"doc1\"}", |
107 | | - "{\"docNum\":2, \"docName\":\"doc2\"}", |
108 | | - "{\"docNum\":3, \"docName\":\"doc3\"}", |
109 | | - "{\"docNum\":4, \"docName\":\"doc4\"}", |
110 | | - "{\"docNum\":5, \"docName\":\"doc5\"}", |
111 | | - "{\"docNum\":6, \"docName\":\"doc6\"}", |
112 | | - "{\"docNum\":7, \"docName\":\"doc7\"}", |
113 | | - "{\"docNum\":8, \"docName\":\"doc8\"}" |
114 | | - ); |
115 | | - |
116 | | - input.stream().forEach(value -> { |
117 | | - //System.out.println("adding "+value); |
118 | | - loader.accept(new StringHandle(value).withFormat(Format.JSON)); |
119 | | - }); |
| 108 | + Set<Integer> expectedDocNums = new TreeSet<>(); |
| 109 | + for (int i = 1; i <= 8; i++) { |
| 110 | + expectedDocNums.add(i); |
| 111 | + loader.accept(new StringHandle("{\"docNum\":" + i + ", \"docName\":\"doc" + i + "\"}").withFormat(Format.JSON)); |
| 112 | + } |
120 | 113 | loader.awaitCompletion(); |
121 | | - checkDocuments("bulkInputTest_1"); |
122 | | - checkDocuments("bulkInputTest_2"); |
123 | | - assertTrue( counter == 4); |
124 | | - assertTrue( |
125 | | - map.get("bulkInputTest_1") >= 1); |
126 | | - assertTrue( |
127 | | - map.get("bulkInputTest_2") >= 1); |
| 114 | + |
| 115 | + // How the batches are spread across the two call contexts depends on thread timing, so only assert on the |
| 116 | + // union of the inputs that were written, not on which collection or URI received them. |
| 117 | + List<Integer> actualDocNums = new ArrayList<>(); |
| 118 | + for (String collection : new String[]{collection1, collection2}) { |
| 119 | + for (int next = 2; next <= 6; next++) { |
| 120 | + String uri = "/marklogic/ds/test/bulkInputCaller/" + collection + "/" + next + ".json"; |
| 121 | + if (docMgr.exists(uri) != null) { |
| 122 | + JsonNode doc = docMgr.read(uri, new JacksonHandle()).get(); |
| 123 | + assertEquals(next, doc.get("state").get("next").asInt()); |
| 124 | + assertEquals(6, doc.get("work").get("max").asInt()); |
| 125 | + doc.get("inputs").forEach(input -> actualDocNums.add(input.get("docNum").asInt())); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + Collections.sort(actualDocNums); |
| 130 | + assertEquals(new ArrayList<>(expectedDocNums), actualDocNums, |
| 131 | + "Every input should have been written exactly once, regardless of how batches were distributed"); |
128 | 132 | } |
129 | 133 |
|
130 | 134 | @AfterAll |
131 | 135 | public static void cleanup(){ |
132 | 136 | IOTestUtil.modMgr.delete(scriptPath, apiPath); |
133 | | - for(int i=2; i<5; i++) { |
134 | | - String uri = "/marklogic/ds/test/bulkInputCaller/bulkInputTest_1/" +i+".json"; |
135 | | - docMgr.delete(uri); |
136 | | - uri = "/marklogic/ds/test/bulkInputCaller/bulkInputTest_2/" +i+".json"; |
137 | | - docMgr.delete(uri); |
138 | | - } |
139 | 137 | } |
140 | 138 |
|
141 | 139 | private void checkDocuments(String collection) { |
|
0 commit comments