|
1 |
| -/* |
2 |
| - * Copyright 2014-2019 MarkLogic Corporation |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 |
| -package com.marklogic.client.functionaltest; |
18 |
| - |
19 |
| -import static org.junit.Assert.assertEquals; |
20 |
| -import static org.junit.Assert.assertTrue; |
21 |
| - |
22 |
| -import java.io.IOException; |
23 |
| -import java.security.KeyManagementException; |
24 |
| -import java.security.NoSuchAlgorithmException; |
25 |
| - |
26 |
| -import org.junit.After; |
27 |
| -import org.junit.AfterClass; |
28 |
| -import org.junit.BeforeClass; |
29 |
| -import org.junit.Test; |
30 |
| - |
31 |
| -import com.marklogic.client.DatabaseClient; |
32 |
| -import com.marklogic.client.document.TextDocumentManager; |
33 |
| - |
34 |
| -public class TestMultithreading extends BasicJavaClientREST { |
35 |
| - |
36 |
| - private static String dbName = "TestMultithreadingDB"; |
37 |
| - private static String[] fNames = { "TestMultithreadingDBDB-1" }; |
38 |
| - |
39 |
| - @BeforeClass |
40 |
| - public static void setUp() throws Exception { |
41 |
| - System.out.println("In setup"); |
42 |
| - configureRESTServer(dbName, fNames); |
43 |
| - } |
44 |
| - |
45 |
| - @After |
46 |
| - public void testCleanUp() throws Exception { |
47 |
| - clearDB(); |
48 |
| - System.out.println("Running clear script"); |
49 |
| - } |
50 |
| - |
51 |
| - @Test |
52 |
| - public void testMultithreading() throws KeyManagementException, NoSuchAlgorithmException, InterruptedException, IOException |
53 |
| - { |
54 |
| - ThreadClass dt1 = new ThreadClass("Thread A"); |
55 |
| - ThreadClass dt2 = new ThreadClass("Thread B"); |
56 |
| - |
57 |
| - Thread t1 = new Thread(dt1); |
58 |
| - t1.start(); // this will start thread of object 1 |
59 |
| - Thread t2 = new Thread(dt2); |
60 |
| - t2.start(); // this will start thread of object 2 |
61 |
| - t2.join(); |
62 |
| - |
63 |
| - DatabaseClient client = getDatabaseClient("rest-reader", "x", getConnType()); |
64 |
| - TextDocumentManager docMgr = client.newTextDocumentManager(); |
65 |
| - |
66 |
| - for (int i = 1; i <= 5; i++) { |
67 |
| - String expectedUri = "/multithread-content-A/filename" + i + ".txt"; |
68 |
| - String docUri = docMgr.exists("/multithread-content-A/filename" + i + ".txt").getUri(); |
69 |
| - assertEquals("URI is not found", expectedUri, docUri); |
70 |
| - } |
71 |
| - |
72 |
| - for (int i = 1; i <= 5; i++) { |
73 |
| - String expectedUri = "/multithread-content-B/filename" + i + ".txt"; |
74 |
| - String docUri = docMgr.exists("/multithread-content-B/filename" + i + ".txt").getUri(); |
75 |
| - assertEquals("URI is not found", expectedUri, docUri); |
76 |
| - } |
77 |
| - |
78 |
| - // release client |
79 |
| - client.release(); |
80 |
| - } |
81 |
| - |
82 |
| - @Test |
83 |
| - public void testMultithreadingMultipleSearch() throws KeyManagementException, NoSuchAlgorithmException, InterruptedException |
84 |
| - { |
85 |
| - System.out.println("testMultithreadingMultipleSearch"); |
86 |
| - |
87 |
| - ThreadWrite tw1 = new ThreadWrite("Write Thread"); |
88 |
| - Thread w1 = new Thread(tw1); |
89 |
| - w1.start(); |
90 |
| - w1.join(); |
91 |
| - |
92 |
| - ThreadSearch ts1 = new ThreadSearch("Search Thread 1"); |
93 |
| - ThreadSearch ts2 = new ThreadSearch("Search Thread 2"); |
94 |
| - ThreadSearch ts3 = new ThreadSearch("Search Thread 3"); |
95 |
| - ThreadSearch ts4 = new ThreadSearch("Search Thread 4"); |
96 |
| - ThreadSearch ts5 = new ThreadSearch("Search Thread 5"); |
97 |
| - |
98 |
| - Thread t1 = new Thread(ts1); |
99 |
| - t1.start(); |
100 |
| - Thread t2 = new Thread(ts2); |
101 |
| - t2.start(); |
102 |
| - Thread t3 = new Thread(ts3); |
103 |
| - t3.start(); |
104 |
| - Thread t4 = new Thread(ts4); |
105 |
| - t4.start(); |
106 |
| - Thread t5 = new Thread(ts5); |
107 |
| - t5.start(); |
108 |
| - |
109 |
| - t1.join(); |
110 |
| - t2.join(); |
111 |
| - t3.join(); |
112 |
| - t4.join(); |
113 |
| - t5.join(); |
114 |
| - |
115 |
| - long totalAllDocumentsReturned = ts1.totalAllResults + ts2.totalAllResults + ts3.totalAllResults + ts4.totalAllResults + ts5.totalAllResults; |
116 |
| - assertTrue("Documents count is incorrect", totalAllDocumentsReturned == 750); |
117 |
| - } |
118 |
| - |
119 |
| - @AfterClass |
120 |
| - public static void tearDown() throws Exception { |
121 |
| - System.out.println("In tear down"); |
122 |
| - cleanupRESTServer(dbName, fNames); |
123 |
| - } |
124 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2014-2019 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.marklogic.client.functionaltest; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.security.KeyManagementException; |
| 24 | +import java.security.NoSuchAlgorithmException; |
| 25 | + |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.AfterClass; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import com.fasterxml.jackson.databind.JsonNode; |
| 34 | +import com.marklogic.client.DatabaseClient; |
| 35 | +import com.marklogic.client.Transaction; |
| 36 | +import com.marklogic.client.document.TextDocumentManager; |
| 37 | +import com.marklogic.client.document.XMLDocumentManager; |
| 38 | +import com.marklogic.client.io.Format; |
| 39 | +import com.marklogic.client.io.JacksonHandle; |
| 40 | +import com.marklogic.client.io.StringHandle; |
| 41 | + |
| 42 | +public class TestMultithreading extends BasicJavaClientREST { |
| 43 | + |
| 44 | + private static String dbName = "TestMultithreadingDB"; |
| 45 | + private static String[] fNames = { "TestMultithreadingDBDB-1" }; |
| 46 | + private static final Logger logger = LoggerFactory.getLogger(TestMultithreading.class); |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public static void setUp() throws Exception { |
| 50 | + System.out.println("In setup"); |
| 51 | + configureRESTServer(dbName, fNames); |
| 52 | + } |
| 53 | + |
| 54 | + @After |
| 55 | + public void testCleanUp() throws Exception { |
| 56 | + clearDB(); |
| 57 | + System.out.println("Running clear script"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testMultithreading() throws KeyManagementException, NoSuchAlgorithmException, InterruptedException, IOException |
| 62 | + { |
| 63 | + ThreadClass dt1 = new ThreadClass("Thread A"); |
| 64 | + ThreadClass dt2 = new ThreadClass("Thread B"); |
| 65 | + |
| 66 | + Thread t1 = new Thread(dt1); |
| 67 | + t1.start(); // this will start thread of object 1 |
| 68 | + Thread t2 = new Thread(dt2); |
| 69 | + t2.start(); // this will start thread of object 2 |
| 70 | + t2.join(); |
| 71 | + |
| 72 | + DatabaseClient client = getDatabaseClient("rest-reader", "x", getConnType()); |
| 73 | + TextDocumentManager docMgr = client.newTextDocumentManager(); |
| 74 | + |
| 75 | + for (int i = 1; i <= 5; i++) { |
| 76 | + String expectedUri = "/multithread-content-A/filename" + i + ".txt"; |
| 77 | + String docUri = docMgr.exists("/multithread-content-A/filename" + i + ".txt").getUri(); |
| 78 | + assertEquals("URI is not found", expectedUri, docUri); |
| 79 | + } |
| 80 | + |
| 81 | + for (int i = 1; i <= 5; i++) { |
| 82 | + String expectedUri = "/multithread-content-B/filename" + i + ".txt"; |
| 83 | + String docUri = docMgr.exists("/multithread-content-B/filename" + i + ".txt").getUri(); |
| 84 | + assertEquals("URI is not found", expectedUri, docUri); |
| 85 | + } |
| 86 | + |
| 87 | + // release client |
| 88 | + client.release(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testMultithreadingMultipleSearch() throws KeyManagementException, NoSuchAlgorithmException, InterruptedException |
| 93 | + { |
| 94 | + System.out.println("testMultithreadingMultipleSearch"); |
| 95 | + |
| 96 | + ThreadWrite tw1 = new ThreadWrite("Write Thread"); |
| 97 | + Thread w1 = new Thread(tw1); |
| 98 | + w1.start(); |
| 99 | + w1.join(); |
| 100 | + |
| 101 | + ThreadSearch ts1 = new ThreadSearch("Search Thread 1"); |
| 102 | + ThreadSearch ts2 = new ThreadSearch("Search Thread 2"); |
| 103 | + ThreadSearch ts3 = new ThreadSearch("Search Thread 3"); |
| 104 | + ThreadSearch ts4 = new ThreadSearch("Search Thread 4"); |
| 105 | + ThreadSearch ts5 = new ThreadSearch("Search Thread 5"); |
| 106 | + |
| 107 | + Thread t1 = new Thread(ts1); |
| 108 | + t1.start(); |
| 109 | + Thread t2 = new Thread(ts2); |
| 110 | + t2.start(); |
| 111 | + Thread t3 = new Thread(ts3); |
| 112 | + t3.start(); |
| 113 | + Thread t4 = new Thread(ts4); |
| 114 | + t4.start(); |
| 115 | + Thread t5 = new Thread(ts5); |
| 116 | + t5.start(); |
| 117 | + |
| 118 | + t1.join(); |
| 119 | + t2.join(); |
| 120 | + t3.join(); |
| 121 | + t4.join(); |
| 122 | + t5.join(); |
| 123 | + |
| 124 | + long totalAllDocumentsReturned = ts1.totalAllResults + ts2.totalAllResults + ts3.totalAllResults + ts4.totalAllResults + ts5.totalAllResults; |
| 125 | + assertTrue("Documents count is incorrect", totalAllDocumentsReturned == 750); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testDeadlockInMultiStmt() throws KeyManagementException, NoSuchAlgorithmException, InterruptedException, IOException |
| 130 | + { |
| 131 | + System.out.println("testDeadlockInMultiStmt"); |
| 132 | + |
| 133 | + final DatabaseClient client = getDatabaseClient("rest-writer", "x", getConnType());; |
| 134 | + String docId = "deadlockTest.xml"; |
| 135 | + |
| 136 | + StringHandle handle = new StringHandle("<doc><name>Original</name></doc>") |
| 137 | + .withFormat(Format.XML); |
| 138 | + |
| 139 | + XMLDocumentManager docMgr = client.newXMLDocumentManager(); |
| 140 | + docMgr.write(docId, handle); |
| 141 | + Transaction t1 = client.openTransaction(); |
| 142 | + Transaction t2 = client.openTransaction(); |
| 143 | + Transaction t3 = client.openTransaction(); |
| 144 | + Transaction t4 = client.openTransaction(); |
| 145 | + Transaction t5 = client.openTransaction(); |
| 146 | + Transaction t6 = client.openTransaction(); |
| 147 | + final Thread th1 = new Thread(() -> { |
| 148 | + try { |
| 149 | + client.newXMLDocumentManager().write(docId, new StringHandle("<doc><t>Original Modified by t1</t></doc>").withFormat(Format.XML), t1); |
| 150 | + logger.info("Committing t1"); |
| 151 | + t1.commit(); |
| 152 | + } catch (Exception ex) { |
| 153 | + logger.info("Rollback t1"); |
| 154 | + t1.rollback(); |
| 155 | + } |
| 156 | + }, "t1"); |
| 157 | + th1.setDaemon(false); |
| 158 | + |
| 159 | + final Thread th2 = new Thread(() -> { |
| 160 | + try { |
| 161 | + client.newXMLDocumentManager().write(docId, new StringHandle("<doc><t>Original Modified by t2</t></doc>").withFormat(Format.XML), t2); |
| 162 | + logger.info("Committing t2"); |
| 163 | + t2.commit(); |
| 164 | + } catch (Exception ex) { |
| 165 | + logger.info("Rollback t2"); |
| 166 | + t2.rollback(); |
| 167 | + } |
| 168 | + }, "t2"); |
| 169 | + th2.setDaemon(false); |
| 170 | + |
| 171 | + final Thread th3 = new Thread(() -> { |
| 172 | + try { |
| 173 | + client.newXMLDocumentManager().write(docId, new StringHandle("<doc><t>Original Modified by t3</t></doc>").withFormat(Format.XML), t3); |
| 174 | + logger.info("Committing t3"); |
| 175 | + t3.commit(); |
| 176 | + } catch (Exception ex) { |
| 177 | + logger.info("Rollback t3"); |
| 178 | + t3.rollback(); |
| 179 | + } |
| 180 | + }, "t3"); |
| 181 | + th3.setDaemon(false); |
| 182 | + |
| 183 | + final Thread th4 = new Thread(() -> { |
| 184 | + try { |
| 185 | + client.newXMLDocumentManager().write(docId, new StringHandle("<doc><t>Original Modified by t4</t></doc>").withFormat(Format.XML), t4); |
| 186 | + logger.info("Committing t4"); |
| 187 | + t4.commit(); |
| 188 | + } catch (Exception ex) { |
| 189 | + logger.info("Rollback t4"); |
| 190 | + t4.rollback(); |
| 191 | + } |
| 192 | + }, "t4"); |
| 193 | + th4.setDaemon(false); |
| 194 | + |
| 195 | + final Thread th5 = new Thread(() -> { |
| 196 | + try { |
| 197 | + client.newXMLDocumentManager().write(docId, new StringHandle("<doc><t>Original Modified by t5</t></doc>").withFormat(Format.XML), t5); |
| 198 | + logger.info("Committing t5"); |
| 199 | + t5.commit(); |
| 200 | + } catch (Exception ex) { |
| 201 | + logger.info("Rollback t5"); |
| 202 | + t5.rollback(); |
| 203 | + } |
| 204 | + }, "t5"); |
| 205 | + th5.setDaemon(false); |
| 206 | + |
| 207 | + final Thread th6 = new Thread(() -> { |
| 208 | + try { |
| 209 | + client.newXMLDocumentManager().write(docId, new StringHandle("<doc><t>Original Modified by t6</t></doc>").withFormat(Format.XML), t6); |
| 210 | + logger.info("Committing t6"); |
| 211 | + JacksonHandle jh = new JacksonHandle(); |
| 212 | + t6.readStatus(jh); |
| 213 | + JsonNode node = jh.get(); |
| 214 | + System.out.println("Read status from t6 is " + node.asText()); |
| 215 | + Thread.sleep(15000); |
| 216 | + t6.commit(); |
| 217 | + } catch (Exception ex) { |
| 218 | + logger.info("Rollback t6"); |
| 219 | + t6.rollback(); |
| 220 | + } |
| 221 | + }, "t6"); |
| 222 | + th6.setDaemon(false); |
| 223 | + |
| 224 | + th2.start(); |
| 225 | + th1.start(); |
| 226 | + th4.start(); |
| 227 | + StringHandle strHdle = docMgr.read(docId, new StringHandle()); |
| 228 | + System.out.println(strHdle.get().toString()); |
| 229 | + Thread.sleep(5000); |
| 230 | + th3.start(); |
| 231 | + th5.start(); |
| 232 | + th6.start(); |
| 233 | + // Thread.sleep(15000); |
| 234 | + StringHandle strHdle1 = docMgr.read(docId, new StringHandle()); |
| 235 | + System.out.println(strHdle1.get().toString()); |
| 236 | + |
| 237 | + StringHandle strHdle2 = docMgr.read(docId, new StringHandle()); |
| 238 | + System.out.println(strHdle2.get().toString()); |
| 239 | +} |
| 240 | + |
| 241 | + @AfterClass |
| 242 | + public static void tearDown() throws Exception { |
| 243 | + System.out.println("In tear down"); |
| 244 | + cleanupRESTServer(dbName, fNames); |
| 245 | + } |
| 246 | +} |
0 commit comments