|
| 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 | + "sync" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "go.mongodb.org/mongo-driver/bson" |
| 17 | + "go.mongodb.org/mongo-driver/event" |
| 18 | + "go.mongodb.org/mongo-driver/mongo/integration/mtest" |
| 19 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 20 | +) |
| 21 | + |
| 22 | +func TestRetryableReadsProse(t *testing.T) { |
| 23 | + tpm := newTestPoolMonitor() |
| 24 | + |
| 25 | + // Client options with MaxPoolSize of 1 and RetryReads used per the test description. |
| 26 | + // Lower HeartbeatInterval used to speed the test up for any server that uses streaming |
| 27 | + // heartbeats. Only connect to first host in list for sharded clusters. |
| 28 | + hosts := mtest.ClusterConnString().Hosts |
| 29 | + clientOpts := options.Client().SetMaxPoolSize(1).SetRetryReads(true). |
| 30 | + SetPoolMonitor(tpm.PoolMonitor).SetHeartbeatInterval(500 * time.Millisecond). |
| 31 | + SetHosts(hosts[:1]) |
| 32 | + |
| 33 | + mtOpts := mtest.NewOptions().ClientOptions(clientOpts).MinServerVersion("4.3") |
| 34 | + mt := mtest.New(t, mtOpts) |
| 35 | + defer mt.Close() |
| 36 | + |
| 37 | + mt.Run("PoolClearedError retryability", func(mt *mtest.T) { |
| 38 | + // Insert a document to test collection. |
| 39 | + _, err := mt.Coll.InsertOne(context.Background(), bson.D{{"x", 1}}) |
| 40 | + assert.Nil(mt, err, "InsertOne error: %v", err) |
| 41 | + |
| 42 | + // Force Find to block for 1 second once. |
| 43 | + mt.SetFailPoint(mtest.FailPoint{ |
| 44 | + ConfigureFailPoint: "failCommand", |
| 45 | + Mode: mtest.FailPointMode{ |
| 46 | + Times: 1, |
| 47 | + }, |
| 48 | + Data: mtest.FailPointData{ |
| 49 | + FailCommands: []string{"find"}, |
| 50 | + ErrorCode: 91, |
| 51 | + BlockConnection: true, |
| 52 | + BlockTimeMS: 1000, |
| 53 | + }, |
| 54 | + }) |
| 55 | + |
| 56 | + // Clear CMAP and command events. |
| 57 | + tpm.ClearEvents() |
| 58 | + mt.ClearEvents() |
| 59 | + |
| 60 | + // Perform a FindOne on two different threads and assert both operations are |
| 61 | + // successful. |
| 62 | + var wg sync.WaitGroup |
| 63 | + for i := 0; i < 2; i++ { |
| 64 | + wg.Add(1) |
| 65 | + go func() { |
| 66 | + defer wg.Done() |
| 67 | + res := mt.Coll.FindOne(context.Background(), bson.D{}) |
| 68 | + assert.Nil(mt, res.Err()) |
| 69 | + }() |
| 70 | + } |
| 71 | + wg.Wait() |
| 72 | + |
| 73 | + // Gather GetSucceeded, GetFailed and PoolCleared pool events. |
| 74 | + events := tpm.Events(func(e *event.PoolEvent) bool { |
| 75 | + getSucceeded := e.Type == event.GetSucceeded |
| 76 | + getFailed := e.Type == event.GetFailed |
| 77 | + poolCleared := e.Type == event.PoolCleared |
| 78 | + return getSucceeded || getFailed || poolCleared |
| 79 | + }) |
| 80 | + |
| 81 | + // Assert that first check out succeeds, pool is cleared, and second check |
| 82 | + // out fails due to connection error. |
| 83 | + assert.True(mt, len(events) >= 3, "expected at least 3 events, got %v", len(events)) |
| 84 | + assert.Equal(mt, event.GetSucceeded, events[0].Type, |
| 85 | + "expected ConnectionCheckedOut event, got %v", events[0].Type) |
| 86 | + assert.Equal(mt, event.PoolCleared, events[1].Type, |
| 87 | + "expected ConnectionPoolCleared event, got %v", events[1].Type) |
| 88 | + assert.Equal(mt, event.GetFailed, events[2].Type, |
| 89 | + "expected ConnectionCheckedOutFailed event, got %v", events[2].Type) |
| 90 | + assert.Equal(mt, event.ReasonConnectionErrored, events[2].Reason, |
| 91 | + "expected check out failure due to connection error, failed due to %q", events[2].Reason) |
| 92 | + |
| 93 | + // Assert that three find CommandStartedEvents were observed. |
| 94 | + for i := 0; i < 3; i++ { |
| 95 | + cmdEvt := mt.GetStartedEvent() |
| 96 | + assert.NotNil(mt, cmdEvt, "expected a find event, got nil") |
| 97 | + assert.Equal(mt, cmdEvt.CommandName, "find", |
| 98 | + "expected a find event, got a(n) %v event", cmdEvt.CommandName) |
| 99 | + } |
| 100 | + }) |
| 101 | +} |
0 commit comments