Skip to content

Commit 5a700db

Browse files
georgeajitgeorgeajit
georgeajit
authored and
georgeajit
committed
Git #882, #891 - New test
1 parent 456f0b2 commit 5a700db

File tree

2 files changed

+160
-13
lines changed

2 files changed

+160
-13
lines changed

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/datamovement/functionaltests/QBFailover.java

Lines changed: 138 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
/*
2+
* Copyright 2014-2018 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+
*/
116
package com.marklogic.client.datamovement.functionaltests;
217

318
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
420

521
import java.io.BufferedReader;
622
import java.io.File;
@@ -58,7 +74,11 @@
5874
import com.marklogic.client.datamovement.JobTicket;
5975
import com.marklogic.client.datamovement.NoResponseListener;
6076
import com.marklogic.client.datamovement.QueryBatch;
77+
import com.marklogic.client.datamovement.QueryBatchException;
78+
import com.marklogic.client.datamovement.QueryBatchListener;
6179
import com.marklogic.client.datamovement.QueryBatcher;
80+
import com.marklogic.client.datamovement.QueryBatcherListener;
81+
import com.marklogic.client.datamovement.QueryFailureListener;
6282
import com.marklogic.client.datamovement.WriteBatcher;
6383
import com.marklogic.client.document.DocumentPage;
6484
import com.marklogic.client.document.DocumentRecord;
@@ -99,7 +119,13 @@ public static void setUpBeforeClass() throws Exception {
99119

100120
server = getRestAppServerName();
101121
port = getRestAppServerPort();
102-
122+
123+
// Create App Server if needed.
124+
createRESTServerWithDB(server, port);
125+
associateRESTServerWithDB(server, dbName);
126+
if (IsSecurityEnabled()) {
127+
enableSecurityOnRESTServer(server, dbName);
128+
}
103129
hostNames = getHosts();
104130
// Perform the setup on multiple nodes only.
105131
if (hostNames.length > 1) {
@@ -182,12 +208,7 @@ public static void setUpBeforeClass() throws Exception {
182208
props.put("journaling", "strict");
183209
changeProperty(props, "/manage/v2/databases/" + dbName + "/properties");
184210
Thread.currentThread().sleep(500L);
185-
// Create App Server if needed.
186-
createRESTServerWithDB(server, port);
187-
associateRESTServerWithDB(server, dbName);
188-
if (IsSecurityEnabled()) {
189-
enableSecurityOnRESTServer(server, dbName);
190-
}
211+
191212
// StringHandle
192213
stringTriple = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>This is so foo</foo>";
193214
stringHandle = new StringHandle(stringTriple);
@@ -783,6 +804,116 @@ public void massDeleteConsistentSnapShot() throws Exception {
783804
System.out.println("Count: " + evalClient.newServerEval().xquery(query1).eval().next().getNumber().intValue());
784805
assertEquals(0, evalClient.newServerEval().xquery(query1).eval().next().getNumber().intValue());
785806
}
807+
808+
/* This test is intended to test closing of listeners when job is done.
809+
*
810+
*
811+
*/
812+
@Test(timeout = 450000)
813+
public void testListenerCloseables() throws Exception {
814+
Assume.assumeTrue(hostNames.length > 1);
815+
816+
System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());
817+
AtomicInteger success = new AtomicInteger(0);
818+
// There two variables are to track close method on Listeners.
819+
AtomicBoolean testCloseOnBatchListenerUriReady = new AtomicBoolean(false);
820+
AtomicBoolean testCloseOnFailureListenerQueryFailure = new AtomicBoolean(false);
821+
822+
// This variable tracks the OnJobCompleteion status
823+
AtomicBoolean getOnePrimaryDBClient = new AtomicBoolean(false);
824+
825+
// Track primary database client on all listeners and job completion
826+
StringBuilder sb_strBatchListenerUriReady = new StringBuilder();
827+
StringBuilder sb_strJobCompletionListener = new StringBuilder();
828+
829+
830+
class TestCloseOnBatchListenerUriReady implements QueryBatchListener, AutoCloseable {
831+
832+
@Override
833+
public void close() throws Exception {
834+
System.out.println("Close called from testMinNodesWithCloseable in TestCloseOnBatchListenerUriReady class");
835+
testCloseOnBatchListenerUriReady.set(true);
836+
}
837+
838+
@Override
839+
public void processEvent(QueryBatch batch) {
840+
System.out.println("processEvent called from testMinNodesWithCloseable in TestCloseOnBatchListenerUriReady class");
841+
// Verify the Primary DatabaseClient instance
842+
if (!getOnePrimaryDBClient.get()) {
843+
getOnePrimaryDBClient.set(true);
844+
sb_strBatchListenerUriReady.append(batch.getBatcher().getPrimaryClient().getHost());
845+
sb_strBatchListenerUriReady.append("|");
846+
sb_strBatchListenerUriReady.append(batch.getBatcher().getPrimaryClient().getPort());
847+
}
848+
}
849+
}
850+
851+
class TestCloseOnBatchListenerQueryFailure implements QueryFailureListener, AutoCloseable {
852+
853+
@Override
854+
public void close() throws Exception {
855+
System.out.println("Close called from testMinNodesWithCloseable in TestCloseOnBatchListenerQueryFailure class");
856+
testCloseOnFailureListenerQueryFailure.set(true);
857+
}
858+
859+
@Override
860+
public void processFailure(QueryBatchException failure) {
861+
System.out.println("processFailure called from testMinNodesWithCloseable in TestCloseOnBatchListenerQueryFailure class");
862+
}
863+
}
864+
865+
// Listener to be called when QueryBatcher has completed reading all URIs
866+
class TestQBJobCompleteionListener implements QueryBatcherListener {
867+
868+
@Override
869+
public void processEvent(QueryBatcher batcher) {
870+
System.out.println("processEvent called from testMinNodesWithCloseable in TestQBJobCompleteionListener class");
871+
872+
// Verify a detail - ticket Id at end of completion
873+
sb_strJobCompletionListener.append(batcher.getBatchSize());
874+
}
875+
}
876+
877+
try {
878+
879+
TestCloseOnBatchListenerUriReady closeBatchURIs = new TestCloseOnBatchListenerUriReady();
880+
TestCloseOnBatchListenerQueryFailure closeQueryFailure = new TestCloseOnBatchListenerQueryFailure();
881+
TestQBJobCompleteionListener jobCompleteListener = new TestQBJobCompleteionListener();
882+
883+
QueryBatcher batcher = dmManager.newQueryBatcher(new StructuredQueryBuilder().collection("XmlTransform"))
884+
.withBatchSize(4000).withThreadCount(5);
885+
886+
// Add the new Listeners to the batcher.
887+
888+
batcher.onUrisReady((batch) -> {
889+
success.addAndGet(batch.getItems().length);
890+
}).onQueryFailure(queryException -> {
891+
queryException.printStackTrace();
892+
}).onUrisReady(closeBatchURIs)
893+
.onQueryFailure(closeQueryFailure)
894+
.onJobCompletion(jobCompleteListener);
895+
896+
ticket = dmManager.startJob(batcher);
897+
898+
batcher.awaitCompletion();
899+
dmManager.stopJob(ticket);
900+
} catch (Exception e) {
901+
e.printStackTrace();
902+
}
903+
// Verify the DatabaseClient instances.
904+
System.out.println("Primary database instance is " + sb_strBatchListenerUriReady.toString());
905+
906+
// Verify the close status
907+
assertTrue("Close is not called from testMinNodesWithCloseable in TestCloseOnBatchListenerUriReady class", testCloseOnBatchListenerUriReady.get());
908+
assertTrue("Close is not called from testMinNodesWithCloseable in TestCloseOnBatchListenerQueryFailure class", testCloseOnFailureListenerQueryFailure.get());
909+
910+
// Verify the batch size on job completion
911+
assertTrue("Job Completion details not equal",
912+
sb_strJobCompletionListener.toString().equalsIgnoreCase("4000"));
913+
// Verify the primary database client
914+
assertTrue("Primary database details not correct",
915+
sb_strBatchListenerUriReady.toString().contains(String.valueOf(port)));
916+
}
786917

787918
private void serverStartStop(String server, String command) throws Exception {
788919
System.out.println("Preparing to " + command + " " + server);

marklogic-client-api-functionaltests/src/test/java/com/marklogic/client/datamovement/functionaltests/WBFailover.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2014-2018 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+
*/
116
package com.marklogic.client.datamovement.functionaltests;
217

318
import java.io.BufferedReader;
@@ -80,6 +95,12 @@ public static void setUpBeforeClass() throws Exception {
8095

8196
server = getRestAppServerName();
8297
port = getRestAppServerPort();
98+
// Create App Server if needed.
99+
createRESTServerWithDB(server, port);
100+
associateRESTServerWithDB(server, dbName);
101+
if (IsSecurityEnabled()) {
102+
enableSecurityOnRESTServer(server, dbName);
103+
}
83104

84105
hostNames = getHosts();
85106
// Perform the setup on multiple nodes only.
@@ -159,12 +180,7 @@ public static void setUpBeforeClass() throws Exception {
159180
props.put("journaling", "strict");
160181
changeProperty(props, "/manage/v2/databases/" + dbName + "/properties");
161182
Thread.currentThread().sleep(2000L);
162-
// Create App Server if needed.
163-
createRESTServerWithDB(server, port);
164-
associateRESTServerWithDB(server, dbName);
165-
if (IsSecurityEnabled()) {
166-
enableSecurityOnRESTServer(server, dbName);
167-
}
183+
168184
// StringHandle
169185
stringTriple = "<top-song xmlns=\"http://marklogic.com/MLU/top-songs\"> <!--Copyright (c) 2010 Mark Logic Corporation. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published bythe Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled \"GNU Free Documentation License.\" Content derived from http://en.wikipedia.org/w/index.php?title=Blue_Champagne_(song)&action=edit&redlink=1 Modified in February 2010 by Mark Logic Corporation under the terms of the GNU Free Documentation License.--> <title href=\"http://en.wikipedia.org/w/index.php?title=Blue_Champagne_(song)&amp;action=edit&amp;redlink=1\" xmlns:ts=\"http://marklogic.com/MLU/top-songs\">Blue Champagne</title> <artist xmlns:ts=\"http://marklogic.com/MLU/top-songs\"/> <weeks last=\"1941-09-27\"> <week>1941-09-27</week> </weeks> <descr/></top-song>";
170186
stringHandle = new StringHandle(stringTriple);

0 commit comments

Comments
 (0)