Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #298: count down latch to be decrease on assertion errors #299

Merged
merged 2 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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