|
| 1 | +// Copyright (C) MongoDB, Inc. 2022-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package integration |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "go.mongodb.org/mongo-driver/internal/testutil/assert" |
| 15 | + "go.mongodb.org/mongo-driver/mongo/integration/mtest" |
| 16 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCSOTProse(t *testing.T) { |
| 20 | + mt := mtest.New(t, mtest.NewOptions().CreateClient(false)) |
| 21 | + defer mt.Close() |
| 22 | + |
| 23 | + mt.Run("server selection", func(mt *mtest.T) { |
| 24 | + cliOpts := options.Client().ApplyURI("mongodb://invalid/?serverSelectionTimeoutMS=10") |
| 25 | + mtOpts := mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false) |
| 26 | + mt.RunOpts("serverSelectionTimeoutMS honored if timeoutMS is not set", mtOpts, func(mt *mtest.T) { |
| 27 | + callback := func(ctx context.Context) { |
| 28 | + err := mt.Client.Ping(ctx, nil) |
| 29 | + assert.NotNil(mt, err, "expected Ping error, got nil") |
| 30 | + } |
| 31 | + |
| 32 | + // Assert that Ping fails within 15ms due to server selection timeout. |
| 33 | + assert.Soon(mt, callback, 15*time.Millisecond) |
| 34 | + }) |
| 35 | + |
| 36 | + cliOpts = options.Client().ApplyURI("mongodb://invalid/?timeoutMS=10&serverSelectionTimeoutMS=20") |
| 37 | + mtOpts = mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false) |
| 38 | + mt.RunOpts("timeoutMS honored for server selection if it's lower than serverSelectionTimeoutMS", mtOpts, func(mt *mtest.T) { |
| 39 | + callback := func(ctx context.Context) { |
| 40 | + err := mt.Client.Ping(ctx, nil) |
| 41 | + assert.NotNil(mt, err, "expected Ping error, got nil") |
| 42 | + } |
| 43 | + |
| 44 | + // Assert that Ping fails within 15ms due to timeout. |
| 45 | + assert.Soon(mt, callback, 15*time.Millisecond) |
| 46 | + }) |
| 47 | + |
| 48 | + cliOpts = options.Client().ApplyURI("mongodb://invalid/?timeoutMS=20&serverSelectionTimeoutMS=10") |
| 49 | + mtOpts = mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false) |
| 50 | + mt.RunOpts("serverSelectionTimeoutMS honored for server selection if it's lower than timeoutMS", mtOpts, func(mt *mtest.T) { |
| 51 | + callback := func(ctx context.Context) { |
| 52 | + err := mt.Client.Ping(ctx, nil) |
| 53 | + assert.NotNil(mt, err, "expected Ping error, got nil") |
| 54 | + } |
| 55 | + |
| 56 | + // Assert that Ping fails within 15ms due to server selection timeout. |
| 57 | + assert.Soon(mt, callback, 15*time.Millisecond) |
| 58 | + }) |
| 59 | + |
| 60 | + cliOpts = options.Client().ApplyURI("mongodb://invalid/?timeoutMS=0&serverSelectionTimeoutMS=10") |
| 61 | + mtOpts = mtest.NewOptions().ClientOptions(cliOpts).CreateCollection(false) |
| 62 | + mt.RunOpts("serverSelectionTimeoutMS honored for server selection if timeoutMS=0", mtOpts, func(mt *mtest.T) { |
| 63 | + callback := func(ctx context.Context) { |
| 64 | + err := mt.Client.Ping(ctx, nil) |
| 65 | + assert.NotNil(mt, err, "expected Ping error, got nil") |
| 66 | + } |
| 67 | + |
| 68 | + // Assert that Ping fails within 15ms due to server selection timeout. |
| 69 | + assert.Soon(mt, callback, 15*time.Millisecond) |
| 70 | + }) |
| 71 | + }) |
| 72 | +} |
0 commit comments