|
| 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.internal; |
| 18 | + |
| 19 | +import com.mongodb.ConnectionString; |
| 20 | +import com.mongodb.MongoClientSettings; |
| 21 | +import com.mongodb.MongoTimeoutException; |
| 22 | +import com.mongodb.ReadConcern; |
| 23 | +import com.mongodb.ReadPreference; |
| 24 | +import com.mongodb.WriteConcern; |
| 25 | +import com.mongodb.client.MongoClient; |
| 26 | +import com.mongodb.client.MongoClients; |
| 27 | +import org.bson.BsonDocument; |
| 28 | +import org.bson.BsonInt32; |
| 29 | +import org.jetbrains.annotations.NotNull; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.ValueSource; |
| 32 | + |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 37 | + |
| 38 | +/** |
| 39 | + * See |
| 40 | + * <a href="https://github.com/mongodb/specifications/blob/master/source/client-side-operations-timeout/tests/README.rst">Prose Tests</a>. |
| 41 | + */ |
| 42 | +public class ClientSideOperationsTimeoutProseTests { |
| 43 | + |
| 44 | + protected MongoClient createMongoClient(final MongoClientSettings settings) { |
| 45 | + return MongoClients.create(settings); |
| 46 | + } |
| 47 | + |
| 48 | + private long msElapsedSince(final long t1) { |
| 49 | + return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1); |
| 50 | + } |
| 51 | + |
| 52 | + @NotNull |
| 53 | + private MongoClientSettings createMongoClientSettings(final String connectionString) { |
| 54 | + // All MongoClient instances created for tests MUST be configured |
| 55 | + // with read/write concern majority, read preference primary, and |
| 56 | + // TODO (CSOT): command monitoring enabled to listen for command_started events. |
| 57 | + ConnectionString cs = new ConnectionString(connectionString); |
| 58 | + MongoClientSettings.Builder builder = MongoClientSettings.builder() |
| 59 | + .readConcern(ReadConcern.MAJORITY) |
| 60 | + .writeConcern(WriteConcern.MAJORITY) |
| 61 | + .readPreference(ReadPreference.primary()) |
| 62 | + .applyConnectionString(cs); |
| 63 | + return builder.build(); |
| 64 | + } |
| 65 | + |
| 66 | + @ParameterizedTest |
| 67 | + @ValueSource(strings = { |
| 68 | + "mongodb://invalid/?serverSelectionTimeoutMS=10", |
| 69 | + "mongodb://invalid/?timeoutMS=10&serverSelectionTimeoutMS=200", |
| 70 | + "mongodb://invalid/?timeoutMS=200&serverSelectionTimeoutMS=10", |
| 71 | + "mongodb://invalid/?timeoutMS=0&serverSelectionTimeoutMS=10", |
| 72 | + }) |
| 73 | + public void test8ServerSelectionPart1(final String connectionString) { |
| 74 | + int timeoutBuffer = 100; // 5 in spec, Java is slower |
| 75 | + // 1. Create a MongoClient |
| 76 | + try (MongoClient mongoClient = createMongoClient(createMongoClientSettings(connectionString))) { |
| 77 | + long start = System.nanoTime(); |
| 78 | + // 2. Using client, execute: |
| 79 | + Throwable throwable = assertThrows(MongoTimeoutException.class, () -> { |
| 80 | + mongoClient.getDatabase("admin").runCommand(new BsonDocument("ping", new BsonInt32(1))); |
| 81 | + }); |
| 82 | + // Expect this to fail with a server selection timeout error after no more than 15ms [this is increased] |
| 83 | + long elapsed = msElapsedSince(start); |
| 84 | + assertTrue(throwable.getMessage().contains("while waiting for a server")); |
| 85 | + assertTrue(elapsed < 10 + timeoutBuffer, "Took too long to time out, elapsedMS: " + elapsed); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments