@@ -20,6 +20,7 @@ import (
20
20
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
21
21
"go.mongodb.org/mongo-driver/version"
22
22
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
23
+ "go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
23
24
)
24
25
25
26
func TestHandshakeProse (t * testing.T ) {
@@ -199,3 +200,53 @@ func TestHandshakeProse(t *testing.T) {
199
200
})
200
201
}
201
202
}
203
+
204
+ func TestLoadBalancedConnectionHandshake (t * testing.T ) {
205
+ mt := mtest .New (t )
206
+
207
+ lbopts := mtest .NewOptions ().ClientType (mtest .Proxy ).Topologies (
208
+ mtest .LoadBalanced )
209
+
210
+ mt .RunOpts ("LB connection handshake uses OP_MSG" , lbopts , func (mt * mtest.T ) {
211
+ // Ping the server to ensure the handshake has completed.
212
+ err := mt .Client .Ping (context .Background (), nil )
213
+ require .NoError (mt , err , "Ping error: %v" , err )
214
+
215
+ messages := mt .GetProxiedMessages ()
216
+ handshakeMessage := messages [:1 ][0 ]
217
+
218
+ // Per the specifications, if loadBalanced=true, drivers MUST use the hello
219
+ // command for the initial handshake and use the OP_MSG protocol.
220
+ assert .Equal (mt , "hello" , handshakeMessage .CommandName )
221
+ assert .Equal (mt , wiremessage .OpMsg , handshakeMessage .Sent .OpCode )
222
+ })
223
+
224
+ opts := mtest .NewOptions ().ClientType (mtest .Proxy ).Topologies (
225
+ mtest .ReplicaSet ,
226
+ mtest .Sharded ,
227
+ mtest .Single ,
228
+ mtest .ShardedReplicaSet )
229
+
230
+ mt .RunOpts ("non-LB connection handshake uses OP_QUERY" , opts , func (mt * mtest.T ) {
231
+ // Ping the server to ensure the handshake has completed.
232
+ err := mt .Client .Ping (context .Background (), nil )
233
+ require .NoError (mt , err , "Ping error: %v" , err )
234
+
235
+ messages := mt .GetProxiedMessages ()
236
+ handshakeMessage := messages [:1 ][0 ]
237
+
238
+ want := wiremessage .OpQuery
239
+
240
+ hello := handshake .LegacyHello
241
+ if os .Getenv ("REQUIRE_API_VERSION" ) == "true" {
242
+ hello = "hello"
243
+
244
+ // If the server API version is requested, then we should use OP_MSG
245
+ // regardless of the topology
246
+ want = wiremessage .OpMsg
247
+ }
248
+
249
+ assert .Equal (mt , hello , handshakeMessage .CommandName )
250
+ assert .Equal (mt , want , handshakeMessage .Sent .OpCode )
251
+ })
252
+ }
0 commit comments