@@ -19,6 +19,7 @@ import (
19
19
20
20
"github.com/google/go-cmp/cmp"
21
21
"go.mongodb.org/mongo-driver/v2/internal/assert"
22
+ "go.mongodb.org/mongo-driver/v2/internal/require"
22
23
"go.mongodb.org/mongo-driver/v2/mongo/address"
23
24
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
24
25
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/description"
@@ -278,7 +279,7 @@ func TestConnection(t *testing.T) {
278
279
279
280
want := []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A }
280
281
err := conn .writeWireMessage (context .Background (), want )
281
- noerr (t , err )
282
+ require . NoError (t , err )
282
283
got := tnc .buf
283
284
if ! cmp .Equal (got , want ) {
284
285
t .Errorf ("writeWireMessage did not write the proper bytes. got %v; want %v" , got , want )
@@ -471,7 +472,7 @@ func TestConnection(t *testing.T) {
471
472
conn .cancellationListener = listener
472
473
473
474
got , err := conn .readWireMessage (context .Background ())
474
- noerr (t , err )
475
+ require . NoError (t , err )
475
476
if ! cmp .Equal (got , want ) {
476
477
t .Errorf ("did not read full wire message. got %v; want %v" , got , want )
477
478
}
@@ -1099,3 +1100,85 @@ func (tcl *testCancellationListener) assertCalledOnce(t *testing.T) {
1099
1100
assert .Equal (t , 1 , tcl .numListen , "expected Listen to be called once, got %d" , tcl .numListen )
1100
1101
assert .Equal (t , 1 , tcl .numStopListening , "expected StopListening to be called once, got %d" , tcl .numListen )
1101
1102
}
1103
+
1104
+ func TestConnection_IsAlive (t * testing.T ) {
1105
+ t .Parallel ()
1106
+
1107
+ t .Run ("uninitialized" , func (t * testing.T ) {
1108
+ t .Parallel ()
1109
+
1110
+ conn := newConnection ("" )
1111
+ assert .False (t ,
1112
+ conn .isAlive (),
1113
+ "expected isAlive for an uninitialized connection to always return false" )
1114
+ })
1115
+
1116
+ t .Run ("connection open" , func (t * testing.T ) {
1117
+ t .Parallel ()
1118
+
1119
+ cleanup := make (chan struct {})
1120
+ defer close (cleanup )
1121
+ addr := bootstrapConnections (t , 1 , func (nc net.Conn ) {
1122
+ // Keep the connection open until the end of the test.
1123
+ <- cleanup
1124
+ _ = nc .Close ()
1125
+ })
1126
+
1127
+ conn := newConnection (address .Address (addr .String ()))
1128
+ err := conn .connect (context .Background ())
1129
+ require .NoError (t , err )
1130
+
1131
+ conn .idleStart .Store (time .Now ().Add (- 11 * time .Second ))
1132
+ assert .True (t ,
1133
+ conn .isAlive (),
1134
+ "expected isAlive for an open connection to return true" )
1135
+ })
1136
+
1137
+ t .Run ("connection closed" , func (t * testing.T ) {
1138
+ t .Parallel ()
1139
+
1140
+ conns := make (chan net.Conn )
1141
+ addr := bootstrapConnections (t , 1 , func (nc net.Conn ) {
1142
+ conns <- nc
1143
+ })
1144
+
1145
+ conn := newConnection (address .Address (addr .String ()))
1146
+ err := conn .connect (context .Background ())
1147
+ require .NoError (t , err )
1148
+
1149
+ // Close the connection before calling isAlive.
1150
+ nc := <- conns
1151
+ err = nc .Close ()
1152
+ require .NoError (t , err )
1153
+
1154
+ conn .idleStart .Store (time .Now ().Add (- 11 * time .Second ))
1155
+ assert .False (t ,
1156
+ conn .isAlive (),
1157
+ "expected isAlive for a closed connection to return false" )
1158
+ })
1159
+
1160
+ t .Run ("connection reads data" , func (t * testing.T ) {
1161
+ t .Parallel ()
1162
+
1163
+ cleanup := make (chan struct {})
1164
+ defer close (cleanup )
1165
+ addr := bootstrapConnections (t , 1 , func (nc net.Conn ) {
1166
+ // Write some data to the connection before calling isAlive.
1167
+ _ , err := nc .Write ([]byte {5 , 0 , 0 , 0 , 0 })
1168
+ require .NoError (t , err )
1169
+
1170
+ // Keep the connection open until the end of the test.
1171
+ <- cleanup
1172
+ _ = nc .Close ()
1173
+ })
1174
+
1175
+ conn := newConnection (address .Address (addr .String ()))
1176
+ err := conn .connect (context .Background ())
1177
+ require .NoError (t , err )
1178
+
1179
+ conn .idleStart .Store (time .Now ().Add (- 11 * time .Second ))
1180
+ assert .False (t ,
1181
+ conn .isAlive (),
1182
+ "expected isAlive for an open connection that reads data to return false" )
1183
+ })
1184
+ }
0 commit comments