@@ -19,6 +19,7 @@ import (
1919
2020 "github.com/google/go-cmp/cmp"
2121 "go.mongodb.org/mongo-driver/internal/assert"
22+ "go.mongodb.org/mongo-driver/internal/require"
2223 "go.mongodb.org/mongo-driver/mongo/address"
2324 "go.mongodb.org/mongo-driver/mongo/description"
2425 "go.mongodb.org/mongo-driver/x/mongo/driver"
@@ -427,7 +428,7 @@ func TestConnection(t *testing.T) {
427428
428429 want := []byte {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A }
429430 err := conn .writeWireMessage (context .Background (), want )
430- noerr (t , err )
431+ require . NoError (t , err )
431432 got := tnc .buf
432433 if ! cmp .Equal (got , want ) {
433434 t .Errorf ("writeWireMessage did not write the proper bytes. got %v; want %v" , got , want )
@@ -624,7 +625,7 @@ func TestConnection(t *testing.T) {
624625 conn .cancellationListener = listener
625626
626627 got , err := conn .readWireMessage (context .Background ())
627- noerr (t , err )
628+ require . NoError (t , err )
628629 if ! cmp .Equal (got , want ) {
629630 t .Errorf ("did not read full wire message. got %v; want %v" , got , want )
630631 }
@@ -1251,3 +1252,85 @@ func (tcl *testCancellationListener) assertCalledOnce(t *testing.T) {
12511252 assert .Equal (t , 1 , tcl .numListen , "expected Listen to be called once, got %d" , tcl .numListen )
12521253 assert .Equal (t , 1 , tcl .numStopListening , "expected StopListening to be called once, got %d" , tcl .numListen )
12531254}
1255+
1256+ func TestConnection_IsAlive (t * testing.T ) {
1257+ t .Parallel ()
1258+
1259+ t .Run ("uninitialized" , func (t * testing.T ) {
1260+ t .Parallel ()
1261+
1262+ conn := newConnection ("" )
1263+ assert .False (t ,
1264+ conn .isAlive (),
1265+ "expected isAlive for an uninitialized connection to always return false" )
1266+ })
1267+
1268+ t .Run ("connection open" , func (t * testing.T ) {
1269+ t .Parallel ()
1270+
1271+ cleanup := make (chan struct {})
1272+ defer close (cleanup )
1273+ addr := bootstrapConnections (t , 1 , func (nc net.Conn ) {
1274+ // Keep the connection open until the end of the test.
1275+ <- cleanup
1276+ _ = nc .Close ()
1277+ })
1278+
1279+ conn := newConnection (address .Address (addr .String ()))
1280+ err := conn .connect (context .Background ())
1281+ require .NoError (t , err )
1282+
1283+ conn .idleStart .Store (time .Now ().Add (- 11 * time .Second ))
1284+ assert .True (t ,
1285+ conn .isAlive (),
1286+ "expected isAlive for an open connection to return true" )
1287+ })
1288+
1289+ t .Run ("connection closed" , func (t * testing.T ) {
1290+ t .Parallel ()
1291+
1292+ conns := make (chan net.Conn )
1293+ addr := bootstrapConnections (t , 1 , func (nc net.Conn ) {
1294+ conns <- nc
1295+ })
1296+
1297+ conn := newConnection (address .Address (addr .String ()))
1298+ err := conn .connect (context .Background ())
1299+ require .NoError (t , err )
1300+
1301+ // Close the connection before calling isAlive.
1302+ nc := <- conns
1303+ err = nc .Close ()
1304+ require .NoError (t , err )
1305+
1306+ conn .idleStart .Store (time .Now ().Add (- 11 * time .Second ))
1307+ assert .False (t ,
1308+ conn .isAlive (),
1309+ "expected isAlive for a closed connection to return false" )
1310+ })
1311+
1312+ t .Run ("connection reads data" , func (t * testing.T ) {
1313+ t .Parallel ()
1314+
1315+ cleanup := make (chan struct {})
1316+ defer close (cleanup )
1317+ addr := bootstrapConnections (t , 1 , func (nc net.Conn ) {
1318+ // Write some data to the connection before calling isAlive.
1319+ _ , err := nc .Write ([]byte {5 , 0 , 0 , 0 , 0 })
1320+ require .NoError (t , err )
1321+
1322+ // Keep the connection open until the end of the test.
1323+ <- cleanup
1324+ _ = nc .Close ()
1325+ })
1326+
1327+ conn := newConnection (address .Address (addr .String ()))
1328+ err := conn .connect (context .Background ())
1329+ require .NoError (t , err )
1330+
1331+ conn .idleStart .Store (time .Now ().Add (- 11 * time .Second ))
1332+ assert .False (t ,
1333+ conn .isAlive (),
1334+ "expected isAlive for an open connection that reads data to return false" )
1335+ })
1336+ }
0 commit comments