From c8931768ba0307d611f95ed2703e183d0704ba9c Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 14 Jun 2016 16:14:57 -0400 Subject: [PATCH] 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. --- .../support/replication/ReplicationTask.java | 6 ++ .../admin/cluster/node/tasks/TasksIT.java | 93 ++++++++++--------- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/support/replication/ReplicationTask.java b/core/src/main/java/org/elasticsearch/action/support/replication/ReplicationTask.java index b31c2439aaf..2e0baa057b2 100644 --- a/core/src/main/java/org/elasticsearch/action/support/replication/ReplicationTask.java +++ b/core/src/main/java/org/elasticsearch/action/support/replication/ReplicationTask.java @@ -19,6 +19,7 @@ package org.elasticsearch.action.support.replication; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -89,6 +90,11 @@ public class ReplicationTask extends Task { out.writeString(phase); } + @Override + public String toString() { + return Strings.toString(this); + } + // Implements equals and hashcode for testing @Override public boolean equals(Object obj) { diff --git a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java index 88c7f1ae210..ebb93ee7f21 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java +++ b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java @@ -338,55 +338,60 @@ public class TasksIT extends ESIntegTestCase { */ ReentrantLock taskFinishLock = new ReentrantLock(); taskFinishLock.lock(); - CountDownLatch taskRegistered = new CountDownLatch(1); - for (TransportService transportService : internalCluster().getInstances(TransportService.class)) { - ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() { - @Override - public void onTaskRegistered(Task task) { - if (task.getAction().startsWith(IndexAction.NAME)) { - taskRegistered.countDown(); + ListenableActionFuture indexFuture = null; + try { + CountDownLatch taskRegistered = new CountDownLatch(1); + for (TransportService transportService : internalCluster().getInstances(TransportService.class)) { + ((MockTaskManager) transportService.getTaskManager()).addListener(new MockTaskManagerListener() { + @Override + public void onTaskRegistered(Task task) { + if (task.getAction().startsWith(IndexAction.NAME)) { + taskRegistered.countDown(); + } } - } - @Override - public void onTaskUnregistered(Task task) { - /* - * We can't block all tasks here or the task listing task - * would never return. - */ - if (false == task.getAction().startsWith(IndexAction.NAME)) { - return; + @Override + public void onTaskUnregistered(Task task) { + /* + * We can't block all tasks here or the task listing task + * would never return. + */ + if (false == task.getAction().startsWith(IndexAction.NAME)) { + return; + } + logger.debug("Blocking {} from being unregistered", task); + taskFinishLock.lock(); + taskFinishLock.unlock(); } - logger.debug("Blocking {} from being unregistered", task); - taskFinishLock.lock(); - taskFinishLock.unlock(); - } - }); - } - ListenableActionFuture indexFuture = client().prepareIndex("test", "test").setSource("test", "test").execute(); - taskRegistered.await(10, TimeUnit.SECONDS); // waiting for at least one task to be registered + }); + } + 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*") - .setDetailed(true).get(); - assertThat(listResponse.getTasks(), not(empty())); - for (TaskInfo task : listResponse.getTasks()) { - assertNotNull(task.getStatus()); - GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get(); - assertFalse("task should still be running", getResponse.getTask().isCompleted()); - TaskInfo fetchedWithGet = getResponse.getTask().getTask(); - assertEquals(task.getId(), fetchedWithGet.getId()); - assertEquals(task.getType(), fetchedWithGet.getType()); - assertEquals(task.getAction(), fetchedWithGet.getAction()); - assertEquals(task.getDescription(), fetchedWithGet.getDescription()); - assertEquals(task.getStatus(), fetchedWithGet.getStatus()); - assertEquals(task.getStartTime(), fetchedWithGet.getStartTime()); - assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos())); - assertEquals(task.isCancellable(), fetchedWithGet.isCancellable()); - assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId()); + ListTasksResponse listResponse = client().admin().cluster().prepareListTasks().setActions("indices:data/write/index*") + .setDetailed(true).get(); + assertThat(listResponse.getTasks(), not(empty())); + for (TaskInfo task : listResponse.getTasks()) { + assertNotNull(task.getStatus()); + GetTaskResponse getResponse = client().admin().cluster().prepareGetTask(task.getTaskId()).get(); + assertFalse("task should still be running", getResponse.getTask().isCompleted()); + TaskInfo fetchedWithGet = getResponse.getTask().getTask(); + assertEquals(task.getId(), fetchedWithGet.getId()); + assertEquals(task.getType(), fetchedWithGet.getType()); + assertEquals(task.getAction(), fetchedWithGet.getAction()); + assertEquals(task.getDescription(), fetchedWithGet.getDescription()); + assertEquals(task.getStatus(), fetchedWithGet.getStatus()); + assertEquals(task.getStartTime(), fetchedWithGet.getStartTime()); + assertThat(fetchedWithGet.getRunningTimeNanos(), greaterThanOrEqualTo(task.getRunningTimeNanos())); + assertEquals(task.isCancellable(), fetchedWithGet.isCancellable()); + assertEquals(task.getParentTaskId(), fetchedWithGet.getParentTaskId()); + } + } finally { + taskFinishLock.unlock(); + if (indexFuture != null) { + indexFuture.get(); + } } - - taskFinishLock.unlock(); - indexFuture.get(); } public void testTasksCancellation() throws Exception {