|
| 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 | +} |
0 commit comments