You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/06/27 22:23:15 UTC

[incubator-tuweni] branch master updated: Add more coverage of concurrent methods

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

toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/master by this push:
     new 23724c6  Add more coverage of concurrent methods
     new 4a9105c  Merge pull request #104 from atoulme/async_result_coverage
23724c6 is described below

commit 23724c623a603a5c8d95081375aaa54644ee32ab
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Sat Jun 27 15:10:34 2020 -0700

    Add more coverage of concurrent methods
---
 concurrent/build.gradle                            |  2 +
 .../DefaultCompletableAsyncCompletionTest.java     | 85 ++++++++++++++++++++++
 .../DefaultCompletableAsyncResultTest.java         | 52 +++++++++++++
 3 files changed, 139 insertions(+)

diff --git a/concurrent/build.gradle b/concurrent/build.gradle
index 4b38ec8..01ddd3f 100644
--- a/concurrent/build.gradle
+++ b/concurrent/build.gradle
@@ -16,9 +16,11 @@ dependencies {
   compile 'com.google.guava:guava'
   compileOnly 'io.vertx:vertx-core'
 
+  testCompile project(':junit')
   testCompile 'org.junit.jupiter:junit-jupiter-api'
   testCompile 'org.junit.jupiter:junit-jupiter-params'
   testCompile 'org.assertj:assertj-core'
+  testCompile 'io.vertx:vertx-core'
 
   testRuntime 'org.junit.jupiter:junit-jupiter-engine'
 }
diff --git a/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncCompletionTest.java b/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncCompletionTest.java
index ff67695..c86378d 100644
--- a/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncCompletionTest.java
+++ b/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncCompletionTest.java
@@ -14,16 +14,26 @@ package org.apache.tuweni.concurrent;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.tuweni.junit.VertxExtension;
+import org.apache.tuweni.junit.VertxInstance;
 
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.CompletionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
+import io.vertx.core.Vertx;
+import io.vertx.core.WorkerExecutor;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 
+@ExtendWith(VertxExtension.class)
 class DefaultCompletableAsyncCompletionTest {
 
   @Test
@@ -236,6 +246,21 @@ class DefaultCompletableAsyncCompletionTest {
   }
 
   @Test
+  void completesWhenAllInStreamComplete() {
+    CompletableAsyncCompletion completion1 = AsyncCompletion.incomplete();
+    CompletableAsyncCompletion completion2 = AsyncCompletion.incomplete();
+    Collection<AsyncCompletion> list = Arrays.asList(completion1, completion2);
+
+    AsyncCompletion completion = AsyncCompletion.allOf(list.stream());
+    assertThat(completion.isDone()).isFalse();
+
+    completion1.complete();
+    assertThat(completion.isDone()).isFalse();
+    completion2.complete();
+    assertThat(completion.isDone()).isTrue();
+  }
+
+  @Test
   void completesWithExceptionWhenAnyInCollectionFail() throws Exception {
     CompletableAsyncCompletion completion1 = AsyncCompletion.incomplete();
     CompletableAsyncCompletion completion2 = AsyncCompletion.incomplete();
@@ -271,6 +296,66 @@ class DefaultCompletableAsyncCompletionTest {
     assertThat(completedThrowable.get()).isInstanceOf(CancellationException.class);
   }
 
+  @Test
+  void testExecutingBlocking() throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    AsyncCompletion completion = AsyncCompletion.executeBlocking(() -> executed.set(true));
+    completion.join();
+    assertTrue(executed.get());
+  }
+
+  @Test
+  void testExecutingBlocking(@VertxInstance Vertx vertx) throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    AsyncCompletion completion = AsyncCompletion.executeBlocking(vertx, () -> executed.set(true));
+    completion.join();
+    assertTrue(executed.get());
+  }
+
+  @Test
+  void testRunOnContextSupplier(@VertxInstance Vertx vertx) throws InterruptedException {
+    AsyncCompletion completion = AsyncCompletion.runOnContext(vertx, AsyncCompletion::completed);
+    completion.join();
+  }
+
+  @Test
+  void testRunOnContext(@VertxInstance Vertx vertx) throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    AsyncCompletion completion = AsyncCompletion.runOnContext(vertx, () -> executed.set(true));
+    completion.join();
+    assertTrue(executed.get());
+  }
+
+  @Test
+  void testRunOnWorker(@VertxInstance Vertx vertx) throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    WorkerExecutor executor = vertx.createSharedWorkerExecutor("foo");
+    AsyncCompletion completion = AsyncCompletion.executeBlocking(executor, () -> executed.set(true));
+    completion.join();
+    assertTrue(executed.get());
+  }
+
+  @Test
+  void testRunOnContextWithCompletion(@VertxInstance Vertx vertx) throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    AsyncCompletion completion = AsyncCompletion.runOnContext(vertx, () -> {
+      executed.set(true);
+      return AsyncCompletion.completed();
+    });
+    completion.join();
+    assertTrue(executed.get());
+  }
+
+  @Test
+  void testRunOnExecutor() throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    ExecutorService service = Executors.newSingleThreadExecutor();
+    AsyncCompletion completion = AsyncCompletion.executeBlocking(service, () -> executed.set(true));
+    completion.join();
+    assertTrue(executed.get());
+    service.shutdown();
+  }
+
   private void assertCompletedWithException(AsyncCompletion completion, Exception exception) throws Exception {
     try {
       completion.join();
diff --git a/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncResultTest.java b/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncResultTest.java
index 5d8b135..f82a73b 100644
--- a/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncResultTest.java
+++ b/concurrent/src/test/java/org/apache/tuweni/concurrent/DefaultCompletableAsyncResultTest.java
@@ -14,17 +14,27 @@ package org.apache.tuweni.concurrent;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 
+import org.apache.tuweni.junit.VertxExtension;
+import org.apache.tuweni.junit.VertxInstance;
+
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.concurrent.CancellationException;
 import java.util.concurrent.CompletionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicReference;
 
+import io.vertx.core.Vertx;
+import io.vertx.core.WorkerExecutor;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 
+@ExtendWith(VertxExtension.class)
 class DefaultCompletableAsyncResultTest {
 
   @Test
@@ -188,6 +198,48 @@ class DefaultCompletableAsyncResultTest {
     assertThat(completedThrowable.get()).isInstanceOf(CancellationException.class);
   }
 
+
+  @Test
+  void testExecutingBlocking() throws InterruptedException {
+    AsyncResult<String> result = AsyncResult.executeBlocking(() -> "foo");
+    assertEquals("foo", result.get());
+  }
+
+  @Test
+  void testExecutingBlocking(@VertxInstance Vertx vertx) throws InterruptedException {
+    AsyncResult<String> result = AsyncResult.executeBlocking(vertx, () -> "foo");
+    assertEquals("foo", result.get());
+  }
+
+  @Test
+  void testRunOnContext(@VertxInstance Vertx vertx) throws InterruptedException {
+    AsyncResult<String> result = AsyncResult.runOnContext(vertx, () -> AsyncResult.completed("foo"));
+    assertEquals("foo", result.get());
+  }
+
+  @Test
+  void testRunOnExecutor() throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    ExecutorService service = Executors.newSingleThreadExecutor();
+    AsyncResult<String> result = AsyncResult.executeBlocking(service, () -> "foo");
+    assertEquals("foo", result.get());
+    service.shutdown();
+  }
+
+  @Test
+  void testRunOnWorker(@VertxInstance Vertx vertx) throws InterruptedException {
+    AtomicReference<Boolean> executed = new AtomicReference<>();
+    WorkerExecutor executor = vertx.createSharedWorkerExecutor("foo");
+    AsyncResult<String> result = AsyncResult.executeBlocking(executor, () -> "foo");
+    assertEquals("foo", result.get());
+  }
+
+  @Test
+  void testRunOnContextWithCompletion(@VertxInstance Vertx vertx) throws InterruptedException {
+    AsyncResult<String> result = AsyncResult.runOnContext(vertx, () -> AsyncResult.completed("foo"));
+    assertEquals("foo", result.get());
+  }
+
   private void assertCompletedWithException(AsyncResult<?> asyncResult, Exception exception) throws Exception {
     try {
       asyncResult.get();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org