|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.client; |
| 18 | + |
| 19 | +import com.mongodb.MongoClientSettings; |
| 20 | +import com.mongodb.client.model.Filters; |
| 21 | +import com.mongodb.client.model.UpdateOneModel; |
| 22 | +import com.mongodb.client.model.Updates; |
| 23 | +import com.mongodb.event.CommandListener; |
| 24 | +import com.mongodb.event.CommandStartedEvent; |
| 25 | +import org.bson.BsonDocument; |
| 26 | +import org.bson.Document; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.concurrent.ExecutorService; |
| 33 | +import java.util.concurrent.Executors; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | + |
| 36 | +import static com.mongodb.ClusterFixture.getConnectionString; |
| 37 | +import static com.mongodb.ClusterFixture.getDefaultDatabaseName; |
| 38 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 39 | +import static java.util.Arrays.asList; |
| 40 | +import static java.util.Collections.singletonList; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 43 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 44 | + |
| 45 | +// Prose tests from https://github.com/mongodb/specifications/tree/master/source/sessions |
| 46 | +public abstract class AbstractSessionsProseTest { |
| 47 | + |
| 48 | + protected abstract MongoClient getMongoClient(MongoClientSettings settings); |
| 49 | + |
| 50 | + // Test 13 from https://github.com/mongodb/specifications/blob/master/source/sessions/driver-sessions.rst#test-plan" |
| 51 | + @Test |
| 52 | + public void shouldCreateServerSessionOnlyAfterConnectionCheckout() throws InterruptedException { |
| 53 | + assumeTrue(serverVersionAtLeast(3, 6)); |
| 54 | + |
| 55 | + Set<BsonDocument> lsidSet = ConcurrentHashMap.newKeySet(); |
| 56 | + MongoCollection<Document> collection; |
| 57 | + try (MongoClient client = getMongoClient(MongoClientSettings.builder() |
| 58 | + .applyConnectionString(getConnectionString()) |
| 59 | + .applyToConnectionPoolSettings(builder -> builder.maxSize(1)) |
| 60 | + .addCommandListener(new CommandListener() { |
| 61 | + @Override |
| 62 | + public void commandStarted(final CommandStartedEvent event) { |
| 63 | + lsidSet.add(event.getCommand().getDocument("lsid")); |
| 64 | + } |
| 65 | + }) |
| 66 | + .build())) { |
| 67 | + collection = client.getDatabase(getDefaultDatabaseName()).getCollection(getClass().getName()); |
| 68 | + |
| 69 | + List<Runnable> operations = asList( |
| 70 | + () -> collection.insertOne(new Document()), |
| 71 | + () -> collection.deleteOne(Filters.eq("_id", 1)), |
| 72 | + () -> collection.updateOne(Filters.eq("_id", 1), Updates.set("x", 1)), |
| 73 | + () -> collection.bulkWrite(singletonList(new UpdateOneModel<>(Filters.eq("_id", 1), Updates.set("x", 1)))), |
| 74 | + () -> collection.findOneAndDelete(Filters.eq("_id", 1)), |
| 75 | + () -> collection.findOneAndUpdate(Filters.eq("_id", 1), Updates.set("x", 1)), |
| 76 | + () -> collection.findOneAndReplace(Filters.eq("_id", 1), new Document("_id", 1)), |
| 77 | + () -> collection.find().first() |
| 78 | + ); |
| 79 | + |
| 80 | + int minLsidSetSize = Integer.MAX_VALUE; |
| 81 | + |
| 82 | + // Try up to times, counting on at least one time that only one lsid will be used |
| 83 | + for (int i = 0; i < 5; i++) { |
| 84 | + // given |
| 85 | + lsidSet.clear(); |
| 86 | + |
| 87 | + // when executing numConcurrentOperations operations concurrently |
| 88 | + ExecutorService executor = Executors.newFixedThreadPool(operations.size()); |
| 89 | + |
| 90 | + operations.forEach(executor::submit); |
| 91 | + |
| 92 | + executor.shutdown(); |
| 93 | + boolean terminated = executor.awaitTermination(5, TimeUnit.SECONDS); |
| 94 | + |
| 95 | + // then |
| 96 | + assertTrue(terminated); |
| 97 | + assertTrue(lsidSet.size() < operations.size()); |
| 98 | + minLsidSetSize = Math.min(minLsidSetSize, lsidSet.size()); |
| 99 | + if (minLsidSetSize == 1) { |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + assertEquals(1, minLsidSetSize); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments