Skip to content

Commit b03c8a4

Browse files
committed
move assertBusy to use CheckException (#25246)
We use assertBusy in many places where the underlying code throw exceptions. Currently we need to wrap those exceptions in a RuntimeException which is ugly.
1 parent b839a14 commit b03c8a4

27 files changed

+263
-416
lines changed

core/src/test/java/org/elasticsearch/action/bulk/BulkIntegrationIT.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ public void testBulkIndexCreatesMapping() throws Exception {
3434
BulkRequestBuilder bulkBuilder = client().prepareBulk();
3535
bulkBuilder.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, null, XContentType.JSON);
3636
bulkBuilder.get();
37-
assertBusy(new Runnable() {
38-
@Override
39-
public void run() {
40-
GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings().get();
41-
assertTrue(mappingsResponse.getMappings().containsKey("logstash-2014.03.30"));
42-
assertTrue(mappingsResponse.getMappings().get("logstash-2014.03.30").containsKey("logs"));
43-
}
37+
assertBusy(() -> {
38+
GetMappingsResponse mappingsResponse = client().admin().indices().prepareGetMappings().get();
39+
assertTrue(mappingsResponse.getMappings().containsKey("logstash-2014.03.30"));
40+
assertTrue(mappingsResponse.getMappings().get("logstash-2014.03.30").containsKey("logs"));
4441
});
4542
}
4643
}

core/src/test/java/org/elasticsearch/action/support/single/instance/TransportInstanceSingleOperationActionTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,7 @@ public void testRetryOfAnAlreadyTimedOutRequest() throws Exception {
275275
transport.handleLocalError(requestId, new ConnectTransportException(node, "test exception"));
276276

277277
// wait until the timeout was triggered and we actually tried to send for the second time
278-
assertBusy(new Runnable() {
279-
@Override
280-
public void run() {
281-
assertThat(transport.capturedRequests().length, equalTo(1));
282-
}
283-
});
278+
assertBusy(() -> assertThat(transport.capturedRequests().length, equalTo(1)));
284279

285280
// let it fail the second time too
286281
requestId = transport.capturedRequests()[0].requestId;

core/src/test/java/org/elasticsearch/cluster/MinimumMasterNodesIT.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.cluster.service.ClusterService;
2626
import org.elasticsearch.common.Priority;
2727
import org.elasticsearch.common.settings.Settings;
28-
import org.elasticsearch.common.unit.TimeValue;
2928
import org.elasticsearch.discovery.Discovery;
3029
import org.elasticsearch.discovery.DiscoverySettings;
3130
import org.elasticsearch.discovery.zen.ElectMasterService;
@@ -37,7 +36,6 @@
3736
import org.elasticsearch.test.ESIntegTestCase.Scope;
3837
import org.elasticsearch.test.discovery.TestZenDiscovery;
3938
import org.elasticsearch.test.disruption.NetworkDisruption;
40-
import org.elasticsearch.test.disruption.NetworkDisruption.NetworkDelay;
4139
import org.elasticsearch.test.disruption.NetworkDisruption.TwoPartitions;
4240
import org.elasticsearch.test.junit.annotations.TestLogging;
4341
import org.elasticsearch.test.transport.MockTransportService;
@@ -161,12 +159,9 @@ public void testSimpleMinimumMasterNodes() throws Exception {
161159
}
162160

163161
internalCluster().stopRandomNonMasterNode();
164-
assertBusy(new Runnable() {
165-
@Override
166-
public void run() {
167-
ClusterState state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
168-
assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(true));
169-
}
162+
assertBusy(() -> {
163+
ClusterState state1 = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
164+
assertThat(state1.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(true));
170165
});
171166

172167
logger.info("--> starting the previous master node again...");
@@ -408,12 +403,7 @@ public void onFailure(String source, Exception e) {
408403
latch.await();
409404

410405
assertThat(failure.get(), instanceOf(Discovery.FailedToCommitClusterStateException.class));
411-
assertBusy(new Runnable() {
412-
@Override
413-
public void run() {
414-
assertThat(masterClusterService.state().nodes().getMasterNode(), nullValue());
415-
}
416-
});
406+
assertBusy(() -> assertThat(masterClusterService.state().nodes().getMasterNode(), nullValue()));
417407

418408
partition.stopDisrupting();
419409

core/src/test/java/org/elasticsearch/cluster/routing/DelayedAllocationIT.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ public void testDelayedAllocationNodeLeavesAndComesBack() throws Exception {
7070
ensureGreen("test");
7171
indexRandomData();
7272
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(findNodeWithShard()));
73-
assertBusy(new Runnable() {
74-
@Override
75-
public void run() {
76-
assertThat(client().admin().cluster().prepareState().all().get().getState().getRoutingNodes().unassigned().size() > 0, equalTo(true));
77-
}
78-
});
73+
assertBusy(() -> assertThat(client().admin().cluster().prepareState().all().get().getState().getRoutingNodes().unassigned().size() > 0, equalTo(true)));
7974
assertThat(client().admin().cluster().prepareHealth().get().getDelayedUnassignedShards(), equalTo(1));
8075
internalCluster().startNode(); // this will use the same data location as the stopped node
8176
ensureGreen("test");
@@ -116,12 +111,7 @@ public void testDelayedAllocationChangeWithSettingTo100ms() throws Exception {
116111
ensureGreen("test");
117112
indexRandomData();
118113
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(findNodeWithShard()));
119-
assertBusy(new Runnable() {
120-
@Override
121-
public void run() {
122-
assertThat(client().admin().cluster().prepareState().all().get().getState().getRoutingNodes().unassigned().size() > 0, equalTo(true));
123-
}
124-
});
114+
assertBusy(() -> assertThat(client().admin().cluster().prepareState().all().get().getState().getRoutingNodes().unassigned().size() > 0, equalTo(true)));
125115
assertThat(client().admin().cluster().prepareHealth().get().getDelayedUnassignedShards(), equalTo(1));
126116
assertAcked(client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), TimeValue.timeValueMillis(100))).get());
127117
ensureGreen("test");

core/src/test/java/org/elasticsearch/cluster/routing/PrimaryAllocationIT.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ private void createStaleReplicaScenario() throws Exception {
120120
logger.info("--> check that old primary shard does not get promoted to primary again");
121121
// kick reroute and wait for all shard states to be fetched
122122
client(master).admin().cluster().prepareReroute().get();
123-
assertBusy(new Runnable() {
124-
@Override
125-
public void run() {
126-
assertThat(internalCluster().getInstance(GatewayAllocator.class, master).getNumberOfInFlightFetch(), equalTo(0));
127-
}
128-
});
123+
assertBusy(() -> assertThat(internalCluster().getInstance(GatewayAllocator.class, master).getNumberOfInFlightFetch(), equalTo(0)));
129124
// kick reroute a second time and check that all shards are unassigned
130125
assertThat(client(master).admin().cluster().prepareReroute().get().getState().getRoutingNodes().unassigned().size(), equalTo(2));
131126
}

core/src/test/java/org/elasticsearch/cluster/routing/allocation/ShardStateIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.cluster.ClusterState;
2222
import org.elasticsearch.cluster.health.ClusterHealthStatus;
2323
import org.elasticsearch.cluster.metadata.IndexMetaData;
24+
import org.elasticsearch.common.CheckedRunnable;
2425
import org.elasticsearch.index.IndexService;
2526
import org.elasticsearch.index.shard.IndexShard;
2627
import org.elasticsearch.indices.IndicesService;
@@ -50,7 +51,7 @@ public void testPrimaryFailureIncreasesTerm() throws Exception {
5051
logger.info("--> waiting for a yellow index");
5152
// JDK 9 type inference gets confused, so we have to help the
5253
// type inference
53-
assertBusy(((Runnable) () -> assertThat(client().admin().cluster().prepareHealth().get().getStatus(),
54+
assertBusy(((CheckedRunnable<Exception>) () -> assertThat(client().admin().cluster().prepareHealth().get().getStatus(),
5455
equalTo(ClusterHealthStatus.YELLOW))));
5556

5657
final long term0 = shard == 0 ? 2 : 1;

core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/MockDiskUsagesIT.java

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ public void testRerouteOccursOnDiskPassingHighWatermark() throws Exception {
5757
List<String> nodes = internalCluster().startNodes(3);
5858

5959
// Wait for all 3 nodes to be up
60-
assertBusy(new Runnable() {
61-
@Override
62-
public void run() {
63-
NodesStatsResponse resp = client().admin().cluster().prepareNodesStats().get();
64-
assertThat(resp.getNodes().size(), equalTo(3));
65-
}
60+
assertBusy(() -> {
61+
NodesStatsResponse resp = client().admin().cluster().prepareNodesStats().get();
62+
assertThat(resp.getNodes().size(), equalTo(3));
6663
});
6764

6865
// Start with all nodes at 50% usage
@@ -86,13 +83,10 @@ public void run() {
8683
ensureGreen("test");
8784

8885
// Block until the "fake" cluster info is retrieved at least once
89-
assertBusy(new Runnable() {
90-
@Override
91-
public void run() {
92-
ClusterInfo info = cis.getClusterInfo();
93-
logger.info("--> got: {} nodes", info.getNodeLeastAvailableDiskUsages().size());
94-
assertThat(info.getNodeLeastAvailableDiskUsages().size(), greaterThan(0));
95-
}
86+
assertBusy(() -> {
87+
ClusterInfo info = cis.getClusterInfo();
88+
logger.info("--> got: {} nodes", info.getNodeLeastAvailableDiskUsages().size());
89+
assertThat(info.getNodeLeastAvailableDiskUsages().size(), greaterThan(0));
9690
});
9791

9892
final List<String> realNodeNames = new ArrayList<>();
@@ -113,21 +107,18 @@ public void run() {
113107
// Retrieve the count of shards on each node
114108
final Map<String, Integer> nodesToShardCount = new HashMap<>();
115109

116-
assertBusy(new Runnable() {
117-
@Override
118-
public void run() {
119-
ClusterStateResponse resp = client().admin().cluster().prepareState().get();
120-
Iterator<RoutingNode> iter = resp.getState().getRoutingNodes().iterator();
121-
while (iter.hasNext()) {
122-
RoutingNode node = iter.next();
123-
logger.info("--> node {} has {} shards",
124-
node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
125-
nodesToShardCount.put(node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
126-
}
127-
assertThat("node1 has 5 shards", nodesToShardCount.get(realNodeNames.get(0)), equalTo(5));
128-
assertThat("node2 has 5 shards", nodesToShardCount.get(realNodeNames.get(1)), equalTo(5));
129-
assertThat("node3 has 0 shards", nodesToShardCount.get(realNodeNames.get(2)), equalTo(0));
110+
assertBusy(() -> {
111+
ClusterStateResponse resp12 = client().admin().cluster().prepareState().get();
112+
Iterator<RoutingNode> iter12 = resp12.getState().getRoutingNodes().iterator();
113+
while (iter12.hasNext()) {
114+
RoutingNode node = iter12.next();
115+
logger.info("--> node {} has {} shards",
116+
node.nodeId(), resp12.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
117+
nodesToShardCount.put(node.nodeId(), resp12.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
130118
}
119+
assertThat("node1 has 5 shards", nodesToShardCount.get(realNodeNames.get(0)), equalTo(5));
120+
assertThat("node2 has 5 shards", nodesToShardCount.get(realNodeNames.get(1)), equalTo(5));
121+
assertThat("node3 has 0 shards", nodesToShardCount.get(realNodeNames.get(2)), equalTo(0));
131122
});
132123

133124
// Update the disk usages so one node is now back under the high watermark
@@ -138,21 +129,18 @@ public void run() {
138129
// Retrieve the count of shards on each node
139130
nodesToShardCount.clear();
140131

141-
assertBusy(new Runnable() {
142-
@Override
143-
public void run() {
144-
ClusterStateResponse resp = client().admin().cluster().prepareState().get();
145-
Iterator<RoutingNode> iter = resp.getState().getRoutingNodes().iterator();
146-
while (iter.hasNext()) {
147-
RoutingNode node = iter.next();
148-
logger.info("--> node {} has {} shards",
149-
node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
150-
nodesToShardCount.put(node.nodeId(), resp.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
151-
}
152-
assertThat("node1 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(0)), greaterThanOrEqualTo(3));
153-
assertThat("node2 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(1)), greaterThanOrEqualTo(3));
154-
assertThat("node3 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(2)), greaterThanOrEqualTo(3));
132+
assertBusy(() -> {
133+
ClusterStateResponse resp1 = client().admin().cluster().prepareState().get();
134+
Iterator<RoutingNode> iter1 = resp1.getState().getRoutingNodes().iterator();
135+
while (iter1.hasNext()) {
136+
RoutingNode node = iter1.next();
137+
logger.info("--> node {} has {} shards",
138+
node.nodeId(), resp1.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
139+
nodesToShardCount.put(node.nodeId(), resp1.getState().getRoutingNodes().node(node.nodeId()).numberOfOwningShards());
155140
}
141+
assertThat("node1 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(0)), greaterThanOrEqualTo(3));
142+
assertThat("node2 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(1)), greaterThanOrEqualTo(3));
143+
assertThat("node3 has at least 3 shards", nodesToShardCount.get(realNodeNames.get(2)), greaterThanOrEqualTo(3));
156144
});
157145
}
158146
}

core/src/test/java/org/elasticsearch/common/util/concurrent/EsExecutorsTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,9 @@ public void run() {
229229
assertThat("wrong pool size", pool.getPoolSize(), equalTo(max));
230230
assertThat("wrong active size", pool.getActiveCount(), equalTo(max));
231231
barrier.await();
232-
assertBusy(new Runnable() {
233-
@Override
234-
public void run() {
235-
assertThat("wrong active count", pool.getActiveCount(), equalTo(0));
236-
assertThat("idle threads didn't shrink below max. (" + pool.getPoolSize() + ")", pool.getPoolSize(), lessThan(max));
237-
}
232+
assertBusy(() -> {
233+
assertThat("wrong active count", pool.getActiveCount(), equalTo(0));
234+
assertThat("idle threads didn't shrink below max. (" + pool.getPoolSize() + ")", pool.getPoolSize(), lessThan(max));
238235
});
239236
terminate(pool);
240237
}

core/src/test/java/org/elasticsearch/common/util/concurrent/PrioritizedExecutorsTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,7 @@ public void run() {
264264

265265
// the timeout handler is added post execution (and quickly cancelled). We have allow for this
266266
// and use assert busy
267-
assertBusy(new Runnable() {
268-
@Override
269-
public void run() {
270-
assertThat(timer.getQueue().size(), equalTo(0));
271-
}
272-
}, 5, TimeUnit.SECONDS);
267+
assertBusy(() -> assertThat(timer.getQueue().size(), equalTo(0)), 5, TimeUnit.SECONDS);
273268
assertThat(timeoutCalled.get(), equalTo(false));
274269
assertTrue(terminate(executor));
275270
assertTrue(terminate(threadPool));

core/src/test/java/org/elasticsearch/discovery/DiscoveryWithServiceDisruptionsIT.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,34 +1324,28 @@ private void assertNoMaster(final String node, TimeValue maxWaitTime) throws Exc
13241324
}
13251325

13261326
private void assertNoMaster(final String node, @Nullable final ClusterBlock expectedBlocks, TimeValue maxWaitTime) throws Exception {
1327-
assertBusy(new Runnable() {
1328-
@Override
1329-
public void run() {
1330-
ClusterState state = getNodeClusterState(node);
1331-
assertNull("node [" + node + "] still has [" + state.nodes().getMasterNode() + "] as master", state.nodes().getMasterNode());
1332-
if (expectedBlocks != null) {
1333-
for (ClusterBlockLevel level : expectedBlocks.levels()) {
1334-
assertTrue("node [" + node + "] does have level [" + level + "] in it's blocks", state.getBlocks().hasGlobalBlock
1335-
(level));
1336-
}
1327+
assertBusy(() -> {
1328+
ClusterState state = getNodeClusterState(node);
1329+
assertNull("node [" + node + "] still has [" + state.nodes().getMasterNode() + "] as master", state.nodes().getMasterNode());
1330+
if (expectedBlocks != null) {
1331+
for (ClusterBlockLevel level : expectedBlocks.levels()) {
1332+
assertTrue("node [" + node + "] does have level [" + level + "] in it's blocks", state.getBlocks().hasGlobalBlock
1333+
(level));
13371334
}
13381335
}
13391336
}, maxWaitTime.getMillis(), TimeUnit.MILLISECONDS);
13401337
}
13411338

13421339
private void assertDifferentMaster(final String node, final String oldMasterNode) throws Exception {
1343-
assertBusy(new Runnable() {
1344-
@Override
1345-
public void run() {
1346-
ClusterState state = getNodeClusterState(node);
1347-
String masterNode = null;
1348-
if (state.nodes().getMasterNode() != null) {
1349-
masterNode = state.nodes().getMasterNode().getName();
1350-
}
1351-
logger.trace("[{}] master is [{}]", node, state.nodes().getMasterNode());
1352-
assertThat("node [" + node + "] still has [" + masterNode + "] as master",
1353-
oldMasterNode, not(equalTo(masterNode)));
1340+
assertBusy(() -> {
1341+
ClusterState state = getNodeClusterState(node);
1342+
String masterNode = null;
1343+
if (state.nodes().getMasterNode() != null) {
1344+
masterNode = state.nodes().getMasterNode().getName();
13541345
}
1346+
logger.trace("[{}] master is [{}]", node, state.nodes().getMasterNode());
1347+
assertThat("node [" + node + "] still has [" + masterNode + "] as master",
1348+
oldMasterNode, not(equalTo(masterNode)));
13551349
}, 10, TimeUnit.SECONDS);
13561350
}
13571351

0 commit comments

Comments
 (0)