You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ro...@apache.org on 2019/10/02 15:48:03 UTC

[james-project] 03/04: JAMES-2813 wrap all lambda as task into a MemoryReferenceTask in test

This is an automated email from the ASF dual-hosted git repository.

rouazana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 983a56d8cf005193166aa758e4226400f87bd203
Author: RĂ©mi KOWALSKI <rk...@linagora.com>
AuthorDate: Wed Oct 2 10:06:36 2019 +0200

    JAMES-2813 wrap all lambda as task into a MemoryReferenceTask in test
---
 server/container/cli-integration/pom.xml           |  7 ++-
 .../james/cli/ReindexCommandIntegrationTest.java   |  5 ++-
 server/protocols/webadmin/webadmin-core/pom.xml    |  6 +++
 .../james/webadmin/routes/TasksRoutesTest.java     | 51 +++++++++++-----------
 .../org/apache/james/task/MemoryReferenceTask.java | 14 ++++--
 .../james/task/SerialTaskManagerWorkerTest.java    |  8 ++--
 .../java/org/apache/james/task/TaskWithIdTest.java |  6 +--
 .../EventSourcingTaskManagerTest.java              |  9 ++--
 8 files changed, 64 insertions(+), 42 deletions(-)

diff --git a/server/container/cli-integration/pom.xml b/server/container/cli-integration/pom.xml
index 5fdaaed..b78c433 100644
--- a/server/container/cli-integration/pom.xml
+++ b/server/container/cli-integration/pom.xml
@@ -65,7 +65,12 @@
             <artifactId>james-server-memory-guice</artifactId>
             <scope>test</scope>
         </dependency>
-
+        <dependency>
+            <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-task-api</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
             <artifactId>testing-base</artifactId>
diff --git a/server/container/cli-integration/src/test/java/org/apache/james/cli/ReindexCommandIntegrationTest.java b/server/container/cli-integration/src/test/java/org/apache/james/cli/ReindexCommandIntegrationTest.java
index 6d81781..c1ff23f 100644
--- a/server/container/cli-integration/src/test/java/org/apache/james/cli/ReindexCommandIntegrationTest.java
+++ b/server/container/cli-integration/src/test/java/org/apache/james/cli/ReindexCommandIntegrationTest.java
@@ -31,6 +31,7 @@ import org.apache.james.mailbox.model.MailboxConstants;
 import org.apache.james.mailbox.model.MailboxPath;
 import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
 import org.apache.james.modules.server.JMXServerModule;
+import org.apache.james.task.MemoryReferenceTask;
 import org.apache.james.task.Task;
 import org.junit.After;
 import org.junit.Before;
@@ -50,8 +51,8 @@ public class ReindexCommandIntegrationTest {
     @Before
     public void setUp() throws Exception {
         reIndexer = mock(ReIndexer.class);
-        when(reIndexer.reIndex()).thenReturn(() -> Task.Result.COMPLETED);
-        when(reIndexer.reIndex(any(MailboxPath.class))).thenReturn(() -> Task.Result.COMPLETED);
+        when(reIndexer.reIndex()).thenReturn(new MemoryReferenceTask(() -> Task.Result.COMPLETED));
+        when(reIndexer.reIndex(any(MailboxPath.class))).thenReturn(new MemoryReferenceTask(() -> Task.Result.COMPLETED));
         guiceJamesServer = memoryJmap.jmapServer(new JMXServerModule(),
             binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class)))
             .overrideWith(binder -> binder.bind(ReIndexer.class)
diff --git a/server/protocols/webadmin/webadmin-core/pom.xml b/server/protocols/webadmin/webadmin-core/pom.xml
index 4c39d4e..606ec07 100644
--- a/server/protocols/webadmin/webadmin-core/pom.xml
+++ b/server/protocols/webadmin/webadmin-core/pom.xml
@@ -59,6 +59,12 @@
         </dependency>
         <dependency>
             <groupId>${james.groupId}</groupId>
+            <artifactId>james-server-task-api</artifactId>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>${james.groupId}</groupId>
             <artifactId>james-server-task-memory</artifactId>
             <scope>test</scope>
         </dependency>
diff --git a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/TasksRoutesTest.java b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/TasksRoutesTest.java
index b119037..c34baa4 100644
--- a/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/TasksRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-core/src/test/java/org/apache/james/webadmin/routes/TasksRoutesTest.java
@@ -34,6 +34,7 @@ import java.util.UUID;
 import java.util.concurrent.CountDownLatch;
 
 import org.apache.james.task.Hostname;
+import org.apache.james.task.MemoryReferenceTask;
 import org.apache.james.task.MemoryTaskManager;
 import org.apache.james.task.Task;
 import org.apache.james.task.TaskId;
@@ -87,11 +88,11 @@ class TasksRoutesTest {
     @Test
     void listShouldReturnTaskDetailsWhenTaskInProgress() throws Exception {
         CountDownLatch taskInProgressLatch = new CountDownLatch(1);
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             taskInProgressLatch.countDown();
             waitForResult();
             return Task.Result.COMPLETED;
-        });
+        }));
 
         taskInProgressLatch.await();
 
@@ -123,11 +124,11 @@ class TasksRoutesTest {
     @Test
     void listShouldListTaskWhenStatusFilter() throws Exception {
         CountDownLatch inProgressLatch = new CountDownLatch(1);
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             inProgressLatch.countDown();
             waitForResult();
             return Task.Result.COMPLETED;
-        });
+        }));
 
         inProgressLatch.await();
 
@@ -140,17 +141,17 @@ class TasksRoutesTest {
             .body("", hasSize(1))
             .body("[0].status", is(TaskManager.Status.IN_PROGRESS.getValue()))
             .body("[0].taskId", is(taskId.asString()))
-            .body("[0].type", is(Task.UNKNOWN.asString()));
+            .body("[0].type", is(MemoryReferenceTask.TYPE.asString()));
     }
 
     @Test
     void listShouldReturnEmptyWhenNonMatchingStatusFilter() throws Exception {
         CountDownLatch inProgressLatch = new CountDownLatch(1);
-        taskManager.submit(() -> {
+        taskManager.submit(new MemoryReferenceTask(() -> {
             inProgressLatch.countDown();
             waitForResult();
             return Task.Result.COMPLETED;
-        });
+        }));
 
         inProgressLatch.await();
 
@@ -166,11 +167,11 @@ class TasksRoutesTest {
     @Test
     void getShouldReturnTaskDetails() throws Exception {
         CountDownLatch inProgressLatch = new CountDownLatch(1);
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             inProgressLatch.countDown();
             waitForResult();
             return Task.Result.COMPLETED;
-        });
+        }));
 
         inProgressLatch.await();
 
@@ -183,14 +184,14 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitShouldAwaitTaskCompletion() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             try {
                 Thread.sleep(100);
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
             return Task.Result.COMPLETED;
-        });
+        }));
 
         when()
             .get("/" + taskId.getValue() + "/await")
@@ -201,14 +202,14 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitWithTimeoutShouldAwaitTaskCompletion() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
             return Task.Result.COMPLETED;
-        });
+        }));
 
         given()
             .queryParam("timeout", "2s")
@@ -221,7 +222,7 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitWithInvalidTimeoutShouldReturnAnError() {
-        TaskId taskId = taskManager.submit(() -> Task.Result.COMPLETED);
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> Task.Result.COMPLETED));
 
         given()
             .queryParam("timeout", "-5")
@@ -235,7 +236,7 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitWithATooBigTimeoutShouldReturnAnError() {
-        TaskId taskId = taskManager.submit(() -> Task.Result.COMPLETED);
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> Task.Result.COMPLETED));
 
         given()
             .queryParam("timeout", "5y")
@@ -249,14 +250,14 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitWithAShorterTimeoutShouldReturnTimeout() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             try {
                 Thread.sleep(10000);
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
             return Task.Result.COMPLETED;
-        });
+        }));
 
         given()
             .queryParam("timeout", "1")
@@ -279,9 +280,9 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitShouldNotFailUponError() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             throw new RuntimeException();
-        });
+        }));
 
         when()
             .get("/" + taskId.getValue() + "/await")
