Skip to content

Commit fc2f125

Browse files
committed
Refactor test initialisation
1 parent 759ad07 commit fc2f125

File tree

2 files changed

+71
-115
lines changed

2 files changed

+71
-115
lines changed

server/src/test/java/org/elasticsearch/action/admin/cluster/bootstrap/TransportBootstrapClusterActionTests.java

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.cluster.coordination.InMemoryPersistedState;
2929
import org.elasticsearch.cluster.coordination.NoOpClusterApplier;
3030
import org.elasticsearch.cluster.node.DiscoveryNode;
31-
import org.elasticsearch.cluster.node.DiscoveryNode.Role;
3231
import org.elasticsearch.cluster.service.MasterService;
3332
import org.elasticsearch.common.io.stream.StreamInput;
3433
import org.elasticsearch.common.settings.ClusterSettings;
@@ -42,6 +41,8 @@
4241
import org.elasticsearch.transport.TransportException;
4342
import org.elasticsearch.transport.TransportResponseHandler;
4443
import org.elasticsearch.transport.TransportService;
44+
import org.junit.After;
45+
import org.junit.Before;
4546

4647
import java.io.IOException;
4748
import java.util.Random;
@@ -51,7 +52,6 @@
5152
import static java.util.Collections.emptyList;
5253
import static java.util.Collections.emptyMap;
5354
import static java.util.Collections.emptySet;
54-
import static java.util.Collections.singleton;
5555
import static java.util.Collections.singletonList;
5656
import static org.hamcrest.Matchers.equalTo;
5757
import static org.mockito.Mockito.mock;
@@ -60,18 +60,37 @@
6060
public class TransportBootstrapClusterActionTests extends ESTestCase {
6161

6262
private final ActionFilters EMPTY_FILTERS = new ActionFilters(emptySet());
63+
private DiscoveryNode discoveryNode;
64+
private ThreadPool threadPool;
65+
private TransportService transportService;
66+
private Coordinator coordinator;
6367

6468
private static BootstrapClusterRequest exampleRequest() {
6569
return new BootstrapClusterRequest(new BootstrapConfiguration(singletonList(new NodeDescription("id", "name"))));
6670
}
6771

68-
public void testHandlesNonstandardDiscoveryImplementation() throws InterruptedException {
72+
@Before
73+
public void setupTest() {
74+
discoveryNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
6975
final MockTransport transport = new MockTransport();
70-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
71-
final DiscoveryNode discoveryNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
72-
final TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool,
76+
threadPool = new TestThreadPool("test", Settings.EMPTY);
77+
transportService = transport.createTransportService(Settings.EMPTY, threadPool,
7378
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> discoveryNode, null, emptySet());
7479

80+
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
81+
coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
82+
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
83+
new MasterService("local", Settings.EMPTY, threadPool),
84+
() -> new InMemoryPersistedState(0, ClusterState.builder(new ClusterName("cluster")).build()), r -> emptyList(),
85+
new NoOpClusterApplier(), new Random(random().nextLong()));
86+
}
87+
88+
@After
89+
public void cleanUp() {
90+
threadPool.shutdown();
91+
}
92+
93+
public void testHandlesNonstandardDiscoveryImplementation() throws InterruptedException {
7594
final Discovery discovery = mock(Discovery.class);
7695
verifyZeroInteractions(discovery);
7796

@@ -94,25 +113,11 @@ public void handleException(TransportException exp) {
94113
});
95114

96115
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
97-
threadPool.shutdown();
98116
}
99117

100118
public void testFailsOnNonMasterEligibleNodes() throws InterruptedException {
101-
final DiscoveryNode discoveryNode
102-
= new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
103-
104-
final MockTransport transport = new MockTransport();
105-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
106-
final TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool,
107-
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> discoveryNode, null, emptySet());
108-
109-
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
110-
final ClusterState state = ClusterState.builder(new ClusterName("cluster")).build();
111-
final Coordinator coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
112-
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
113-
new MasterService("local", Settings.EMPTY, threadPool),
114-
() -> new InMemoryPersistedState(0, state), r -> emptyList(),
115-
new NoOpClusterApplier(), new Random(random().nextLong()));
119+
discoveryNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
120+
// transport service only picks up local node when started, so we can change it here ^
116121

117122
new TransportBootstrapClusterAction(Settings.EMPTY, EMPTY_FILTERS, transportService, coordinator); // registers action
118123
transportService.start();
@@ -134,26 +139,9 @@ public void handleException(TransportException exp) {
134139
});
135140

136141
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
137-
threadPool.shutdown();
138142
}
139143

140144
public void testSetsInitialConfiguration() throws InterruptedException {
141-
final DiscoveryNode discoveryNode
142-
= new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), singleton(Role.MASTER), Version.CURRENT);
143-
144-
final MockTransport transport = new MockTransport();
145-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
146-
final TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool,
147-
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> discoveryNode, null, emptySet());
148-
149-
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
150-
final ClusterState state = ClusterState.builder(new ClusterName("cluster")).build();
151-
final Coordinator coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
152-
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
153-
new MasterService("local", Settings.EMPTY, threadPool),
154-
() -> new InMemoryPersistedState(0, state), r -> emptyList(),
155-
new NoOpClusterApplier(), new Random(random().nextLong()));
156-
157145
new TransportBootstrapClusterAction(Settings.EMPTY, EMPTY_FILTERS, transportService, coordinator); // registers action
158146
transportService.start();
159147
transportService.acceptIncomingRequests();

server/src/test/java/org/elasticsearch/action/admin/cluster/bootstrap/TransportGetDiscoveredNodesActionTests.java

Lines changed: 44 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.elasticsearch.cluster.coordination.NoOpClusterApplier;
2929
import org.elasticsearch.cluster.coordination.PeersResponse;
3030
import org.elasticsearch.cluster.node.DiscoveryNode;
31-
import org.elasticsearch.cluster.node.DiscoveryNode.Role;
3231
import org.elasticsearch.cluster.service.MasterService;
3332
import org.elasticsearch.common.io.stream.StreamInput;
3433
import org.elasticsearch.common.settings.ClusterSettings;
@@ -37,7 +36,6 @@
3736
import org.elasticsearch.discovery.Discovery;
3837
import org.elasticsearch.discovery.PeersRequest;
3938
import org.elasticsearch.test.ESTestCase;
40-
import org.elasticsearch.test.junit.annotations.TestLogging;
4139
import org.elasticsearch.test.transport.MockTransport;
4240
import org.elasticsearch.threadpool.TestThreadPool;
4341
import org.elasticsearch.threadpool.ThreadPool;
@@ -47,6 +45,8 @@
4745
import org.elasticsearch.transport.TransportResponseHandler;
4846
import org.elasticsearch.transport.TransportService;
4947
import org.elasticsearch.transport.TransportService.HandshakeResponse;
48+
import org.junit.After;
49+
import org.junit.Before;
5050

5151
import java.io.IOException;
5252
import java.util.Random;
@@ -56,7 +56,6 @@
5656
import static java.util.Collections.emptyList;
5757
import static java.util.Collections.emptyMap;
5858
import static java.util.Collections.emptySet;
59-
import static java.util.Collections.singleton;
6059
import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING;
6160
import static org.elasticsearch.discovery.PeerFinder.REQUEST_PEERS_ACTION_NAME;
6261
import static org.elasticsearch.transport.TransportService.HANDSHAKE_ACTION_NAME;
@@ -69,14 +68,46 @@
6968
public class TransportGetDiscoveredNodesActionTests extends ESTestCase {
7069

7170
private final ActionFilters EMPTY_FILTERS = new ActionFilters(emptySet());
71+
private DiscoveryNode localNode;
72+
private ThreadPool threadPool;
73+
private String clusterName;
74+
private TransportService transportService;
75+
private Coordinator coordinator;
76+
private DiscoveryNode otherNode;
77+
78+
@Before
79+
public void setupTest() {
80+
clusterName = randomAlphaOfLength(10);
81+
localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
82+
otherNode = new DiscoveryNode("other", buildNewFakeTransportAddress(), Version.CURRENT);
7283

73-
public void testHandlesNonstandardDiscoveryImplementation() throws InterruptedException {
74-
final MockTransport transport = new MockTransport();
75-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
76-
final DiscoveryNode discoveryNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
77-
final TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool,
78-
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> discoveryNode, null, emptySet());
84+
final MockTransport transport = new MockTransport() {
85+
@Override
86+
protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) {
87+
if (action.equals(HANDSHAKE_ACTION_NAME) && node.getAddress().equals(otherNode.getAddress())) {
88+
handleResponse(requestId, new HandshakeResponse(otherNode, new ClusterName(clusterName), Version.CURRENT));
89+
}
90+
}
91+
};
92+
threadPool = new TestThreadPool("test", Settings.EMPTY);
93+
transportService = transport.createTransportService(
94+
Settings.builder().put(CLUSTER_NAME_SETTING.getKey(), clusterName).build(), threadPool,
95+
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> localNode, null, emptySet());
7996

97+
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
98+
coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
99+
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
100+
new MasterService("local", Settings.EMPTY, threadPool),
101+
() -> new InMemoryPersistedState(0, ClusterState.builder(new ClusterName(clusterName)).build()), r -> emptyList(),
102+
new NoOpClusterApplier(), new Random(random().nextLong()));
103+
}
104+
105+
@After
106+
public void cleanUp() {
107+
threadPool.shutdown();
108+
}
109+
110+
public void testHandlesNonstandardDiscoveryImplementation() throws InterruptedException {
80111
final Discovery discovery = mock(Discovery.class);
81112
verifyZeroInteractions(discovery);
82113

@@ -86,7 +117,7 @@ public void testHandlesNonstandardDiscoveryImplementation() throws InterruptedEx
86117
transportService.acceptIncomingRequests();
87118

88119
final CountDownLatch countDownLatch = new CountDownLatch(1);
89-
transportService.sendRequest(discoveryNode, GetDiscoveredNodesAction.NAME, new GetDiscoveredNodesRequest(), new ResponseHandler() {
120+
transportService.sendRequest(localNode, GetDiscoveredNodesAction.NAME, new GetDiscoveredNodesRequest(), new ResponseHandler() {
90121
@Override
91122
public void handleResponse(GetDiscoveredNodesResponse response) {
92123
throw new AssertionError("should not be called");
@@ -100,34 +131,19 @@ public void handleException(TransportException exp) {
100131
});
101132

102133
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
103-
threadPool.shutdown();
104134
}
105135

106136
public void testFailsOnNonMasterEligibleNodes() throws InterruptedException {
107-
final DiscoveryNode discoveryNode
108-
= new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
109-
110-
final MockTransport transport = new MockTransport();
111-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
112-
final TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool,
113-
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> discoveryNode, null, emptySet());
114-
115-
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
116-
final ClusterState state = ClusterState.builder(new ClusterName("cluster")).build();
117-
final Coordinator coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
118-
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
119-
new MasterService("local", Settings.EMPTY, threadPool),
120-
() -> new InMemoryPersistedState(0, state), r -> emptyList(),
121-
new NoOpClusterApplier(), new Random(random().nextLong()));
137+
localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
138+
// transport service only picks up local node when started, so we can change it here ^
122139

123140
new TransportGetDiscoveredNodesAction(Settings.EMPTY, EMPTY_FILTERS, transportService, coordinator); // registers action
124141
transportService.start();
125142
transportService.acceptIncomingRequests();
126143
coordinator.start();
127144

128-
129145
final CountDownLatch countDownLatch = new CountDownLatch(1);
130-
transportService.sendRequest(discoveryNode, GetDiscoveredNodesAction.NAME, new GetDiscoveredNodesRequest(), new ResponseHandler() {
146+
transportService.sendRequest(localNode, GetDiscoveredNodesAction.NAME, new GetDiscoveredNodesRequest(), new ResponseHandler() {
131147
@Override
132148
public void handleResponse(GetDiscoveredNodesResponse response) {
133149
throw new AssertionError("should not be called");
@@ -141,26 +157,9 @@ public void handleException(TransportException exp) {
141157
});
142158

143159
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
144-
threadPool.shutdown();
145160
}
146161

147162
public void testFailsQuicklyWithZeroTimeout() throws InterruptedException {
148-
final DiscoveryNode localNode
149-
= new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), singleton(Role.MASTER), Version.CURRENT);
150-
151-
final MockTransport transport = new MockTransport();
152-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
153-
final TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool,
154-
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> localNode, null, emptySet());
155-
156-
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
157-
final ClusterState state = ClusterState.builder(new ClusterName("cluster")).build();
158-
final Coordinator coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
159-
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
160-
new MasterService("local", Settings.EMPTY, threadPool),
161-
() -> new InMemoryPersistedState(0, state), r -> emptyList(),
162-
new NoOpClusterApplier(), new Random(random().nextLong()));
163-
164163
new TransportGetDiscoveredNodesAction(Settings.EMPTY, EMPTY_FILTERS, transportService, coordinator); // registers action
165164
transportService.start();
166165
transportService.acceptIncomingRequests();
@@ -186,38 +185,9 @@ public void handleException(TransportException exp) {
186185
});
187186

188187
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
189-
threadPool.shutdown();
190188
}
191189

192-
@TestLogging("org.elasticsearch.cluster.coordination:TRACE")
193190
public void testGetsDiscoveredNodes() throws InterruptedException {
194-
final DiscoveryNode localNode
195-
= new DiscoveryNode("local", buildNewFakeTransportAddress(), emptyMap(), singleton(Role.MASTER), Version.CURRENT);
196-
final DiscoveryNode otherNode
197-
= new DiscoveryNode("other", buildNewFakeTransportAddress(), emptyMap(), singleton(Role.MASTER), Version.CURRENT);
198-
final String clusterName = randomAlphaOfLength(10);
199-
200-
final MockTransport transport = new MockTransport() {
201-
@Override
202-
protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) {
203-
if (action.equals(HANDSHAKE_ACTION_NAME) && node.getAddress().equals(otherNode.getAddress())) {
204-
handleResponse(requestId, new HandshakeResponse(otherNode, new ClusterName(clusterName), Version.CURRENT));
205-
}
206-
}
207-
};
208-
final ThreadPool threadPool = new TestThreadPool("test", Settings.EMPTY);
209-
final TransportService transportService = transport.createTransportService(
210-
Settings.builder().put(CLUSTER_NAME_SETTING.getKey(), clusterName).build(), threadPool,
211-
TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> localNode, null, emptySet());
212-
213-
final ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
214-
final ClusterState state = ClusterState.builder(new ClusterName(clusterName)).build();
215-
final Coordinator coordinator = new Coordinator("local", Settings.EMPTY, clusterSettings, transportService,
216-
ESAllocationTestCase.createAllocationService(Settings.EMPTY),
217-
new MasterService("local", Settings.EMPTY, threadPool),
218-
() -> new InMemoryPersistedState(0, state), r -> emptyList(),
219-
new NoOpClusterApplier(), new Random(random().nextLong()));
220-
221191
new TransportGetDiscoveredNodesAction(Settings.EMPTY, EMPTY_FILTERS, transportService, coordinator); // registers action
222192
transportService.start();
223193
transportService.acceptIncomingRequests();
@@ -286,8 +256,6 @@ public void handleException(TransportException exp) {
286256

287257
assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
288258
}
289-
290-
threadPool.shutdown();
291259
}
292260

293261
private abstract class ResponseHandler implements TransportResponseHandler<GetDiscoveredNodesResponse> {

0 commit comments

Comments
 (0)