@@ -1420,6 +1420,52 @@ func TestConnExecBatch(t *testing.T) {
1420
1420
assert .Equal (t , "SELECT 1" , results [2 ].CommandTag .String ())
1421
1421
}
1422
1422
1423
+ type mockConnection struct {
1424
+ net.Conn
1425
+ writeLatency * time.Duration
1426
+ }
1427
+
1428
+ func (m mockConnection ) Write (b []byte ) (n int , err error ) {
1429
+ time .Sleep (* m .writeLatency )
1430
+ return m .Conn .Write (b )
1431
+ }
1432
+
1433
+ func TestConnExecBatchWriteError (t * testing.T ) {
1434
+ t .Parallel ()
1435
+
1436
+ ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
1437
+ defer cancel ()
1438
+
1439
+ config , err := pgconn .ParseConfig (os .Getenv ("PGX_TEST_DATABASE" ))
1440
+ require .NoError (t , err )
1441
+
1442
+ var mockConn mockConnection
1443
+ writeLatency := 0 * time .Second
1444
+ config .DialFunc = func (ctx context.Context , network , address string ) (net.Conn , error ) {
1445
+ conn , err := net .Dial (network , address )
1446
+ mockConn = mockConnection {conn , & writeLatency }
1447
+ return mockConn , err
1448
+ }
1449
+
1450
+ pgConn , err := pgconn .ConnectConfig (ctx , config )
1451
+ require .NoError (t , err )
1452
+ defer closeConn (t , pgConn )
1453
+
1454
+ batch := & pgconn.Batch {}
1455
+ pgConn .Conn ()
1456
+
1457
+ ctx2 , cancel2 := context .WithTimeout (context .Background (), 1 * time .Second )
1458
+ defer cancel2 ()
1459
+
1460
+ batch .ExecParams ("select $1::text" , [][]byte {[]byte ("ExecParams 1" )}, nil , nil , nil )
1461
+ writeLatency = 2 * time .Second
1462
+ mrr := pgConn .ExecBatch (ctx2 , batch )
1463
+ err = mrr .Close ()
1464
+ require .Error (t , err )
1465
+ assert .ErrorIs (t , err , context .DeadlineExceeded )
1466
+ require .True (t , pgConn .IsClosed ())
1467
+ }
1468
+
1423
1469
func TestConnExecBatchDeferredError (t * testing.T ) {
1424
1470
t .Parallel ()
1425
1471
0 commit comments