Skip to content

Commit 6f492ef

Browse files
authored
GODRIVER-2343 Add CSOT prose tests for server selection. (#996)
1 parent 4f6313e commit 6f492ef

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

mongo/integration/csot_prose_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

x/mongo/driver/topology/topology.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (t *Topology) RequestImmediateCheck() {
356356
}
357357

358358
// SelectServer selects a server with given a selector. SelectServer complies with the
359-
// server selection spec, and will time out after severSelectionTimeout or when the
359+
// server selection spec, and will time out after serverSelectionTimeout or when the
360360
// parent context is done.
361361
func (t *Topology) SelectServer(ctx context.Context, ss description.ServerSelector) (driver.Server, error) {
362362
if atomic.LoadInt64(&t.state) != topologyConnected {

0 commit comments

Comments
 (0)