@@ -292,14 +293,14 @@ class TasksRoutesTest {
 
     @Test
     void getAwaitShouldNotFailUponFutureError() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             try {
                 Thread.sleep(200);
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
             throw new RuntimeException();
-        });
+        }));
 
         when()
             .get("/" + taskId.getValue() + "/await")
@@ -310,10 +311,10 @@ class TasksRoutesTest {
 
     @Test
     void deleteShouldReturnOk() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             waitForResult();
             return Task.Result.COMPLETED;
-        });
+        }));
 
         when()
             .delete("/" + taskId.getValue())
@@ -325,14 +326,14 @@ class TasksRoutesTest {
     void deleteShouldCancelMatchingTask() {
         CountDownLatch inProgressLatch = new CountDownLatch(1);
 
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             try {
                 inProgressLatch.await();
             } catch (InterruptedException e) {
                 //ignore
             }
             return Task.Result.COMPLETED;
-        });
+        }));
 
         with()
             .delete("/" + taskId.getValue());
diff --git a/server/task/task-api/src/test/java/org/apache/james/task/MemoryReferenceTask.java b/server/task/task-api/src/test/java/org/apache/james/task/MemoryReferenceTask.java
index 6ce1499..b00a53e 100644
--- a/server/task/task-api/src/test/java/org/apache/james/task/MemoryReferenceTask.java
+++ b/server/task/task-api/src/test/java/org/apache/james/task/MemoryReferenceTask.java
@@ -20,17 +20,25 @@ package org.apache.james.task;
 
 import java.util.Optional;
 
