|
| 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 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "go.mongodb.org/mongo-driver/bson" |
| 15 | + "go.mongodb.org/mongo-driver/internal/testutil/assert" |
| 16 | + "go.mongodb.org/mongo-driver/mongo/integration/mtest" |
| 17 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 18 | +) |
| 19 | + |
| 20 | +func TestSDAMErrorHandling(t *testing.T) { |
| 21 | + mt := mtest.New(t, noClientOpts) |
| 22 | + clientOpts := options.Client(). |
| 23 | + ApplyURI(mt.ConnString()). |
| 24 | + SetRetryWrites(false). |
| 25 | + SetPoolMonitor(poolMonitor). |
| 26 | + SetWriteConcern(mtest.MajorityWc) |
| 27 | + mtOpts := mtest.NewOptions(). |
| 28 | + Topologies(mtest.ReplicaSet). // Don't run on sharded clusters to avoid complexity of sharded failpoints. |
| 29 | + MinServerVersion("4.0"). // 4.0+ is required to use failpoints on replica sets. |
| 30 | + ClientOptions(clientOpts) |
| 31 | + |
| 32 | + mt.RunOpts("network errors", mtOpts, func(mt *mtest.T) { |
| 33 | + mt.Run("pool cleared on non-timeout network error", func(mt *mtest.T) { |
| 34 | + clearPoolChan() |
| 35 | + mt.SetFailPoint(mtest.FailPoint{ |
| 36 | + ConfigureFailPoint: "failCommand", |
| 37 | + Mode: mtest.FailPointMode{ |
| 38 | + Times: 1, |
| 39 | + }, |
| 40 | + Data: mtest.FailPointData{ |
| 41 | + FailCommands: []string{"insert"}, |
| 42 | + CloseConnection: true, |
| 43 | + }, |
| 44 | + }) |
| 45 | + |
| 46 | + _, err := mt.Coll.InsertOne(mtest.Background, bson.D{{"test", 1}}) |
| 47 | + assert.NotNil(mt, err, "expected InsertOne error, got nil") |
| 48 | + assert.True(mt, isPoolCleared(), "expected pool to be cleared but was not") |
| 49 | + }) |
| 50 | + mt.Run("pool not cleared on timeout network error", func(mt *mtest.T) { |
| 51 | + clearPoolChan() |
| 52 | + |
| 53 | + _, err := mt.Coll.InsertOne(mtest.Background, bson.D{{"x", 1}}) |
| 54 | + assert.Nil(mt, err, "InsertOne error: %v", err) |
| 55 | + |
| 56 | + filter := bson.M{ |
| 57 | + "$where": "function() { sleep(1000); return false; }", |
| 58 | + } |
| 59 | + timeoutCtx, cancel := context.WithTimeout(mtest.Background, 100*time.Millisecond) |
| 60 | + defer cancel() |
| 61 | + _, err = mt.Coll.Find(timeoutCtx, filter) |
| 62 | + assert.NotNil(mt, err, "expected Find error, got %v", err) |
| 63 | + |
| 64 | + assert.False(mt, isPoolCleared(), "expected pool to not be cleared but was") |
| 65 | + }) |
| 66 | + }) |
| 67 | +} |
0 commit comments