From 93a3d21a313a97376b95b9d2a29f177d9132dba8 Mon Sep 17 00:00:00 2001 From: Ivan Senic Date: Mon, 27 Mar 2023 19:08:35 +0200 Subject: [PATCH] catch and rethrow exceptions --- .../api/v1/DeleteManyIntegrationTest.java | 16 ++++++++++++++++ .../jsonapi/api/v1/DeleteOneIntegrationTest.java | 16 ++++++++++++++++ .../api/v1/UpdateManyIntegrationTest.java | 16 ++++++++++++++++ .../jsonapi/api/v1/UpdateOneIntegrationTest.java | 16 ++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java index a4a8d74008..3113107ba3 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteManyIntegrationTest.java @@ -19,6 +19,7 @@ import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReferenceArray; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.RepeatedTest; @@ -297,7 +298,9 @@ public void concurrentDeletes() throws Exception { """; // start all threads AtomicInteger reportedDeletions = new AtomicInteger(0); + AtomicReferenceArray exceptions = new AtomicReferenceArray<>(threads); for (int i = 0; i < threads; i++) { + int index = i; new Thread( () -> { try { @@ -322,6 +325,10 @@ public void concurrentDeletes() throws Exception { // add reported deletes reportedDeletions.addAndGet(deletedCount); + } catch (Exception e) { + + // set exception so we can rethrow + exceptions.set(index, e); } finally { // count down @@ -333,6 +340,15 @@ public void concurrentDeletes() throws Exception { latch.await(); + // check if there are any exceptions + // throw first that is seen + for (int i = 0; i < threads; i++) { + Exception exception = exceptions.get(i); + if (null != exception) { + throw exception; + } + } + // assert reported deletes are exactly one assertThat(reportedDeletions.get()).isEqualTo(totalDocuments); diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java index 626f19de57..d1f6e34988 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/DeleteOneIntegrationTest.java @@ -16,6 +16,7 @@ import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReferenceArray; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; @@ -326,7 +327,9 @@ public void concurrentDeletes() throws Exception { """; // start all threads AtomicInteger reportedDeletions = new AtomicInteger(0); + AtomicReferenceArray exceptions = new AtomicReferenceArray<>(threads); for (int i = 0; i < threads; i++) { + int index = i; new Thread( () -> { try { @@ -349,6 +352,10 @@ public void concurrentDeletes() throws Exception { // add reported deletes reportedDeletions.addAndGet(deletedCount); + } catch (Exception e) { + + // set exception so we can rethrow + exceptions.set(index, e); } finally { // count down @@ -360,6 +367,15 @@ public void concurrentDeletes() throws Exception { latch.await(); + // check if there are any exceptions + // throw first that is seen + for (int i = 0; i < threads; i++) { + Exception exception = exceptions.get(i); + if (null != exception) { + throw exception; + } + } + // assert reported deletes are exactly one assertThat(reportedDeletions.get()).isOne(); diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateManyIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateManyIntegrationTest.java index 4ca34c0697..9061926a14 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateManyIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateManyIntegrationTest.java @@ -14,6 +14,7 @@ import io.stargate.sgv2.api.common.config.constants.HttpConstants; import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReferenceArray; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.RepeatedTest; @@ -496,7 +497,9 @@ public void concurrentUpdates() throws Exception { } """; // start all threads + AtomicReferenceArray exceptions = new AtomicReferenceArray<>(threads); for (int i = 0; i < threads; i++) { + int index = i; new Thread( () -> { try { @@ -511,6 +514,10 @@ public void concurrentUpdates() throws Exception { .body("status.matchedCount", is(5)) .body("status.modifiedCount", is(5)) .body("errors", is(nullValue())); + } catch (Exception e) { + + // set exception so we can rethrow + exceptions.set(index, e); } finally { // count down @@ -522,6 +529,15 @@ public void concurrentUpdates() throws Exception { latch.await(); + // check if there are any exceptions + // throw first that is seen + for (int i = 0; i < threads; i++) { + Exception exception = exceptions.get(i); + if (null != exception) { + throw exception; + } + } + // assert state after all updates String findJson = """ diff --git a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateOneIntegrationTest.java b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateOneIntegrationTest.java index 0d4516514d..2eb3f18343 100644 --- a/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateOneIntegrationTest.java +++ b/src/test/java/io/stargate/sgv2/jsonapi/api/v1/UpdateOneIntegrationTest.java @@ -13,6 +13,7 @@ import io.stargate.sgv2.api.common.config.constants.HttpConstants; import io.stargate.sgv2.jsonapi.testresource.DseTestResource; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicReferenceArray; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.RepeatedTest; @@ -1744,7 +1745,9 @@ public void concurrentUpdates() throws Exception { } """; // start all threads + AtomicReferenceArray exceptions = new AtomicReferenceArray<>(threads); for (int i = 0; i < threads; i++) { + int index = i; new Thread( () -> { try { @@ -1759,6 +1762,10 @@ public void concurrentUpdates() throws Exception { .body("status.matchedCount", is(1)) .body("status.modifiedCount", is(1)) .body("errors", is(nullValue())); + } catch (Exception e) { + + // set exception so we can rethrow + exceptions.set(index, e); } finally { // count down @@ -1770,6 +1777,15 @@ public void concurrentUpdates() throws Exception { latch.await(); + // check if there are any exceptions + // throw first that is seen + for (int i = 0; i < threads; i++) { + Exception exception = exceptions.get(i); + if (null != exception) { + throw exception; + } + } + // assert state after all updates String expectedDoc = """