Skip to content

Commit

Permalink
catch and rethrow exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Senic committed Mar 27, 2023
1 parent ece129f commit 93a3d21
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -297,7 +298,9 @@ public void concurrentDeletes() throws Exception {
""";
// start all threads
AtomicInteger reportedDeletions = new AtomicInteger(0);
AtomicReferenceArray<Exception> exceptions = new AtomicReferenceArray<>(threads);
for (int i = 0; i < threads; i++) {
int index = i;
new Thread(
() -> {
try {
Expand All @@ -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
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -326,7 +327,9 @@ public void concurrentDeletes() throws Exception {
""";
// start all threads
AtomicInteger reportedDeletions = new AtomicInteger(0);
AtomicReferenceArray<Exception> exceptions = new AtomicReferenceArray<>(threads);
for (int i = 0; i < threads; i++) {
int index = i;
new Thread(
() -> {
try {
Expand All @@ -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
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -496,7 +497,9 @@ public void concurrentUpdates() throws Exception {
}
""";
// start all threads
AtomicReferenceArray<Exception> exceptions = new AtomicReferenceArray<>(threads);
for (int i = 0; i < threads; i++) {
int index = i;
new Thread(
() -> {
try {
Expand All @@ -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
Expand All @@ -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 =
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1744,7 +1745,9 @@ public void concurrentUpdates() throws Exception {
}
""";
// start all threads
AtomicReferenceArray<Exception> exceptions = new AtomicReferenceArray<>(threads);
for (int i = 0; i < threads; i++) {
int index = i;
new Thread(
() -> {
try {
Expand All @@ -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
Expand All @@ -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 =
"""
Expand Down

0 comments on commit 93a3d21

Please sign in to comment.