Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb.internal.event;

import com.mongodb.annotations.ThreadSafe;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;
Expand All @@ -29,7 +30,11 @@
import static com.mongodb.assertions.Assertions.isTrue;
import static java.lang.String.format;


/**
* This {@link CommandListener} is {@linkplain ThreadSafe thread-safe},
* provided that the recipient listeners passed to {@link #CommandListenerMulticaster(List)} are.
*/
@ThreadSafe
final class CommandListenerMulticaster implements CommandListener {
private static final Logger LOGGER = Loggers.getLogger("protocol.event");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.internal.connection;

import com.mongodb.MongoTimeoutException;
import com.mongodb.annotations.ThreadSafe;
import com.mongodb.client.TestListener;
import com.mongodb.event.CommandEvent;
import com.mongodb.event.CommandFailedEvent;
Expand Down Expand Up @@ -56,6 +57,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

@ThreadSafe
public class TestCommandListener implements CommandListener {
private final List<String> eventTypes;
private final List<String> ignoredCommandMonitoringEvents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.ContextProvider;
import com.mongodb.RequestContext;
import com.mongodb.WriteConcern;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;
Expand Down Expand Up @@ -186,6 +187,7 @@ public void contextShouldBeAvailableInCommandEvents() {
}
}

@NotThreadSafe
private static final class TestCommandListener implements CommandListener {
private int numCommandStartedEventsWithExpectedContext;
private int numCommandSucceededEventsWithExpectedContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,27 @@
import org.bson.Document;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static com.mongodb.client.model.Filters.eq;

/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests">Retryable Reads Tests</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#prose-tests">
* Prose Tests</a>.
*/
final class RetryableReadsProseTest {
/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests#poolclearederror-retryability-test">
* PoolClearedError Retryability Test</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#1-poolclearederror-retryability-test">
* 1. PoolClearedError Retryability Test</a>.
*/
@Test
void poolClearedExceptionMustBeRetryable() throws InterruptedException, ExecutionException, TimeoutException {
void poolClearedExceptionMustBeRetryable() throws Exception {
RetryableWritesProseTest.poolClearedExceptionMustBeRetryable(
SyncMongoClient::new,
mongoCollection -> mongoCollection.find(eq(0)).iterator().hasNext(), "find", false);
}

/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests#21-retryable-reads-are-retried-on-a-different-mongos-when-one-is-available">
* Retryable Reads Are Retried on a Different mongos When One is Available</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#21-retryable-reads-are-retried-on-a-different-mongos-when-one-is-available">
* 2.1 Retryable Reads Are Retried on a Different mongos When One is Available</a>.
*/
@Test
void retriesOnDifferentMongosWhenAvailable() {
Expand All @@ -61,9 +56,8 @@ void retriesOnDifferentMongosWhenAvailable() {
}

/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests#22-retryable-reads-are-retried-on-the-same-mongos-when-no-others-are-available">
* Retryable Reads Are Retried on the Same mongos When No Others are Available</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#22-retryable-reads-are-retried-on-the-same-mongos-when-no-others-are-available">
* 2.2 Retryable Reads Are Retried on the Same mongos When No Others are Available</a>.
*/
@Test
void retriesOnSameMongosWhenAnotherNotAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,87 @@

package com.mongodb.reactivestreams.client;

import com.mongodb.client.test.CollectionHelper;
import com.mongodb.reactivestreams.client.syncadapter.SyncMongoClient;
import org.bson.Document;
import org.bson.codecs.DocumentCodec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
* See
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#prose-tests">Retryable Write Prose Tests</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#prose-tests">
* Prose Tests</a>.
*/
public class RetryableWritesProseTest extends DatabaseTestCase {
private CollectionHelper<Document> collectionHelper;

@BeforeEach
@Override
public void setUp() {
super.setUp();

collectionHelper = new CollectionHelper<>(new DocumentCodec(), collection.getNamespace());
collectionHelper.create();
}

final class RetryableWritesProseTest {
/**
* Prose test #2.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#2-test-that-drivers-properly-retry-after-encountering-poolclearederrors">
* 2. Test that drivers properly retry after encountering PoolClearedErrors</a>.
*/
@Test
public void poolClearedExceptionMustBeRetryable() throws InterruptedException, ExecutionException, TimeoutException {
void poolClearedExceptionMustBeRetryable() throws Exception {
com.mongodb.client.RetryableWritesProseTest.poolClearedExceptionMustBeRetryable(
SyncMongoClient::new,
mongoCollection -> mongoCollection.insertOne(new Document()), "insert", true);
}

/**
* Prose test #3.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#3-test-that-drivers-return-the-original-error-after-encountering-a-writeconcernerror-with-a-retryablewriteerror-label">
* 3. Test that drivers return the original error after encountering a WriteConcernError with a RetryableWriteError label</a>.
*/
@Test
public void originalErrorMustBePropagatedIfNoWritesPerformed() throws InterruptedException {
void originalErrorMustBePropagatedIfNoWritesPerformed() throws Exception {
com.mongodb.client.RetryableWritesProseTest.originalErrorMustBePropagatedIfNoWritesPerformed(
SyncMongoClient::new);
}

/**
* Prose test #4.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#4-test-that-in-a-sharded-cluster-writes-are-retried-on-a-different-mongos-when-one-is-available">
* 4. Test that in a sharded cluster writes are retried on a different mongos when one is available</a>.
*/
@Test
public void retriesOnDifferentMongosWhenAvailable() {
void retriesOnDifferentMongosWhenAvailable() {
com.mongodb.client.RetryableWritesProseTest.retriesOnDifferentMongosWhenAvailable(
SyncMongoClient::new,
mongoCollection -> mongoCollection.insertOne(new Document()), "insert", true);
}

/**
* Prose test #5.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#5-test-that-in-a-sharded-cluster-writes-are-retried-on-the-same-mongos-when-no-others-are-available">
* 5. Test that in a sharded cluster writes are retried on the same mongos when no others are available</a>.
*/
@Test
public void retriesOnSameMongosWhenAnotherNotAvailable() {
void retriesOnSameMongosWhenAnotherNotAvailable() {
com.mongodb.client.RetryableWritesProseTest.retriesOnSameMongosWhenAnotherNotAvailable(
SyncMongoClient::new,
mongoCollection -> mongoCollection.insertOne(new Document()), "insert", true);
}

/**
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#case-1-test-that-drivers-return-the-correct-error-when-receiving-only-errors-without-nowritesperformed">
* 6. Test error propagation after encountering multiple errors.
* Case 1: Test that drivers return the correct error when receiving only errors without NoWritesPerformed</a>.
*/
@Test
@Disabled("TODO-BACKPRESSURE Valentin Enable when implementing JAVA-6055")
void errorPropagationAfterEncounteringMultipleErrorsCase1() throws Exception {
com.mongodb.client.RetryableWritesProseTest.errorPropagationAfterEncounteringMultipleErrorsCase1(SyncMongoClient::new);
}

/**
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#case-2-test-that-drivers-return-the-correct-error-when-receiving-only-errors-with-nowritesperformed">
* 6. Test error propagation after encountering multiple errors.
* Case 2: Test that drivers return the correct error when receiving only errors with NoWritesPerformed</a>.
*/
@Test
void errorPropagationAfterEncounteringMultipleErrorsCase2() throws Exception {
com.mongodb.client.RetryableWritesProseTest.errorPropagationAfterEncounteringMultipleErrorsCase2(SyncMongoClient::new);
}

/**
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-writes/tests/README.md#case-3-test-that-drivers-return-the-correct-error-when-receiving-some-errors-with-nowritesperformed-and-some-without-nowritesperformed">
* 6. Test error propagation after encountering multiple errors.
* Case 3: Test that drivers return the correct error when receiving some errors with NoWritesPerformed and some without NoWritesPerformed</a>.
*/
@Test
void errorPropagationAfterEncounteringMultipleErrorsCase3() throws Exception {
com.mongodb.client.RetryableWritesProseTest.errorPropagationAfterEncounteringMultipleErrorsCase3(SyncMongoClient::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.ContextProvider;
import com.mongodb.RequestContext;
import com.mongodb.WriteConcern;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.CommandStartedEvent;
Expand Down Expand Up @@ -206,6 +207,7 @@ public void contextShouldBeAvailableInCommandEvents() {
}
}

@NotThreadSafe
private static final class TestCommandListener implements CommandListener {
private int numCommandStartedEventsWithExpectedContext;
private int numCommandSucceededEventsWithExpectedContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,26 @@
import org.bson.Document;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static com.mongodb.client.model.Filters.eq;

/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests">Retryable Reads Tests</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#prose-tests">
* Prose Tests</a>.
*/
final class RetryableReadsProseTest {
/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests#poolclearederror-retryability-test">
* PoolClearedError Retryability Test</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#1-poolclearederror-retryability-test">
* 1. PoolClearedError Retryability Test</a>.
*/
@Test
void poolClearedExceptionMustBeRetryable() throws InterruptedException, ExecutionException, TimeoutException {
void poolClearedExceptionMustBeRetryable() throws Exception {
RetryableWritesProseTest.poolClearedExceptionMustBeRetryable(MongoClients::create,
mongoCollection -> mongoCollection.find(eq(0)).iterator().hasNext(), "find", false);
}

/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests#21-retryable-reads-are-retried-on-a-different-mongos-when-one-is-available">
* Retryable Reads Are Retried on a Different mongos When One is Available</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#21-retryable-reads-are-retried-on-a-different-mongos-when-one-is-available">
* 2.1 Retryable Reads Are Retried on a Different mongos When One is Available</a>.
*/
@Test
void retriesOnDifferentMongosWhenAvailable() {
Expand All @@ -56,9 +51,8 @@ void retriesOnDifferentMongosWhenAvailable() {
}

/**
* See
* <a href="https://github.com/mongodb/specifications/tree/master/source/retryable-reads/tests#22-retryable-reads-are-retried-on-the-same-mongos-when-no-others-are-available">
* Retryable Reads Are Retried on the Same mongos When No Others are Available</a>.
* <a href="https://github.com/mongodb/specifications/blob/master/source/retryable-reads/tests/README.md#22-retryable-reads-are-retried-on-the-same-mongos-when-no-others-are-available">
* 2.2 Retryable Reads Are Retried on the Same mongos When No Others are Available</a>.
*/
@Test
void retriesOnSameMongosWhenAnotherNotAvailable() {
Expand Down
Loading