Clean up after test failure

If the test fails we properly clean up. Also add a toString
implementation so we get useful results on failure.
This commit is contained in:
Nik Everett 2016-06-14 16:14:57 -04:00
parent 2ebc5c342b
commit c8931768ba
2 changed files with 55 additions and 44 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.support.replication; package org.elasticsearch.action.support.replication;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -89,6 +90,11 @@ public class ReplicationTask extends Task {
out.writeString(phase); out.writeString(phase);
} }
@Override
public String toString() {
return Strings.toString(this);
}
// Implements equals and hashcode for testing // Implements equals and hashcode for testing
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {

View File

@ -338,55 +338,60 @@ public class TasksIT extends ESIntegTestCase {
*/ */
ReentrantLock taskFinishLock = new ReentrantLock(); ReentrantLock taskFinishLock = new ReentrantLock();
taskFinishLock.lock(); taskFinishLock.lock();
CountDownLatch taskRegistered = new CountDownLatch(1); ListenableActionFuture<?> indexFuture = null;
for (TransportService transportService : internalCluster().getInstances(TransportService.class)) { try {
((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() { CountDownLatch taskRegistered = new CountDownLatch(1);
@Override for (TransportService transportService : internalCluster().getInstances(TransportService.class)) {
public void onTaskRegistered(Task task) { ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() {
if (task.getAction().startsWith(IndexAction.NAME)) { @Override
taskRegistered.countDown(); public void onTaskRegistered(Task task) {
if (task.getAction().startsWith(IndexAction.NAME)) {
taskRegistered.countDown();
}
} }
}
@Override @Override
public void onTaskUnregistered(Task task) { public void onTaskUnregistered(Task task) {
/* /*
* We can't block all tasks here or the task listing task * We can't block all tasks here or the task listing task
* would never return. * would never return.
*/ */
if (false == task.getAction().startsWith(IndexAction.NAME)) { if (false == task.getAction().startsWith(IndexAction.NAME)) {
return; return;
}
logger.debug("Blocking {} from being unregistered", task);
taskFinishLock.lock();
taskFinishLock.unlock();
} }
logger.debug("Blocking {} from being unregistered", task); });
taskFinishLock.lock(); }
taskFinishLock.unlock(); indexFuture = client().prepareIndex("test", "test").setSource("test", "test").execute();
} taskRegistered.await(10, TimeUnit.SECONDS); // waiting for at least one task to be registered
});
}
ListenableActionFuture<?> indexFuture = client().prepareIndex("test", "test").setSource("test", "test").execute();
taskRegistered.await(10, TimeUnit.SECONDS); // waiting for at least one task to be registered
ListTasksResponse listResponse = client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*") ListTasksResponse listResponse = client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*")
.setDetailed(true).get(); .setDetailed(true).get();
assertThat(listResponse.getTasks(), not(empty())); assertThat(listResponse.getTasks(), not(empty()));
for (TaskInfo task : listResponse.getTasks()) { for (TaskInfo task : listResponse.getTasks()) {
assertNotNull(task.getStatus()); assertNotNull(task.getStatus());
GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get(); GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get();
assertFalse("task should still be running", getResponse.getTask().isCompleted()); assertFalse("task should still be running", getResponse.getTask().isCompleted());
TaskInfo fetchedWithGet = getResponse.getTask().getTask(); TaskInfo fetchedWithGet = getResponse.getTask().getTask();
assertEquals(task.getId(), fetchedWithGet.getId()); assertEquals(task.getId(), fetchedWithGet.getId());
assertEquals(task.getType(), fetchedWithGet.getType()); assertEquals(task.getType(), fetchedWithGet.getType());
assertEquals(task.getAction(), fetchedWithGet.getAction()); assertEquals(task.getAction(), fetchedWithGet.getAction());
assertEquals(task.getDescription(), fetchedWithGet.getDescription()); assertEquals(task.getDescription(), fetchedWithGet.getDescription());
assertEquals(task.getStatus(), fetchedWithGet.getStatus()); assertEquals(task.getStatus(), fetchedWithGet.getStatus());
assertEquals(task.getStartTime(), fetchedWithGet.getStartTime()); assertEquals(task.getStartTime(), fetchedWithGet.getStartTime());
assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos())); assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos()));
assertEquals(task.isCancellable(), fetchedWithGet.isCancellable()); assertEquals(task.isCancellable(), fetchedWithGet.isCancellable());
assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId()); assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId());
}
} finally {
taskFinishLock.unlock();
if (indexFuture != null) {
indexFuture.get();
}
} }
taskFinishLock.unlock();
indexFuture.get();
} }
public void testTasksCancellation() throws Exception { public void testTasksCancellation() throws Exception {