use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.
the class RemoteClusterClientTests method testEnsureWeReconnect.
@TestLogging(value = "org.opensearch.transport.SniffConnectionStrategy:TRACE,org.opensearch.transport.ClusterConnectionManager:TRACE", reason = "debug intermittent test failure")
public void testEnsureWeReconnect() throws Exception {
Settings remoteSettings = Settings.builder().put(ClusterName.CLUSTER_NAME_SETTING.getKey(), "foo_bar_cluster").build();
try (MockTransportService remoteTransport = startTransport("remote_node", Collections.emptyList(), Version.CURRENT, threadPool, remoteSettings)) {
DiscoveryNode remoteNode = remoteTransport.getLocalDiscoNode();
Settings localSettings = Settings.builder().put(onlyRole(DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE)).put("cluster.remote.test.seeds", remoteNode.getAddress().getAddress() + ":" + remoteNode.getAddress().getPort()).build();
try (MockTransportService service = MockTransportService.createNewService(localSettings, Version.CURRENT, threadPool, null)) {
service.start();
// this test is not perfect since we might reconnect concurrently but it will fail most of the time if we don't have
// the right calls in place in the RemoteAwareClient
service.acceptIncomingRequests();
RemoteClusterService remoteClusterService = service.getRemoteClusterService();
assertBusy(() -> assertTrue(remoteClusterService.isRemoteNodeConnected("test", remoteNode)));
for (int i = 0; i < 10; i++) {
RemoteClusterConnection remoteClusterConnection = remoteClusterService.getRemoteClusterConnection("test");
assertBusy(remoteClusterConnection::assertNoRunningConnections);
ConnectionManager connectionManager = remoteClusterConnection.getConnectionManager();
Transport.Connection connection = connectionManager.getConnection(remoteNode);
PlainActionFuture<Void> closeFuture = PlainActionFuture.newFuture();
connection.addCloseListener(closeFuture);
connectionManager.disconnectFromNode(remoteNode);
closeFuture.get();
Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test");
ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get();
assertNotNull(clusterStateResponse);
assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value());
assertTrue(remoteClusterConnection.isNodeConnected(remoteNode));
}
}
}
}
use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.
the class AbstractSimpleTransportTestCase method testTracerLog.
@TestLogging(value = "org.opensearch.transport.TransportService.tracer:trace", reason = "to ensure we log network events on TRACE level")
public void testTracerLog() throws Exception {
TransportRequestHandler<TransportRequest> handler = (request, channel, task) -> channel.sendResponse(new StringMessageResponse(""));
TransportRequestHandler<StringMessageRequest> handlerWithError = (request, channel, task) -> {
if (request.timeout() > 0) {
Thread.sleep(request.timeout);
}
channel.sendResponse(new RuntimeException(""));
};
TransportResponseHandler<StringMessageResponse> noopResponseHandler = new TransportResponseHandler<StringMessageResponse>() {
@Override
public StringMessageResponse read(StreamInput in) throws IOException {
return new StringMessageResponse(in);
}
@Override
public void handleResponse(StringMessageResponse response) {
}
@Override
public void handleException(TransportException exp) {
}
@Override
public String executor() {
return ThreadPool.Names.SAME;
}
};
serviceA.registerRequestHandler("internal:test", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
serviceA.registerRequestHandler("internal:testNotSeen", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
serviceA.registerRequestHandler("internal:testError", ThreadPool.Names.SAME, StringMessageRequest::new, handlerWithError);
serviceB.registerRequestHandler("internal:test", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
serviceB.registerRequestHandler("internal:testNotSeen", ThreadPool.Names.SAME, StringMessageRequest::new, handler);
serviceB.registerRequestHandler("internal:testError", ThreadPool.Names.SAME, StringMessageRequest::new, handlerWithError);
String includeSettings;
String excludeSettings;
if (randomBoolean()) {
// sometimes leave include empty (default)
includeSettings = randomBoolean() ? "*" : "";
excludeSettings = "internal:testNotSeen";
} else {
includeSettings = "internal:test,internal:testError";
excludeSettings = "DOESN'T_MATCH";
}
clusterSettingsA.applySettings(Settings.builder().put(TransportSettings.TRACE_LOG_INCLUDE_SETTING.getKey(), includeSettings).put(TransportSettings.TRACE_LOG_EXCLUDE_SETTING.getKey(), excludeSettings).build());
final Logger logger = LogManager.getLogger("org.opensearch.transport.TransportService.tracer");
try (MockLogAppender appender = MockLogAppender.createForLoggers(logger)) {
final String requestSent = ".*\\[internal:test].*sent to.*\\{TS_B}.*";
final MockLogAppender.LoggingExpectation requestSentExpectation = new MockLogAppender.PatternSeenEventExpectation("sent request", "org.opensearch.transport.TransportService.tracer", Level.TRACE, requestSent);
final String requestReceived = ".*\\[internal:test].*received request.*";
final MockLogAppender.LoggingExpectation requestReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("received request", "org.opensearch.transport.TransportService.tracer", Level.TRACE, requestReceived);
final String responseSent = ".*\\[internal:test].*sent response.*";
final MockLogAppender.LoggingExpectation responseSentExpectation = new MockLogAppender.PatternSeenEventExpectation("sent response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, responseSent);
final String responseReceived = ".*\\[internal:test].*received response from.*\\{TS_B}.*";
final MockLogAppender.LoggingExpectation responseReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("received response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, responseReceived);
appender.addExpectation(requestSentExpectation);
appender.addExpectation(requestReceivedExpectation);
appender.addExpectation(responseSentExpectation);
appender.addExpectation(responseReceivedExpectation);
StringMessageRequest request = new StringMessageRequest("", 10);
serviceA.sendRequest(nodeB, "internal:test", request, TransportRequestOptions.EMPTY, noopResponseHandler);
assertBusy(appender::assertAllExpectationsMatched);
final String errorResponseSent = ".*\\[internal:testError].*sent error response.*";
final MockLogAppender.LoggingExpectation errorResponseSentExpectation = new MockLogAppender.PatternSeenEventExpectation("sent error response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, errorResponseSent);
final String errorResponseReceived = ".*\\[internal:testError].*received response from.*\\{TS_B}.*";
final MockLogAppender.LoggingExpectation errorResponseReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("received error response", "org.opensearch.transport.TransportService.tracer", Level.TRACE, errorResponseReceived);
appender.addExpectation(errorResponseSentExpectation);
appender.addExpectation(errorResponseReceivedExpectation);
serviceA.sendRequest(nodeB, "internal:testError", new StringMessageRequest(""), noopResponseHandler);
assertBusy(appender::assertAllExpectationsMatched);
final String notSeenSent = "*[internal:testNotSeen]*sent to*";
final MockLogAppender.LoggingExpectation notSeenSentExpectation = new MockLogAppender.UnseenEventExpectation("not seen request sent", "org.opensearch.transport.TransportService.tracer", Level.TRACE, notSeenSent);
final String notSeenReceived = ".*\\[internal:testNotSeen].*received request.*";
final MockLogAppender.LoggingExpectation notSeenReceivedExpectation = new MockLogAppender.PatternSeenEventExpectation("not seen request received", "org.opensearch.transport.TransportService.tracer", Level.TRACE, notSeenReceived);
appender.addExpectation(notSeenSentExpectation);
appender.addExpectation(notSeenReceivedExpectation);
PlainTransportFuture<StringMessageResponse> future = new PlainTransportFuture<>(noopResponseHandler);
serviceA.sendRequest(nodeB, "internal:testNotSeen", new StringMessageRequest(""), future);
future.txGet();
assertBusy(appender::assertAllExpectationsMatched);
}
}
use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.
the class LoggingListenerTests method runTestCustomLevelPerMethod.
private void runTestCustomLevelPerMethod(final Class<?> clazz) throws Exception {
LoggingListener loggingListener = new LoggingListener();
Description suiteDescription = Description.createSuiteDescription(clazz);
Logger xyzLogger = LogManager.getLogger("xyz");
Logger abcLogger = LogManager.getLogger("abc");
final Level level = LogManager.getRootLogger().getLevel();
assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testRunStarted(suiteDescription);
assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
Method method = clazz.getMethod("annotatedTestMethod");
TestLogging testLogging = method.getAnnotation(TestLogging.class);
TestIssueLogging testIssueLogging = method.getAnnotation(TestIssueLogging.class);
Annotation[] annotations = Stream.of(testLogging, testIssueLogging).filter(Objects::nonNull).toArray(Annotation[]::new);
Description testDescription = Description.createTestDescription(LoggingListenerTests.class, "annotatedTestMethod", annotations);
loggingListener.testStarted(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(Level.TRACE));
assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testFinished(testDescription);
assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
loggingListener.testRunFinished(new Result());
assertThat(xyzLogger.getLevel(), equalTo(level));
assertThat(abcLogger.getLevel(), equalTo(level));
}
use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.
the class LoggingListener method testStarted.
@Override
public void testStarted(final Description description) throws Exception {
final TestLogging testLogging = description.getAnnotation(TestLogging.class);
final TestIssueLogging testIssueLogging = description.getAnnotation(TestIssueLogging.class);
previousLoggingMap = processTestLogging(testLogging, testIssueLogging);
}
use of org.opensearch.test.junit.annotations.TestLogging in project OpenSearch by opensearch-project.
the class MasterServiceTests method testClusterStateUpdateLogging.
@TestLogging(value = "org.opensearch.cluster.service:TRACE", reason = "to ensure that we log cluster state events on TRACE level")
public void testClusterStateUpdateLogging() throws Exception {
try (MockLogAppender mockAppender = MockLogAppender.createForLoggers(LogManager.getLogger(MasterService.class))) {
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1 start", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test1]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1 computation", MasterService.class.getCanonicalName(), Level.DEBUG, "took [1s] to compute cluster state update for [test1]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test1 notification", MasterService.class.getCanonicalName(), Level.DEBUG, "took [0s] to notify listeners on unchanged cluster state for [test1]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 start", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test2]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 failure", MasterService.class.getCanonicalName(), Level.TRACE, "failed to execute cluster state update (on version: [*], uuid: [*]) for [test2]*"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 computation", MasterService.class.getCanonicalName(), Level.DEBUG, "took [2s] to compute cluster state update for [test2]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test2 notification", MasterService.class.getCanonicalName(), Level.DEBUG, "took [0s] to notify listeners on unchanged cluster state for [test2]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3 start", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test3]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3 computation", MasterService.class.getCanonicalName(), Level.DEBUG, "took [3s] to compute cluster state update for [test3]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test3 notification", MasterService.class.getCanonicalName(), Level.DEBUG, "took [4s] to notify listeners on successful publication of cluster state (version: *, uuid: *) for [test3]"));
mockAppender.addExpectation(new MockLogAppender.SeenEventExpectation("test4", MasterService.class.getCanonicalName(), Level.DEBUG, "executing cluster state update for [test4]"));
try (MasterService masterService = createMasterService(true)) {
masterService.submitStateUpdateTask("test1", new ClusterStateUpdateTask() {
@Override
public ClusterState execute(ClusterState currentState) {
relativeTimeInMillis += TimeValue.timeValueSeconds(1).millis();
return currentState;
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
}
@Override
public void onFailure(String source, Exception e) {
fail();
}
});
masterService.submitStateUpdateTask("test2", new ClusterStateUpdateTask() {
@Override
public ClusterState execute(ClusterState currentState) {
relativeTimeInMillis += TimeValue.timeValueSeconds(2).millis();
throw new IllegalArgumentException("Testing handling of exceptions in the cluster state task");
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
fail();
}
@Override
public void onFailure(String source, Exception e) {
}
});
masterService.submitStateUpdateTask("test3", new ClusterStateUpdateTask() {
@Override
public ClusterState execute(ClusterState currentState) {
relativeTimeInMillis += TimeValue.timeValueSeconds(3).millis();
return ClusterState.builder(currentState).incrementVersion().build();
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
relativeTimeInMillis += TimeValue.timeValueSeconds(4).millis();
}
@Override
public void onFailure(String source, Exception e) {
fail();
}
});
masterService.submitStateUpdateTask("test4", new ClusterStateUpdateTask() {
@Override
public ClusterState execute(ClusterState currentState) {
return currentState;
}
@Override
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
}
@Override
public void onFailure(String source, Exception e) {
fail();
}
});
assertBusy(mockAppender::assertAllExpectationsMatched);
}
}
}
Aggregations