@@ -14,12 +14,14 @@ import {
1414 hasSessionSupport ,
1515 type HostAddress ,
1616 isHello ,
17- type MessageStream ,
17+ MessageStream ,
1818 MongoNetworkError ,
1919 MongoNetworkTimeoutError ,
2020 MongoRuntimeError ,
21+ Msg ,
2122 ns ,
22- type OperationDescription
23+ type OperationDescription ,
24+ Query
2325} from '../../mongodb' ;
2426import * as mock from '../../tools/mongodb-mock/index' ;
2527import { generateOpMsgBuffer , getSymbolFrom } from '../../tools/utils' ;
@@ -1027,4 +1029,109 @@ describe('new Connection()', function () {
10271029 } ) ;
10281030 } ) ;
10291031 } ) ;
1032+
1033+ describe ( 'when load-balanced' , ( ) => {
1034+ const CONNECT_DEFAULTS = {
1035+ id : 1 ,
1036+ tls : false ,
1037+ generation : 1 ,
1038+ monitorCommands : false ,
1039+ metadata : { } as ClientMetadata
1040+ } ;
1041+ let server ;
1042+ let connectOptions ;
1043+ let connection : Connection ;
1044+ let writeCommandSpy ;
1045+
1046+ beforeEach ( async ( ) => {
1047+ server = await mock . createServer ( ) ;
1048+ server . setMessageHandler ( request => {
1049+ request . reply ( mock . HELLO ) ;
1050+ } ) ;
1051+ writeCommandSpy = sinon . spy ( MessageStream . prototype , 'writeCommand' ) ;
1052+ } ) ;
1053+
1054+ afterEach ( async ( ) => {
1055+ connection ?. destroy ( { force : true } ) ;
1056+ sinon . restore ( ) ;
1057+ await mock . cleanup ( ) ;
1058+ } ) ;
1059+
1060+ it ( 'sends the first command as OP_MSG' , async ( ) => {
1061+ try {
1062+ connectOptions = {
1063+ ...CONNECT_DEFAULTS ,
1064+ hostAddress : server . hostAddress ( ) as HostAddress ,
1065+ socketTimeoutMS : 100 ,
1066+ loadBalanced : true
1067+ } ;
1068+
1069+ connection = await promisify < Connection > ( callback =>
1070+ //@ts -expect-error: Callbacks do not have mutual exclusion for error/result existence
1071+ connect ( connectOptions , callback )
1072+ ) ( ) ;
1073+
1074+ await promisify ( callback =>
1075+ connection . command ( ns ( 'admin.$cmd' ) , { hello : 1 } , { } , callback )
1076+ ) ( ) ;
1077+ } catch ( error ) {
1078+ /** Connection timeouts, but the handshake message is sent. */
1079+ }
1080+
1081+ expect ( writeCommandSpy ) . to . have . been . called ;
1082+ expect ( writeCommandSpy . firstCall . args [ 0 ] instanceof Msg ) . to . equal ( true ) ;
1083+ } ) ;
1084+ } ) ;
1085+
1086+ describe ( 'when not load-balanced' , ( ) => {
1087+ const CONNECT_DEFAULTS = {
1088+ id : 1 ,
1089+ tls : false ,
1090+ generation : 1 ,
1091+ monitorCommands : false ,
1092+ metadata : { } as ClientMetadata
1093+ } ;
1094+ let server ;
1095+ let connectOptions ;
1096+ let connection : Connection ;
1097+ let writeCommandSpy ;
1098+
1099+ beforeEach ( async ( ) => {
1100+ server = await mock . createServer ( ) ;
1101+ server . setMessageHandler ( request => {
1102+ request . reply ( mock . HELLO ) ;
1103+ } ) ;
1104+ writeCommandSpy = sinon . spy ( MessageStream . prototype , 'writeCommand' ) ;
1105+ } ) ;
1106+
1107+ afterEach ( async ( ) => {
1108+ connection ?. destroy ( { force : true } ) ;
1109+ sinon . restore ( ) ;
1110+ await mock . cleanup ( ) ;
1111+ } ) ;
1112+
1113+ it ( 'sends the first command as OP_QUERY' , async ( ) => {
1114+ try {
1115+ connectOptions = {
1116+ ...CONNECT_DEFAULTS ,
1117+ hostAddress : server . hostAddress ( ) as HostAddress ,
1118+ socketTimeoutMS : 100
1119+ } ;
1120+
1121+ connection = await promisify < Connection > ( callback =>
1122+ //@ts -expect-error: Callbacks do not have mutual exclusion for error/result existence
1123+ connect ( connectOptions , callback )
1124+ ) ( ) ;
1125+
1126+ await promisify ( callback =>
1127+ connection . command ( ns ( 'admin.$cmd' ) , { hello : 1 } , { } , callback )
1128+ ) ( ) ;
1129+ } catch ( error ) {
1130+ /** Connection timeouts, but the handshake message is sent. */
1131+ }
1132+
1133+ expect ( writeCommandSpy ) . to . have . been . called ;
1134+ expect ( writeCommandSpy . firstCall . args [ 0 ] instanceof Query ) . to . equal ( true ) ;
1135+ } ) ;
1136+ } ) ;
10301137} ) ;
0 commit comments