+import org.junit.jupiter.api.function.ThrowingSupplier;
+
 public class MemoryReferenceTask implements Task {
     public static final TaskType TYPE = TaskType.of("memory-reference-task");
-    private final Task task;
+    private final ThrowingSupplier<Result> task;
 
-    public MemoryReferenceTask(Task task) {
+    public MemoryReferenceTask(ThrowingSupplier<Result> task) {
         this.task = task;
     }
 
     @Override
     public Result run() throws InterruptedException {
-        return task.run();
+        try {
+            return task.get();
+        } catch (InterruptedException e) {
+            throw e;
+        } catch (Throwable throwable) {
+            throw new RuntimeException(throwable);
+        }
     }
 
     @Override
diff --git a/server/task/task-memory/src/test/java/org/apache/james/task/SerialTaskManagerWorkerTest.java b/server/task/task-memory/src/test/java/org/apache/james/task/SerialTaskManagerWorkerTest.java
index 522ed0c..a0779b4 100644
--- a/server/task/task-memory/src/test/java/org/apache/james/task/SerialTaskManagerWorkerTest.java
+++ b/server/task/task-memory/src/test/java/org/apache/james/task/SerialTaskManagerWorkerTest.java
@@ -94,11 +94,11 @@ class SerialTaskManagerWorkerTest {
         CountDownLatch latch = new CountDownLatch(1);
         CountDownLatch taskLaunched = new CountDownLatch(1);
 
-        Task inProgressTask = () -> {
+        Task inProgressTask = new MemoryReferenceTask(() -> {
             taskLaunched.countDown();
             await(latch);
             return Task.Result.COMPLETED;
-        };
+        });
 
         TaskWithId taskWithId = new TaskWithId(id, inProgressTask);
 
@@ -116,11 +116,11 @@ class SerialTaskManagerWorkerTest {
         AtomicInteger counter = new AtomicInteger(0);
         CountDownLatch latch = new CountDownLatch(1);
 
-        Task inProgressTask = () -> {
+        Task inProgressTask = new MemoryReferenceTask(() -> {
             await(latch);
             counter.incrementAndGet();
             return Task.Result.COMPLETED;
-        };
+        });
 
         TaskWithId taskWithId = new TaskWithId(id, inProgressTask);
 
diff --git a/server/task/task-memory/src/test/java/org/apache/james/task/TaskWithIdTest.java b/server/task/task-memory/src/test/java/org/apache/james/task/TaskWithIdTest.java
index a0a1ca0..40efa89 100644
--- a/server/task/task-memory/src/test/java/org/apache/james/task/TaskWithIdTest.java
+++ b/server/task/task-memory/src/test/java/org/apache/james/task/TaskWithIdTest.java
@@ -28,8 +28,8 @@ public class TaskWithIdTest {
     @Test
     public void twoTasksWithSameIdShouldBeEqual() {
         TaskId id = TaskId.generateTaskId();
-        Task task1 = () -> Task.Result.COMPLETED;
-        Task task2 = () -> Task.Result.COMPLETED;
+        Task task1 = new MemoryReferenceTask(() -> Task.Result.COMPLETED);
+        Task task2 = new MemoryReferenceTask(() -> Task.Result.COMPLETED);
         TaskWithId taskWithId1 = new TaskWithId(id, task1);
         TaskWithId taskWithId2 = new TaskWithId(id, task2);
         assertThat(taskWithId1).isEqualTo(taskWithId2);
@@ -39,7 +39,7 @@ public class TaskWithIdTest {
     public void sameTaskWithDifferentIdShouldNotBeEqual() {
         TaskId id1 = TaskId.generateTaskId();
         TaskId id2 = TaskId.generateTaskId();
-        Task task = () -> Task.Result.COMPLETED;
+        Task task = new MemoryReferenceTask(() -> Task.Result.COMPLETED);
         TaskWithId taskWithId1 = new TaskWithId(id1, task);
         TaskWithId taskWithId2 = new TaskWithId(id2, task);
         assertThat(taskWithId1).isNotEqualTo(taskWithId2);
diff --git a/server/task/task-memory/src/test/java/org/apache/james/task/eventsourcing/EventSourcingTaskManagerTest.java b/server/task/task-memory/src/test/java/org/apache/james/task/eventsourcing/EventSourcingTaskManagerTest.java
index 891c346..d3d9653 100644
--- a/server/task/task-memory/src/test/java/org/apache/james/task/eventsourcing/EventSourcingTaskManagerTest.java
+++ b/server/task/task-memory/src/test/java/org/apache/james/task/eventsourcing/EventSourcingTaskManagerTest.java
@@ -26,6 +26,7 @@ import org.apache.james.eventsourcing.eventstore.EventStore;
 import org.apache.james.eventsourcing.eventstore.memory.InMemoryEventStore;
 import org.apache.james.task.CountDownLatchExtension;
 import org.apache.james.task.Hostname;
+import org.apache.james.task.MemoryReferenceTask;
 import org.apache.james.task.MemoryWorkQueue;
 import org.apache.james.task.SerialTaskManagerWorker;
 import org.apache.james.task.Task;
@@ -75,7 +76,7 @@ class EventSourcingTaskManagerTest implements TaskManagerContract {
 
     @Test
     void createdTaskShouldKeepOriginHostname() {
-        TaskId taskId = taskManager.submit(() -> Task.Result.COMPLETED);
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> Task.Result.COMPLETED));
         TaskAggregateId aggregateId = new TaskAggregateId(taskId);
         assertThat(eventStore.getEventsOfAggregate(aggregateId).getEvents())
                 .filteredOn(event -> event instanceof Created)
@@ -85,7 +86,7 @@ class EventSourcingTaskManagerTest implements TaskManagerContract {
 
     @Test
     void startedTaskShouldKeepOriginHostname() {
-        TaskId taskId = taskManager.submit(() -> Task.Result.COMPLETED);
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> Task.Result.COMPLETED));
         TaskAggregateId aggregateId = new TaskAggregateId(taskId);
 
         CALMLY_AWAIT.untilAsserted(() ->
@@ -97,10 +98,10 @@ class EventSourcingTaskManagerTest implements TaskManagerContract {
 
     @Test
     void cancelRequestedTaskShouldKeepOriginHostname() {
-        TaskId taskId = taskManager.submit(() -> {
+        TaskId taskId = taskManager.submit(new MemoryReferenceTask(() -> {
             Thread.sleep(100);
             return Task.Result.COMPLETED;
-        });
+        }));
         taskManager.cancel(taskId);
 
         TaskAggregateId aggregateId = new TaskAggregateId(taskId);


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org