Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,104 @@
/*
* 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 com.mongodb.session.ClientSession;
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 org.junit.jupiter.api.Assumptions.abort;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the SocketSettings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% sure it was in the original sync prose tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it and will see if it causes any issues

.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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we call ?

client.getClusterDescription().getServerDescriptions()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests require the actual cursor server address - which isn't available in the reactive streams implementation

}

@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() {
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Mono<Document> testWriteConcern = Mono.from(client.startSession())
.flatMap(clientSession -> {
clientSession.startTransaction();
return Mono.using(() -> clientSession,
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())
.doOnNext(Assertions::assertNotNull)
),
ClientSession::close
);
});

StepVerifier.create(testWriteConcern).verifyComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,19 +34,24 @@
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 org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Copy link
Member Author

Choose a reason for hiding this comment

The 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))
Expand All @@ -52,7 +61,7 @@ public void setUp() {
collection.drop();
}

@After
@AfterEach
public void tearDown() {
if (collection != null) {
collection.drop();
Expand All @@ -62,63 +71,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("{}"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add assumeTrue(serverVersionAtLeast(4, 2)); // spec requires 4.1.6

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

try (ClientSession session = client.startSession()) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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());
Copy link
Member Author

Choose a reason for hiding this comment

The 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 {
ClientSession session = null;
try {
collection.insertOne(Document.parse("{}"));
session = client.startSession();
collection.insertOne(Document.parse("{}"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto (version check)

try (ClientSession session = client.startSession()) {
session.startTransaction();
collection.insertOne(session, Document.parse("{ _id : 1 }"));
session.commitTransaction();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec test doesn't commit the transaction, an oversight? (same for the previous test)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think thats just the pythonic code - it uses a with block that auto closes / commits the transaction.


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() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new prose test.

try (ClientSession session = client.startSession()) {
MongoCollection<Document> wcCollection = collection.withWriteConcern(new WriteConcern(0));

assertDoesNotThrow(() -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure at which point this test asserts that the write concern is not inherited ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its quite implicit, if it inherited the UNACKNOWLEDGED write concern it would throw an error. - This just checks for the absence of an error and therefore the lack of inheritance.

session.startTransaction();
wcCollection.insertOne(session, new Document("n", 1));
session.commitTransaction();
});
assertNotNull(collection.find(new Document("n", 1)).first());
}
}

}