Skip to content

Commit

Permalink
closes #298: count down latch to be decrease on assertion errors (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Senic authored Mar 28, 2023
1 parent 39c07f7 commit 00b2475
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 70 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,37 +298,57 @@ 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(
() -> {
Integer deletedCount =
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(deleteJson)
.when()
.post(
CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body(
"status.deletedCount",
anyOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(totalDocuments)))
.body("errors", is(nullValue()))
.extract()
.path("status.deletedCount");

// add reported deletes
reportedDeletions.addAndGet(deletedCount);

// count down
latch.countDown();
try {
Integer deletedCount =
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(deleteJson)
.when()
.post(
CollectionResource.BASE_PATH,
keyspaceId.asInternal(),
collectionName)
.then()
.statusCode(200)
.body(
"status.deletedCount",
anyOf(greaterThanOrEqualTo(0), lessThanOrEqualTo(totalDocuments)))
.body("errors", is(nullValue()))
.extract()
.path("status.deletedCount");

// add reported deletes
reportedDeletions.addAndGet(deletedCount);
} catch (Exception e) {

// set exception so we can rethrow
exceptions.set(index, e);
} finally {

// count down
latch.countDown();
}
})
.start();
}

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,35 +327,55 @@ 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(
() -> {
Integer deletedCount =
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(deleteJson)
.when()
.post(
CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("status.deletedCount", anyOf(is(0), is(1)))
.body("errors", is(nullValue()))
.extract()
.path("status.deletedCount");

// add reported deletes
reportedDeletions.addAndGet(deletedCount);

// count down
latch.countDown();
try {
Integer deletedCount =
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(deleteJson)
.when()
.post(
CollectionResource.BASE_PATH,
keyspaceId.asInternal(),
collectionName)
.then()
.statusCode(200)
.body("status.deletedCount", anyOf(is(0), is(1)))
.body("errors", is(nullValue()))
.extract()
.path("status.deletedCount");

// add reported deletes
reportedDeletions.addAndGet(deletedCount);
} catch (Exception e) {

// set exception so we can rethrow
exceptions.set(index, e);
} finally {

// count down
latch.countDown();
}
})
.start();
}

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,29 +497,47 @@ 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(
() -> {
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(updateJson)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("status.matchedCount", is(5))
.body("status.modifiedCount", is(5))
.body("errors", is(nullValue()));

// count down
latch.countDown();
try {
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(updateJson)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.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
latch.countDown();
}
})
.start();
}

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,29 +1745,47 @@ 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(
() -> {
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(updateJson)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.body("status.matchedCount", is(1))
.body("status.modifiedCount", is(1))
.body("errors", is(nullValue()));

// count down
latch.countDown();
try {
given()
.header(HttpConstants.AUTHENTICATION_TOKEN_HEADER_NAME, getAuthToken())
.contentType(ContentType.JSON)
.body(updateJson)
.when()
.post(CollectionResource.BASE_PATH, keyspaceId.asInternal(), collectionName)
.then()
.statusCode(200)
.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
latch.countDown();
}
})
.start();
}

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 00b2475

Please sign in to comment.