Cleanup TransportReplicationActionTests#runReplicateTest

This commit cleans up some of the assertions in
TransportReplicationActionTests#runReplicateTest:
 - use a Map to track actual vs. expected requests
 - assert that no request was sent to the local node
 - use RoutingTable#shardRoutingTable convenience method
 - explicitly use false in boolean conditions
 - clarify requests are expected on replica shards when assigned and
   execution on replicas is true
 - test ShardRouting equality when checking the failed shard request
This commit is contained in:
Jason Tedor 2016-01-06 11:40:07 -05:00
parent 6413adb5bc
commit c291c17142
1 changed files with 18 additions and 12 deletions

View File

@ -65,9 +65,9 @@ import org.junit.BeforeClass;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@ -76,10 +76,13 @@ import java.util.concurrent.atomic.AtomicInteger;
import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.state;
import static org.elasticsearch.action.support.replication.ClusterStateCreationUtils.stateWithStartedPrimary;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
@ -490,35 +493,38 @@ public class TransportReplicationActionTests extends ESTestCase {
transport.clear();
assertThat(capturedRequests.length, equalTo(assignedReplicas));
Set<String> nodesSentTo = new HashSet<>();
HashMap<String, Request> nodesSentTo = new HashMap<>();
boolean executeOnReplica =
action.shouldExecuteReplication(clusterService.state().getMetaData().index(shardId.getIndex()).getSettings());
for (CapturingTransport.CapturedRequest capturedRequest : capturedRequests) {
// no duplicate requests
assertTrue(nodesSentTo.add(capturedRequest.node.getId()));
Request replicationRequest = (Request) capturedRequest.request;
assertNull(nodesSentTo.put(capturedRequest.node.getId(), replicationRequest));
// the request is hitting the correct shard
Request replicationRequest = (Request)capturedRequest.request;
assertEquals(request.shardId, replicationRequest.shardId);
}
// no request was sent to the local node
assertThat(nodesSentTo.keySet(), not(hasItem(clusterService.state().getNodes().localNodeId())));
// requests were sent to the correct shard copies
List<ShardRouting> shards =
clusterService.state().getRoutingTable().index(shardId.getIndex()).shard(shardId.id()).shards();
for (ShardRouting shard : shards) {
if (!shard.primary() && !executeOnReplica) {
for (ShardRouting shard : clusterService.state().getRoutingTable().shardRoutingTable(shardId.getIndex(), shardId.id())) {
if (shard.primary() == false && executeOnReplica == false) {
continue;
}
if (shard.unassigned()) {
continue;
}
if (!clusterService.state().getNodes().localNodeId().equals(shard.currentNodeId())) {
assertThat(nodesSentTo, hasItem(shard.currentNodeId()));
if (shard.primary() == false) {
nodesSentTo.remove(shard.currentNodeId());
}
if (shard.relocating()) {
assertThat(nodesSentTo, hasItem(shard.relocatingNodeId()));
nodesSentTo.remove(shard.relocatingNodeId());
}
}
assertThat(nodesSentTo.entrySet(), is(empty()));
if (assignedReplicas > 0) {
assertThat("listener is done, but there are outstanding replicas", listener.isDone(), equalTo(false));
}
@ -548,7 +554,7 @@ public class TransportReplicationActionTests extends ESTestCase {
// and the shard that was requested to be failed
ShardStateAction.ShardRoutingEntry shardRoutingEntry = (ShardStateAction.ShardRoutingEntry)shardFailedRequest.request;
// the shard the request was sent to and the shard to be failed should be the same
assertTrue(shardRoutingEntry.getShardRouting().isSameAllocation(routing));
assertEquals(shardRoutingEntry.getShardRouting(), routing);
failures.add(shardFailedRequest);
transport.handleResponse(shardFailedRequest.requestId, TransportResponse.Empty.INSTANCE);
}