Skip to content

Commit 7874a68

Browse files
author
Divjot Arora
committed
add prose tests
1 parent b753c20 commit 7874a68

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ evg-test-load-balancers:
171171
go test $(BUILD_TAGS) ./mongo/integration -run TestRetryableWritesSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite
172172
go test $(BUILD_TAGS) ./mongo/integration -run TestChangeStreamSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite
173173
go test $(BULID_TAGS) ./mongo/integration -run TestInitialDNSSeedlistDiscoverySpec/load_balanced -v -timeout $(TEST_TIMEOUT)s >> test.suite
174+
go test $(BUILD_TAGS) ./mongo/integration -run TestLoadBalancerSupport -v -timeout $(TEST_TIMEOUT)s >> test.suite
174175
go test $(BUILD_TAGS) ./mongo/integration/unified -run TestUnifiedSpec -v -timeout $(TEST_TIMEOUT)s >> test.suite
175176

176177
.PHONY: evg-test-kms
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (C) MongoDB, Inc. 2017-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+
"fmt"
12+
"strings"
13+
"testing"
14+
"time"
15+
16+
"go.mongodb.org/mongo-driver/bson"
17+
"go.mongodb.org/mongo-driver/internal/testutil/assert"
18+
"go.mongodb.org/mongo-driver/mongo"
19+
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
20+
"go.mongodb.org/mongo-driver/mongo/options"
21+
)
22+
23+
func TestLoadBalancerSupport(t *testing.T) {
24+
mt := mtest.New(t, mtest.NewOptions().Topologies(mtest.LoadBalanced).CreateClient(false))
25+
defer mt.Close()
26+
27+
mt.Run("RunCommandCursor pins to a connection", func(mt *mtest.T) {
28+
// The LB spec tests cover the behavior for cursors created by CRUD operations, but RunCommandCursor is
29+
// Go-specific so there is no spec test coverage for it.
30+
31+
initCollection(mt, mt.Coll)
32+
findCmd := bson.D{
33+
{"find", mt.Coll.Name()},
34+
{"filter", bson.D{}},
35+
{"batchSize", 2},
36+
}
37+
cursor, err := mt.DB.RunCommandCursor(mtest.Background, findCmd)
38+
assert.Nil(mt, err, "RunCommandCursor error: %v", err)
39+
defer func() {
40+
_ = cursor.Close(mtest.Background)
41+
}()
42+
43+
assert.True(mt, cursor.ID() > 0, "expected cursor ID to be non-zero")
44+
assert.Equal(mt, 1, mt.NumberConnectionsCheckedOut(),
45+
"expected one connection to be checked out, got %d", mt.NumberConnectionsCheckedOut())
46+
})
47+
48+
mt.RunOpts("wait queue timeout errors include extra information", noClientOpts, func(mt *mtest.T) {
49+
// There are spec tests to assert this behavior, but they rely on the waitQueueTimeoutMS Client option, which is
50+
// not supported in Go, so we have to skip them. These prose tests make the same assertions, but use context
51+
// deadlines to force wait queue timeout errors.
52+
53+
assertErrorHasInfo := func(mt *mtest.T, err error, numCursorConns, numTxnConns, numOtherConns int) {
54+
mt.Helper()
55+
56+
assert.NotNil(mt, err, "expected wait queue timeout error, got nil")
57+
expectedMsg := fmt.Sprintf("maxPoolSize: 1, "+
58+
"connections in use by cursors: %d, "+
59+
"connections in use by transactions: %d, "+
60+
"connections in use by other operations: %d",
61+
numCursorConns, numTxnConns, numOtherConns,
62+
)
63+
assert.True(mt, strings.Contains(err.Error(), expectedMsg),
64+
"expected error %q to contain substring %q", err, expectedMsg)
65+
}
66+
maxPoolSizeMtOpts := mtest.NewOptions().
67+
ClientOptions(options.Client().SetMaxPoolSize(1))
68+
69+
mt.RunOpts("cursors", maxPoolSizeMtOpts, func(mt *mtest.T) {
70+
initCollection(mt, mt.Coll)
71+
findOpts := options.Find().SetBatchSize(2)
72+
cursor, err := mt.Coll.Find(mtest.Background, bson.M{}, findOpts)
73+
assert.Nil(mt, err, "Find error: %v", err)
74+
defer func() {
75+
_ = cursor.Close(mtest.Background)
76+
}()
77+
78+
ctx, cancel := context.WithTimeout(mtest.Background, 50*time.Millisecond)
79+
defer cancel()
80+
_, err = mt.Coll.InsertOne(ctx, bson.M{"x": 1})
81+
assertErrorHasInfo(mt, err, 1, 0, 0)
82+
})
83+
mt.RunOpts("transactions", maxPoolSizeMtOpts, func(mt *mtest.T) {
84+
sess, err := mt.Client.StartSession()
85+
assert.Nil(mt, err, "StartSession error: %v", err)
86+
defer sess.EndSession(mtest.Background)
87+
sessCtx := mongo.NewSessionContext(context.Background(), sess)
88+
89+
// Start a transaction and perform one transactional operation to pin a connection.
90+
err = sess.StartTransaction()
91+
assert.Nil(mt, err, "StartTransaction error: %v", err)
92+
_, err = mt.Coll.InsertOne(sessCtx, bson.M{"x": 1})
93+
assert.Nil(mt, err, "InsertOne error: %v", err)
94+
95+
ctx, cancel := context.WithTimeout(mtest.Background, 50*time.Millisecond)
96+
defer cancel()
97+
_, err = mt.Coll.InsertOne(ctx, bson.M{"x": 1})
98+
assertErrorHasInfo(mt, err, 0, 1, 0)
99+
})
100+
})
101+
}

mongo/integration/mtest/mongotest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ func (t *T) GetProxiedMessages() []*ProxyMessage {
359359
return t.proxyDialer.Messages()
360360
}
361361

362+
// NumberConnectionsCheckedOut returns the number of connections checked out from the test Client.
363+
func (t *T) NumberConnectionsCheckedOut() int {
364+
return t.connsCheckedOut
365+
}
366+
362367
// ClearEvents clears the existing command monitoring events.
363368
func (t *T) ClearEvents() {
364369
t.started = t.started[:0]

0 commit comments

Comments
 (0)