@@ -10,12 +10,12 @@ import (
10
10
"context"
11
11
"sync"
12
12
"testing"
13
- "time"
14
13
15
14
"github.com/stretchr/testify/assert"
16
15
"github.com/stretchr/testify/require"
17
16
"go.mongodb.org/mongo-driver/bson"
18
17
"go.mongodb.org/mongo-driver/event"
18
+ "go.mongodb.org/mongo-driver/mongo/description"
19
19
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
20
20
"go.mongodb.org/mongo-driver/mongo/options"
21
21
)
@@ -59,17 +59,29 @@ func TestServerSelectionProse(t *testing.T) {
59
59
mt .ClearFailPoints ()
60
60
}()
61
61
62
- // Reset the client with exactly 2 mongos hosts.
62
+ // Reset the client with exactly 2 mongos hosts. Use a ServerMonitor to wait for both mongos
63
+ // host descriptions to move from kind "Unknown" to kind "Mongos".
64
+ topologyEvents := make (chan * event.TopologyDescriptionChangedEvent , 10 )
63
65
tpm := newTestPoolMonitor ()
64
66
mt .ResetClient (options .Client ().
65
67
SetHosts (hosts [:2 ]).
66
68
SetPoolMonitor (tpm .PoolMonitor ).
67
- SetAppName ("loadBalancingTest" ))
69
+ SetAppName ("loadBalancingTest" ).
70
+ SetServerMonitor (& event.ServerMonitor {
71
+ TopologyDescriptionChanged : func (evt * event.TopologyDescriptionChangedEvent ) {
72
+ topologyEvents <- evt
73
+ },
74
+ }))
75
+ for evt := range topologyEvents {
76
+ servers := evt .NewDescription .Servers
77
+ if len (servers ) == 2 && servers [0 ].Kind == description .Mongos && servers [1 ].Kind == description .Mongos {
78
+ break
79
+ }
80
+ }
68
81
69
- // Start 25 goroutines that each run 10 findOne operations. Run more operations than the
70
- // prose test specifies to get more samples and reduce intermittent test failures.
82
+ // Start 10 goroutines that each run 10 findOne operations.
71
83
var wg sync.WaitGroup
72
- for i := 0 ; i < 25 ; i ++ {
84
+ for i := 0 ; i < 10 ; i ++ {
73
85
wg .Add (1 )
74
86
go func () {
75
87
defer wg .Done ()
@@ -112,28 +124,33 @@ func TestServerSelectionProse(t *testing.T) {
112
124
hosts := options .Client ().ApplyURI (mtest .ClusterURI ()).Hosts
113
125
require .GreaterOrEqualf (mt , len (hosts ), 2 , "test cluster must have at least 2 mongos hosts" )
114
126
115
- // Reset the client with exactly 2 mongos hosts.
127
+ // Reset the client with exactly 2 mongos hosts. Use a ServerMonitor to wait for both mongos
128
+ // host descriptions to move from kind "Unknown" to kind "Mongos".
129
+ topologyEvents := make (chan * event.TopologyDescriptionChangedEvent , 10 )
116
130
tpm := newTestPoolMonitor ()
117
131
mt .ResetClient (options .Client ().
118
132
SetHosts (hosts [:2 ]).
119
133
SetPoolMonitor (tpm .PoolMonitor ).
120
- SetHeartbeatInterval (500 * time .Millisecond ))
121
-
122
- // Sleep for 1s to allow server state discovery and at least 1 heartbeat to complete. We
123
- // need both servers to be selectable when we start running the test or the distribution of
124
- // selected servers will be skewed. Unfortunately there's not currently another signal we
125
- // can wait on.
126
- time .Sleep (1 * time .Second )
134
+ SetServerMonitor (& event.ServerMonitor {
135
+ TopologyDescriptionChanged : func (evt * event.TopologyDescriptionChangedEvent ) {
136
+ topologyEvents <- evt
137
+ },
138
+ }))
139
+ for evt := range topologyEvents {
140
+ servers := evt .NewDescription .Servers
141
+ if len (servers ) == 2 && servers [0 ].Kind == description .Mongos && servers [1 ].Kind == description .Mongos {
142
+ break
143
+ }
144
+ }
127
145
128
- // Start 25 goroutines that each run 200 findOne operations. Run more operations than the
129
- // prose test specifies to get more samples and reduce intermittent test failures.
146
+ // Start 10 goroutines that each run 10 findOne operations.
130
147
var wg sync.WaitGroup
131
- for i := 0 ; i < 25 ; i ++ {
148
+ for i := 0 ; i < 10 ; i ++ {
132
149
wg .Add (1 )
133
150
go func () {
134
151
defer wg .Done ()
135
152
136
- for i := 0 ; i < 200 ; i ++ {
153
+ for i := 0 ; i < 20 ; i ++ {
137
154
res := mt .Coll .FindOne (context .Background (), bson.D {})
138
155
assert .NoError (mt , res .Err (), "FindOne() error" )
139
156
}
0 commit comments