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 d629d6b  Add coverage and fix a bug in concurrrent coroutine functions
     new 43fb3c5  Merge pull request #132 from atoulme/coroutine_coverage
d629d6b is described below

commit d629d6bb332a0a2dbc006a6e6f062920a420c689
Author: Antoine Toulme <[email protected]>
AuthorDate: Sat Jul 18 18:14:35 2020 -0700

    Add coverage and fix a bug in concurrrent coroutine functions
---
 .../concurrent/coroutines/AsyncCompletion.kt       |   6 +-
 .../concurrent/coroutines/AsyncCompletionTest.kt   | 134 +++++++++++++++++++++
 .../concurrent/coroutines/AsyncResultTest.kt       | 134 +++++++++++++++++++++
 3 files changed, 271 insertions(+), 3 deletions(-)

diff --git 
a/concurrent-coroutines/src/main/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletion.kt
 
b/concurrent-coroutines/src/main/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletion.kt
index 4969918..df81893 100644
--- 
a/concurrent-coroutines/src/main/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletion.kt
+++ 
b/concurrent-coroutines/src/main/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletion.kt
@@ -96,10 +96,10 @@ fun Deferred<Unit>.asAsyncCompletion(): AsyncCompletion {
   val asyncCompletion = AsyncCompletion.incomplete()
   asyncCompletion.whenComplete { cancel() }
   invokeOnCompletion {
-    try {
+    if (it != null) {
+      asyncCompletion.completeExceptionally(it)
+    } else {
       asyncCompletion.complete()
-    } catch (exception: Exception) {
-      asyncCompletion.completeExceptionally(exception)
     }
   }
   return asyncCompletion
diff --git 
a/concurrent-coroutines/src/test/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletionTest.kt
 
b/concurrent-coroutines/src/test/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletionTest.kt
new file mode 100644
index 0000000..246ff3d
--- /dev/null
+++ 
b/concurrent-coroutines/src/test/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncCompletionTest.kt
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuweni.concurrent.coroutines
+
+import kotlinx.coroutines.async
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.runBlocking
+import org.apache.tuweni.concurrent.AsyncCompletion
+import org.junit.jupiter.api.Assertions.assertFalse
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+
+class AsyncCompletionTest {
+
+  @Test
+  fun testCompleted() = runBlocking {
+    val completion = asyncCompletion {
+    }
+    completion.await()
+    assertTrue(completion.isDone)
+    assertFalse(completion.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testFailed() = runBlocking {
+    val completion = asyncCompletion {
+      throw IllegalArgumentException("foo")
+    }
+    assertThrows<IllegalArgumentException> {
+      runBlocking {
+        completion.await()
+      }
+    }
+    assertTrue(completion.isDone)
+    assertTrue(completion.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testCanceled() = runBlocking {
+    val completion = asyncCompletion {
+      delay(5000)
+    }
+    completion.cancel()
+    assertTrue(completion.isDone)
+    assertTrue(completion.isCancelled)
+  }
+
+  @Test
+  fun testDeferredAlreadyCompleted() = runBlocking {
+    val completion = asyncCompletion {
+    }
+    completion.await()
+    val deferred = completion.asDeferred()
+    assertTrue(deferred.isCompleted)
+    assertFalse(completion.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testDeferredCompleted() = runBlocking {
+    val completion = asyncCompletion {
+    }
+    val deferred = completion.asDeferred()
+    deferred.await()
+    assertTrue(completion.isDone)
+    assertFalse(completion.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testDeferredFailed() = runBlocking {
+    val completion = asyncCompletion {
+      throw IllegalArgumentException("foo")
+    }
+    val deferred = completion.asDeferred()
+    assertThrows<IllegalArgumentException> {
+      runBlocking {
+        deferred.await()
+      }
+    }
+    assertTrue(completion.isDone)
+    assertTrue(completion.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testDeferredCanceled() = runBlocking {
+    val completion = asyncCompletion {
+      delay(5000)
+    }
+    val deferred = completion.asDeferred()
+    deferred.cancel()
+    assertTrue(completion.isDone)
+    assertTrue(completion.isCancelled)
+  }
+
+  @Test
+  fun deferredToAsyncCompletion() = runBlocking {
+    val completion = async {
+    }.asAsyncCompletion()
+    completion.await()
+    assertTrue(completion.isDone)
+    assertFalse(completion.isCompletedExceptionally)
+  }
+
+  @Test
+  fun deferredFailedToAsyncCompletion() = runBlocking {
+    var completion: AsyncCompletion? = null
+    assertThrows<IllegalArgumentException> {
+      runBlocking {
+      completion = async {
+        delay(1000)
+        throw IllegalArgumentException("foo")
+      }.asAsyncCompletion()
+
+        completion!!.await()
+      }
+    }
+    assertTrue(completion!!.isDone)
+    assertTrue(completion!!.isCompletedExceptionally)
+  }
+}
diff --git 
a/concurrent-coroutines/src/test/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncResultTest.kt
 
b/concurrent-coroutines/src/test/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncResultTest.kt
new file mode 100644
index 0000000..21325a8
--- /dev/null
+++ 
b/concurrent-coroutines/src/test/kotlin/org/apache/tuweni/concurrent/coroutines/AsyncResultTest.kt
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuweni.concurrent.coroutines
+
+import kotlinx.coroutines.async
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.runBlocking
+import org.apache.tuweni.concurrent.AsyncResult
+import org.junit.jupiter.api.Assertions.assertFalse
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.assertThrows
+
+class AsyncResultTest {
+
+  @Test
+  fun testCompleted() = runBlocking {
+    val result = asyncResult {
+    }
+    result.await()
+    assertTrue(result.isDone)
+    assertFalse(result.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testFailed() = runBlocking {
+    val result = asyncResult<String> {
+      throw IllegalArgumentException("foo")
+    }
+    assertThrows<IllegalArgumentException> {
+      runBlocking {
+        result.await()
+      }
+    }
+    assertTrue(result.isDone)
+    assertTrue(result.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testCanceled() = runBlocking {
+    val result = asyncResult {
+      delay(5000)
+    }
+    result.cancel()
+    assertTrue(result.isDone)
+    assertTrue(result.isCancelled)
+  }
+
+  @Test
+  fun testDeferredAlreadyCompleted() = runBlocking {
+    val result = asyncResult {
+    }
+    result.await()
+    val deferred = result.asDeferred()
+    assertTrue(deferred.isCompleted)
+    assertFalse(result.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testDeferredCompleted() = runBlocking {
+    val result = asyncResult {
+    }
+    val deferred = result.asDeferred()
+    deferred.await()
+    assertTrue(result.isDone)
+    assertFalse(result.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testDeferredFailed() = runBlocking {
+    val result = asyncResult<String> {
+      throw IllegalArgumentException("foo")
+    }
+    val deferred = result.asDeferred()
+    assertThrows<IllegalArgumentException> {
+      runBlocking {
+        deferred.await()
+      }
+    }
+    assertTrue(result.isDone)
+    assertTrue(result.isCompletedExceptionally)
+  }
+
+  @Test
+  fun testDeferredCanceled() = runBlocking {
+    val result = asyncResult {
+      delay(5000)
+    }
+    val deferred = result.asDeferred()
+    deferred.cancel()
+    assertTrue(result.isDone)
+    assertTrue(result.isCancelled)
+  }
+
+  @Test
+  fun deferredToAsyncResult() = runBlocking {
+    val result = async {
+    }.asAsyncResult()
+    result.await()
+    assertTrue(result.isDone)
+    assertFalse(result.isCompletedExceptionally)
+  }
+
+  @Test
+  fun deferredFailedToAsyncResult() = runBlocking {
+    var result: AsyncResult<String>? = null
+    assertThrows<IllegalArgumentException> {
+      runBlocking {
+      result = async {
+        delay(1000)
+        throw IllegalArgumentException("foo")
+      }.asAsyncResult()
+
+        result!!.await()
+      }
+    }
+    assertTrue(result!!.isDone)
+    assertTrue(result!!.isCompletedExceptionally)
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to