-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implemented new prose test for Transactions #1824
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
base: main
Are you sure you want to change the base?
Changes from all commits
66eb45b
bb815e8
eb277f9
b6715db
2b03fbc
b1f5e10
91528ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.mongodb.reactivestreams.client; | ||
|
|
||
| import com.mongodb.ConnectionString; | ||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.WriteConcern; | ||
| import org.bson.Document; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import reactor.core.publisher.Mono; | ||
| import reactor.test.StepVerifier; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static com.mongodb.ClusterFixture.getDefaultDatabaseName; | ||
| import static com.mongodb.ClusterFixture.getMultiMongosConnectionString; | ||
| import static com.mongodb.ClusterFixture.isSharded; | ||
| import static com.mongodb.ClusterFixture.serverVersionAtLeast; | ||
| import static org.junit.jupiter.api.Assumptions.abort; | ||
| import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
|
||
| public class TransactionProseTest { | ||
| private MongoClient client; | ||
| private MongoCollection<Document> collection; | ||
|
|
||
| @BeforeEach | ||
| public void setUp() { | ||
| assumeTrue(isSharded()); | ||
| ConnectionString multiMongosConnectionString = getMultiMongosConnectionString(); | ||
| assumeTrue(multiMongosConnectionString != null); | ||
|
|
||
| MongoClientSettings.Builder builder = MongoClientSettings.builder() | ||
| .applyConnectionString(multiMongosConnectionString); | ||
|
|
||
| client = MongoClients.create(MongoClientSettings.builder(builder.build()) | ||
| .applyToSocketSettings(builder1 -> builder1.readTimeout(5, TimeUnit.SECONDS)) | ||
| .build()); | ||
|
|
||
| collection = client.getDatabase(getDefaultDatabaseName()).getCollection(getClass().getName()); | ||
| StepVerifier.create(Mono.from(collection.drop())).verifyComplete(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| if (collection != null) { | ||
| StepVerifier.create(Mono.from(collection.drop())).verifyComplete(); | ||
| } | ||
| if (client != null) { | ||
| client.close(); | ||
| } | ||
| } | ||
|
|
||
| @DisplayName("Mongos Pinning Prose Tests: 1. Test that starting a new transaction on a pinned ClientSession unpins the session " | ||
| + "and normal server selection is performed for the next operation.") | ||
| @Test | ||
| void testNewTransactionUnpinsSession() { | ||
| abort("There is no ability to get the server address with the reactive api"); | ||
| } | ||
|
|
||
| @DisplayName("Mongos Pinning Prose Tests: 2. Test non-transaction operations using a pinned ClientSession unpins the session" | ||
| + " and normal server selection is performed") | ||
| @Test | ||
| void testNonTransactionOpsUnpinsSession() { | ||
| abort("There is no ability to get the server address with the reactive api"); | ||
| } | ||
|
|
||
| @DisplayName("Options Inside Transaction Prose Tests. 1. Write concern not inherited from collection object inside transaction") | ||
| @Test | ||
| void testWriteConcernInheritance() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having to use map / flatMap to pass the session into each Mono is painful. But appears to be the correct way to ensure that the session use is correctly ordered. |
||
| assumeTrue(serverVersionAtLeast(4, 4)); | ||
| Mono<Document> testWriteConcern = Mono.from(client.startSession()) | ||
| .flatMap(session -> | ||
| Mono.fromRunnable(session::startTransaction) | ||
| .then(Mono.from(collection.withWriteConcern(new WriteConcern(0)).insertOne(session, new Document("n", 1)))) | ||
| .then(Mono.from(session.commitTransaction())) | ||
| .then(Mono.from(collection.find(new Document("n", 1)).first())) | ||
| .doFinally(signalType -> session.close()) | ||
| ); | ||
|
|
||
| StepVerifier.create(testWriteConcern).assertNext(Assertions::assertNotNull).verifyComplete(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,16 @@ | |
|
|
||
| package com.mongodb.client; | ||
|
|
||
| import com.mongodb.ConnectionString; | ||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.MongoException; | ||
| import com.mongodb.ServerAddress; | ||
| import com.mongodb.WriteConcern; | ||
| import org.bson.Document; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
@@ -30,19 +34,25 @@ | |
| import static com.mongodb.ClusterFixture.getDefaultDatabaseName; | ||
| import static com.mongodb.ClusterFixture.getMultiMongosConnectionString; | ||
| import static com.mongodb.ClusterFixture.isSharded; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assume.assumeTrue; | ||
| import static com.mongodb.ClusterFixture.serverVersionAtLeast; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the opportunity to modernize the test to junit5 |
||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
|
|
||
| // See https://github.com/mongodb/specifications/blob/master/source/transactions/tests/README.md#mongos-pinning-prose-tests | ||
| public class TransactionProseTest { | ||
| private MongoClient client; | ||
| private MongoCollection<Document> collection; | ||
|
|
||
| @Before | ||
| @BeforeEach | ||
| public void setUp() { | ||
| assumeTrue(canRunTests()); | ||
| assumeTrue(isSharded()); | ||
| ConnectionString multiMongosConnectionString = getMultiMongosConnectionString(); | ||
| assumeTrue(multiMongosConnectionString != null); | ||
|
|
||
| MongoClientSettings.Builder builder = MongoClientSettings.builder() | ||
| .applyConnectionString(getMultiMongosConnectionString()); | ||
| .applyConnectionString(multiMongosConnectionString); | ||
|
|
||
| client = MongoClients.create(MongoClientSettings.builder(builder.build()) | ||
| .applyToSocketSettings(builder1 -> builder1.readTimeout(5, TimeUnit.SECONDS)) | ||
|
|
@@ -52,7 +62,7 @@ public void setUp() { | |
| collection.drop(); | ||
| } | ||
|
|
||
| @After | ||
| @AfterEach | ||
| public void tearDown() { | ||
| if (collection != null) { | ||
| collection.drop(); | ||
|
|
@@ -62,63 +72,64 @@ public void tearDown() { | |
| } | ||
| } | ||
|
|
||
| // Test that starting a new transaction on a pinned ClientSession unpins the session and normal | ||
| // server selection is performed for the next operation. | ||
| @DisplayName("Mongos Pinning Prose Tests: 1. Test that starting a new transaction on a pinned ClientSession unpins the session " | ||
| + "and normal server selection is performed for the next operation.") | ||
| @Test | ||
| public void testNewTransactionUnpinsSession() throws MongoException { | ||
| ClientSession session = null; | ||
| try { | ||
| collection.insertOne(Document.parse("{}")); | ||
| session = client.startSession(); | ||
| public void testNewTransactionUnpinsSession() { | ||
| collection.insertOne(Document.parse("{}")); | ||
| try (ClientSession session = client.startSession()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer try with resources (this code was from java 6 days). |
||
| session.startTransaction(); | ||
| collection.insertOne(session, Document.parse("{ _id : 1 }")); | ||
| session.commitTransaction(); | ||
|
|
||
| Set<FindIterable<Document>> addresses = new HashSet<>(); | ||
| Set<ServerAddress> addresses = new HashSet<>(); | ||
| int iterations = 50; | ||
| while (iterations-- > 0) { | ||
| session.startTransaction(); | ||
| addresses.add(collection.find(session, Document.parse("{}"))); | ||
| try (MongoCursor<Document> cursor = collection.find(session, Document.parse("{}")).cursor()) { | ||
| addresses.add(cursor.getServerAddress()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test wasn't to spec as it didn't actually check the server addresses, as it does in the prose test spec. |
||
| } | ||
| session.commitTransaction(); | ||
| } | ||
| assertTrue(addresses.size() > 1); | ||
| } finally { | ||
| if (session != null) { | ||
| session.close(); | ||
| } | ||
| if (collection != null) { | ||
| collection.drop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Test non-transaction operations using a pinned ClientSession unpins the session and normal server selection is performed. | ||
| @DisplayName("Mongos Pinning Prose Tests: 2. Test non-transaction operations using a pinned ClientSession unpins the session" | ||
| + " and normal server selection is performed") | ||
| @Test | ||
| public void testNonTransactionOpsUnpinsSession() throws MongoException { | ||
rozza marked this conversation as resolved.
Show resolved
Hide resolved
rozza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ClientSession session = null; | ||
| try { | ||
| collection.insertOne(Document.parse("{}")); | ||
| session = client.startSession(); | ||
| collection.insertOne(Document.parse("{}")); | ||
| try (ClientSession session = client.startSession()) { | ||
| session.startTransaction(); | ||
| collection.insertOne(session, Document.parse("{ _id : 1 }")); | ||
| session.commitTransaction(); | ||
|
|
||
| Set<FindIterable<Document>> addresses = new HashSet<>(); | ||
| Set<ServerAddress> addresses = new HashSet<>(); | ||
| int iterations = 50; | ||
| while (iterations-- > 0) { | ||
| addresses.add(collection.find(session, Document.parse("{}"))); | ||
| try (MongoCursor<Document> cursor = collection.find(session, Document.parse("{}")).cursor()) { | ||
| addresses.add(cursor.getServerAddress()); | ||
| } | ||
| } | ||
| assertTrue(addresses.size() > 1); | ||
| } finally { | ||
| if (session != null) { | ||
| session.close(); | ||
| } | ||
| if (collection != null) { | ||
| collection.drop(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean canRunTests() { | ||
| return isSharded(); | ||
| @DisplayName("Options Inside Transaction Prose Tests. 1. Write concern not inherited from collection object inside transaction") | ||
| @Test | ||
| void testWriteConcernInheritance() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new prose test. |
||
| assumeTrue(serverVersionAtLeast(4, 4)); | ||
| try (ClientSession session = client.startSession()) { | ||
| MongoCollection<Document> wcCollection = collection.withWriteConcern(new WriteConcern(0)); | ||
|
|
||
| assertDoesNotThrow(() -> { | ||
| session.startTransaction(); | ||
| wcCollection.insertOne(session, new Document("n", 1)); | ||
| session.commitTransaction(); | ||
| }); | ||
| assertNotNull(collection.find(new Document("n", 1)).first()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing the reactive streams prose test identified a bug. Validation was done when the publisher was created and not when the publisher was being used.
This complicated the usage of transactions, a fact that was hidden by the lack of prose tests! This fix is in the remit of JAVA-5895