From 545266496c6992f9559056764943bb9e02f02d4f Mon Sep 17 00:00:00 2001 From: Shivansh Arora Date: Wed, 19 Jun 2024 13:42:10 +0530 Subject: [PATCH] Move TestCapturingListener to test/framework Signed-off-by: Shivansh Arora --- ...oteClusterStateAttributesManagerTests.java | 84 +++++++++---------- .../gateway/remote/RemoteStateTestUtil.java | 28 ------- 2 files changed, 41 insertions(+), 71 deletions(-) delete mode 100644 server/src/test/java/org/opensearch/gateway/remote/RemoteStateTestUtil.java diff --git a/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateAttributesManagerTests.java b/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateAttributesManagerTests.java index e31dc69d93fca..fe9ed57fa77b8 100644 --- a/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateAttributesManagerTests.java +++ b/server/src/test/java/org/opensearch/gateway/remote/RemoteClusterStateAttributesManagerTests.java @@ -20,6 +20,7 @@ import org.opensearch.common.blobstore.BlobPath; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; +import org.opensearch.common.util.TestCapturingListener; import org.opensearch.core.action.ActionListener; import org.opensearch.core.common.io.stream.NamedWriteableRegistry; import org.opensearch.core.common.io.stream.StreamInput; @@ -116,18 +117,17 @@ public void testGetAsyncMetadataWriteAction_DiscoveryNodes() throws IOException, }).when(blobStoreTransferService) .uploadBlob(any(InputStream.class), anyIterable(), anyString(), eq(URGENT), any(ActionListener.class)); final CountDownLatch latch = new CountDownLatch(1); - final RemoteStateTestUtil.TestCapturingListener listener = - new RemoteStateTestUtil.TestCapturingListener<>(); + final TestCapturingListener listener = new TestCapturingListener<>(); remoteClusterStateAttributesManager.getAsyncMetadataWriteAction( DISCOVERY_NODES, remoteDiscoveryNodes, new LatchedActionListener<>(listener, latch) ).run(); latch.await(); - assertNull(listener.failure); - assertNotNull(listener.result); - assertEquals(DISCOVERY_NODES, listener.result.getComponent()); - String uploadedFileName = listener.result.getUploadedFilename(); + assertNull(listener.getFailure()); + assertNotNull(listener.getResult()); + assertEquals(DISCOVERY_NODES, listener.getResult().getComponent()); + String uploadedFileName = listener.getResult().getUploadedFilename(); String[] pathTokens = uploadedFileName.split(PATH_DELIMITER); assertEquals(5, pathTokens.length); assertEquals(RemoteClusterStateUtils.encodeString(CLUSTER_NAME), pathTokens[0]); @@ -149,18 +149,18 @@ public void testGetAsyncMetadataReadAction_DiscoveryNodes() throws IOException, ); RemoteDiscoveryNodes remoteObjForDownload = new RemoteDiscoveryNodes(fileName, "cluster-uuid", compressor); CountDownLatch latch = new CountDownLatch(1); - RemoteStateTestUtil.TestCapturingListener listener = new RemoteStateTestUtil.TestCapturingListener<>(); + TestCapturingListener listener = new TestCapturingListener<>(); remoteClusterStateAttributesManager.getAsyncMetadataReadAction( DISCOVERY_NODES, remoteObjForDownload, new LatchedActionListener<>(listener, latch) ).run(); latch.await(); - assertNull(listener.failure); - assertNotNull(listener.result); - assertEquals(CLUSTER_STATE_ATTRIBUTE, listener.result.getComponent()); - assertEquals(DISCOVERY_NODES, listener.result.getComponentName()); - DiscoveryNodes readDiscoveryNodes = (DiscoveryNodes) listener.result.getObj(); + assertNull(listener.getFailure()); + assertNotNull(listener.getResult()); + assertEquals(CLUSTER_STATE_ATTRIBUTE, listener.getResult().getComponent()); + assertEquals(DISCOVERY_NODES, listener.getResult().getComponentName()); + DiscoveryNodes readDiscoveryNodes = (DiscoveryNodes) listener.getResult().getObj(); assertEquals(discoveryNodes.getSize(), readDiscoveryNodes.getSize()); discoveryNodes.getNodes().forEach((nodeId, node) -> assertEquals(readDiscoveryNodes.get(nodeId), node)); assertEquals(discoveryNodes.getClusterManagerNodeId(), readDiscoveryNodes.getClusterManagerNodeId()); @@ -175,18 +175,17 @@ public void testGetAsyncMetadataWriteAction_ClusterBlocks() throws IOException, }).when(blobStoreTransferService) .uploadBlob(any(InputStream.class), anyIterable(), anyString(), eq(URGENT), any(ActionListener.class)); final CountDownLatch latch = new CountDownLatch(1); - final RemoteStateTestUtil.TestCapturingListener listener = - new RemoteStateTestUtil.TestCapturingListener<>(); + final TestCapturingListener listener = new TestCapturingListener<>(); remoteClusterStateAttributesManager.getAsyncMetadataWriteAction( CLUSTER_BLOCKS, remoteClusterBlocks, new LatchedActionListener<>(listener, latch) ).run(); latch.await(); - assertNull(listener.failure); - assertNotNull(listener.result); - assertEquals(CLUSTER_BLOCKS, listener.result.getComponent()); - String uploadedFileName = listener.result.getUploadedFilename(); + assertNull(listener.getFailure()); + assertNotNull(listener.getResult()); + assertEquals(CLUSTER_BLOCKS, listener.getResult().getComponent()); + String uploadedFileName = listener.getResult().getUploadedFilename(); String[] pathTokens = uploadedFileName.split(PATH_DELIMITER); assertEquals(5, pathTokens.length); assertEquals(encodeString(CLUSTER_NAME), pathTokens[0]); @@ -208,7 +207,7 @@ public void testGetAsyncMetadataReadAction_ClusterBlocks() throws IOException, I ); RemoteClusterBlocks remoteClusterBlocks = new RemoteClusterBlocks(fileName, "cluster-uuid", compressor); CountDownLatch latch = new CountDownLatch(1); - RemoteStateTestUtil.TestCapturingListener listener = new RemoteStateTestUtil.TestCapturingListener<>(); + TestCapturingListener listener = new TestCapturingListener<>(); remoteClusterStateAttributesManager.getAsyncMetadataReadAction( CLUSTER_BLOCKS, @@ -216,11 +215,11 @@ public void testGetAsyncMetadataReadAction_ClusterBlocks() throws IOException, I new LatchedActionListener<>(listener, latch) ).run(); latch.await(); - assertNull(listener.failure); - assertNotNull(listener.result); - assertEquals(CLUSTER_STATE_ATTRIBUTE, listener.result.getComponent()); - assertEquals(CLUSTER_BLOCKS, listener.result.getComponentName()); - ClusterBlocks readClusterBlocks = (ClusterBlocks) listener.result.getObj(); + assertNull(listener.getFailure()); + assertNotNull(listener.getResult()); + assertEquals(CLUSTER_STATE_ATTRIBUTE, listener.getResult().getComponent()); + assertEquals(CLUSTER_BLOCKS, listener.getResult().getComponentName()); + ClusterBlocks readClusterBlocks = (ClusterBlocks) listener.getResult().getObj(); assertEquals(clusterBlocks.global(), readClusterBlocks.global()); assertEquals(clusterBlocks.indices().keySet(), readClusterBlocks.indices().keySet()); for (String index : clusterBlocks.indices().keySet()) { @@ -243,8 +242,7 @@ public void testGetAsyncMetadataWriteAction_Custom() throws IOException, Interru return null; }).when(blobStoreTransferService) .uploadBlob(any(InputStream.class), anyIterable(), anyString(), eq(URGENT), any(ActionListener.class)); - final RemoteStateTestUtil.TestCapturingListener listener = - new RemoteStateTestUtil.TestCapturingListener<>(); + final TestCapturingListener listener = new TestCapturingListener<>(); final CountDownLatch latch = new CountDownLatch(1); remoteClusterStateAttributesManager.getAsyncMetadataWriteAction( CLUSTER_STATE_CUSTOM, @@ -252,10 +250,10 @@ public void testGetAsyncMetadataWriteAction_Custom() throws IOException, Interru new LatchedActionListener<>(listener, latch) ).run(); latch.await(); - assertNull(listener.failure); - assertNotNull(listener.result); - assertEquals(String.join(CUSTOM_DELIMITER, CLUSTER_STATE_CUSTOM, custom.getWriteableName()), listener.result.getComponent()); - String uploadedFileName = listener.result.getUploadedFilename(); + assertNull(listener.getFailure()); + assertNotNull(listener.getResult()); + assertEquals(String.join(CUSTOM_DELIMITER, CLUSTER_STATE_CUSTOM, custom.getWriteableName()), listener.getResult().getComponent()); + String uploadedFileName = listener.getResult().getUploadedFilename(); String[] pathTokens = uploadedFileName.split(PATH_DELIMITER); assertEquals(5, pathTokens.length); assertEquals(encodeString(CLUSTER_NAME), pathTokens[0]); @@ -282,7 +280,7 @@ public void testGetAsyncMetadataReadAction_Custom() throws IOException, Interrup when(blobStoreTransferService.downloadBlob(anyIterable(), anyString())).thenReturn( remoteClusterStateCustoms.clusterStateCustomsFormat.serialize(custom, fileName, compressor).streamInput() ); - RemoteStateTestUtil.TestCapturingListener capturingListener = new RemoteStateTestUtil.TestCapturingListener<>(); + TestCapturingListener capturingListener = new TestCapturingListener<>(); final CountDownLatch latch = new CountDownLatch(1); remoteClusterStateAttributesManager.getAsyncMetadataReadAction( CLUSTER_STATE_CUSTOM, @@ -290,11 +288,11 @@ public void testGetAsyncMetadataReadAction_Custom() throws IOException, Interrup new LatchedActionListener<>(capturingListener, latch) ).run(); latch.await(); - assertNull(capturingListener.failure); - assertNotNull(capturingListener.result); - assertEquals(custom, capturingListener.result.getObj()); - assertEquals(CLUSTER_STATE_ATTRIBUTE, capturingListener.result.getComponent()); - assertEquals(CLUSTER_STATE_CUSTOM, capturingListener.result.getComponentName()); + assertNull(capturingListener.getFailure()); + assertNotNull(capturingListener.getResult()); + assertEquals(custom, capturingListener.getResult().getObj()); + assertEquals(CLUSTER_STATE_ATTRIBUTE, capturingListener.getResult().getComponent()); + assertEquals(CLUSTER_STATE_CUSTOM, capturingListener.getResult().getComponentName()); } public void testGetAsyncMetadataWriteAction_Exception() throws IOException, InterruptedException { @@ -308,8 +306,7 @@ public void testGetAsyncMetadataWriteAction_Exception() throws IOException, Inte }).when(blobStoreTransferService) .uploadBlob(any(InputStream.class), anyIterable(), anyString(), eq(URGENT), any(ActionListener.class)); - RemoteStateTestUtil.TestCapturingListener capturingListener = - new RemoteStateTestUtil.TestCapturingListener<>(); + TestCapturingListener capturingListener = new TestCapturingListener<>(); final CountDownLatch latch = new CountDownLatch(1); remoteClusterStateAttributesManager.getAsyncMetadataWriteAction( DISCOVERY_NODES, @@ -317,8 +314,9 @@ public void testGetAsyncMetadataWriteAction_Exception() throws IOException, Inte new LatchedActionListener<>(capturingListener, latch) ).run(); latch.await(); - assertNull(capturingListener.result); - assertTrue(capturingListener.failure instanceof RemoteStateTransferException); + assertNull(capturingListener.getResult()); + assertTrue(capturingListener.getFailure() instanceof RemoteStateTransferException); + assertEquals(ioException, capturingListener.getFailure().getCause()); } public void testGetAsyncMetadataReadAction_Exception() throws IOException, InterruptedException { @@ -327,15 +325,15 @@ public void testGetAsyncMetadataReadAction_Exception() throws IOException, Inter Exception ioException = new IOException("mock test exception"); when(blobStoreTransferService.downloadBlob(anyIterable(), anyString())).thenThrow(ioException); CountDownLatch latch = new CountDownLatch(1); - RemoteStateTestUtil.TestCapturingListener capturingListener = new RemoteStateTestUtil.TestCapturingListener<>(); + TestCapturingListener capturingListener = new TestCapturingListener<>(); remoteClusterStateAttributesManager.getAsyncMetadataReadAction( DISCOVERY_NODES, remoteDiscoveryNodes, new LatchedActionListener<>(capturingListener, latch) ).run(); latch.await(); - assertNull(capturingListener.result); - assertEquals(ioException, capturingListener.failure); + assertNull(capturingListener.getResult()); + assertEquals(ioException, capturingListener.getFailure()); } public void testGetUpdatedCustoms() { diff --git a/server/src/test/java/org/opensearch/gateway/remote/RemoteStateTestUtil.java b/server/src/test/java/org/opensearch/gateway/remote/RemoteStateTestUtil.java deleted file mode 100644 index 50d17651602bf..0000000000000 --- a/server/src/test/java/org/opensearch/gateway/remote/RemoteStateTestUtil.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -package org.opensearch.gateway.remote; - -import org.opensearch.core.action.ActionListener; - -public class RemoteStateTestUtil { - public static class TestCapturingListener implements ActionListener { - T result; - Exception failure; - - @Override - public void onResponse(T result) { - this.result = result; - } - - @Override - public void onFailure(Exception e) { - this.failure = e; - } - } -}