@@ -53,6 +53,11 @@ func TestCommitAborted(t *testing.T) {
53
53
if g , w := len (commitReqs ), 2 ; g != w {
54
54
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
55
55
}
56
+
57
+ // Verify that the db is still usable.
58
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
59
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
60
+ }
56
61
}
57
62
58
63
func TestCommitAbortedWithInternalRetriesDisabled (t * testing.T ) {
@@ -80,6 +85,11 @@ func TestCommitAbortedWithInternalRetriesDisabled(t *testing.T) {
80
85
if g , w := len (commitReqs ), 1 ; g != w {
81
86
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
82
87
}
88
+
89
+ // Verify that the db is still usable.
90
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
91
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
92
+ }
83
93
}
84
94
85
95
func TestUpdateAborted (t * testing.T ) {
@@ -121,6 +131,11 @@ func TestUpdateAborted(t *testing.T) {
121
131
if g , w := len (commitReqs ), 1 ; g != w {
122
132
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
123
133
}
134
+
135
+ // Verify that the db is still usable.
136
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
137
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
138
+ }
124
139
}
125
140
126
141
func TestBatchUpdateAborted (t * testing.T ) {
@@ -160,6 +175,11 @@ func TestBatchUpdateAborted(t *testing.T) {
160
175
if g , w := len (commitReqs ), 1 ; g != w {
161
176
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
162
177
}
178
+
179
+ // Verify that the db is still usable.
180
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
181
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
182
+ }
163
183
}
164
184
165
185
func TestQueryAborted (t * testing.T ) {
@@ -172,7 +192,7 @@ func TestQueryAborted(t *testing.T) {
172
192
173
193
func TestEmptyQueryAbortedTwice (t * testing.T ) {
174
194
testRetryReadWriteTransactionWithQueryWithRetrySuccess (t , func (server testutil.InMemSpannerServer ) {
175
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
195
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
176
196
Type : testutil .StatementResultResultSet ,
177
197
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {}, "FOO" ),
178
198
})
@@ -243,7 +263,7 @@ func TestQueryConsumedHalfway_RetryContainsMoreResults_CommitAborted(t *testing.
243
263
})
244
264
}, codes .OK , 2 , 2 , 2 ,
245
265
func (server testutil.InMemSpannerServer ) {
246
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
266
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
247
267
Type : testutil .StatementResultResultSet ,
248
268
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {1 , 2 , 3 }, "FOO" ),
249
269
})
@@ -253,7 +273,7 @@ func TestQueryConsumedHalfway_RetryContainsMoreResults_CommitAborted(t *testing.
253
273
func TestQueryWithError_CommitAborted (t * testing.T ) {
254
274
testRetryReadWriteTransactionWithQueryWithRetrySuccess (t , func (server testutil.InMemSpannerServer ) {
255
275
// Let the query return a Table not found error.
256
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
276
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
257
277
Type : testutil .StatementResultError ,
258
278
Err : status .Errorf (codes .NotFound , "Table not found" ),
259
279
})
@@ -287,7 +307,7 @@ func TestQueryAbortedWithMoreResultsDuringRetry(t *testing.T) {
287
307
})
288
308
// Replace the original query result with a new one with an additional row
289
309
// before the transaction is committed. This will cause the retry to fail.
290
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
310
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
291
311
Type : testutil .StatementResultResultSet ,
292
312
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {1 , 2 , 3 }, "FOO" ),
293
313
})
@@ -303,7 +323,7 @@ func TestQueryAbortedWithLessResultsDuringRetry(t *testing.T) {
303
323
})
304
324
// Replace the original query result with a new one with an additional row
305
325
// before the transaction is committed. This will cause the retry to fail.
306
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
326
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
307
327
Type : testutil .StatementResultResultSet ,
308
328
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {1 }, "FOO" ),
309
329
})
@@ -315,7 +335,7 @@ func TestQueryAbortedWithLessResultsDuringRetry(t *testing.T) {
315
335
func TestQueryWithEmptyResult_CommitAborted (t * testing.T ) {
316
336
testRetryReadWriteTransactionWithQueryWithRetrySuccess (t , func (server testutil.InMemSpannerServer ) {
317
337
// Let the query return an empty result set with only one column.
318
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
338
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
319
339
Type : testutil .StatementResultResultSet ,
320
340
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {}, "FOO" ),
321
341
})
@@ -330,7 +350,7 @@ func TestQueryWithEmptyResult_CommitAborted(t *testing.T) {
330
350
func TestQueryWithNewColumn_CommitAborted (t * testing.T ) {
331
351
testRetryReadWriteTransactionWithQuery (t , func (server testutil.InMemSpannerServer ) {
332
352
// Let the query return an empty result set with only one column.
333
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
353
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
334
354
Type : testutil .StatementResultResultSet ,
335
355
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {}, "FOO" ),
336
356
})
@@ -341,7 +361,7 @@ func TestQueryWithNewColumn_CommitAborted(t *testing.T) {
341
361
func (server testutil.InMemSpannerServer ) {
342
362
// Let the query return an empty result set with two columns during the retry.
343
363
// This should cause a retry failure.
344
- server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
364
+ _ = server .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
345
365
Type : testutil .StatementResultResultSet ,
346
366
ResultSet : testutil .CreateTwoColumnResultSet ([][2 ]int64 {}, [2 ]string {"FOO" , "BAR" }),
347
367
})
@@ -366,6 +386,7 @@ func testRetryReadWriteTransactionWithQuery(t *testing.T, setupServer func(serve
366
386
367
387
db , server , teardown := setupTestDBConnectionWithParams (t , "minSessions=1;maxSessions=1" )
368
388
defer teardown ()
389
+ db .SetMaxOpenConns (1 )
369
390
370
391
if setupServer != nil {
371
392
setupServer (server .TestSpanner )
@@ -431,6 +452,12 @@ func testRetryReadWriteTransactionWithQuery(t *testing.T, setupServer func(serve
431
452
if g , w := len (commitReqs ), wantCommitCount ; g != w {
432
453
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
433
454
}
455
+
456
+ // Execute another statement to ensure that the session that was used
457
+ // has been returned to the pool.
458
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
459
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
460
+ }
434
461
}
435
462
436
463
// Tests that a query that is aborted halfway the stream will be retried,
@@ -439,15 +466,17 @@ func testRetryReadWriteTransactionWithQuery(t *testing.T, setupServer func(serve
439
466
func TestQueryAbortedHalfway_WithDifferentResultsInFirstHalf (t * testing.T ) {
440
467
t .Parallel ()
441
468
442
- db , server , teardown := setupTestDBConnection ( t )
469
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
443
470
defer teardown ()
471
+ db .SetMaxOpenConns (1 )
444
472
// Ensure that the second call to Next() will fail with an Aborted error.
445
473
server .TestSpanner .AddPartialResultSetError (testutil .SelectFooFromBar , testutil.PartialResultSetExecutionTime {
446
474
ResumeToken : testutil .EncodeResumeToken (2 ),
447
475
Err : status .Error (codes .Aborted , "Aborted" ),
448
476
})
449
477
450
- ctx := context .Background ()
478
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
479
+ defer cancel ()
451
480
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
452
481
if err != nil {
453
482
t .Fatalf ("begin failed: %v" , err )
@@ -477,7 +506,7 @@ func TestQueryAbortedHalfway_WithDifferentResultsInFirstHalf(t *testing.T) {
477
506
// Replace the original query result with a new one with a different value
478
507
// for the first row. This should cause the transaction to fail with an
479
508
// ErrAbortedDueToConcurrentModification error.
480
- server .TestSpanner .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
509
+ _ = server .TestSpanner .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
481
510
Type : testutil .StatementResultResultSet ,
482
511
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {2 , 2 }, "FOO" ),
483
512
})
@@ -490,28 +519,38 @@ func TestQueryAbortedHalfway_WithDifferentResultsInFirstHalf(t *testing.T) {
490
519
if g , w := rows .Err (), ErrAbortedDueToConcurrentModification ; g != w {
491
520
t .Fatalf ("next error mismatch\n Got: %v\n Want: %v" , g , w )
492
521
}
522
+ if err := tx .Rollback (); err != nil {
523
+ t .Fatalf ("failed to rollback transaction: %v" , err )
524
+ }
493
525
494
526
reqs := drainRequestsFromServer (server .TestSpanner )
495
527
execReqs := requestsOfType (reqs , reflect .TypeOf (& sppb.ExecuteSqlRequest {}))
496
528
if g , w := len (execReqs ), 2 ; g != w {
497
529
t .Fatalf ("execute request count mismatch\n Got: %v\n Want: %v" , g , w )
498
530
}
531
+
532
+ // Verify that the db is still usable.
533
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
534
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
535
+ }
499
536
}
500
537
501
538
// Tests that a change in the results of a result set that has not yet been seen
502
539
// by the user does not cause a retry failure.
503
540
func TestQueryAbortedHalfway_WithDifferentResultsInSecondHalf (t * testing.T ) {
504
541
t .Parallel ()
505
542
506
- db , server , teardown := setupTestDBConnection ( t )
543
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
507
544
defer teardown ()
545
+ db .SetMaxOpenConns (1 )
508
546
// Ensure that the second call to Next() will fail with an Aborted error.
509
547
server .TestSpanner .AddPartialResultSetError (testutil .SelectFooFromBar , testutil.PartialResultSetExecutionTime {
510
548
ResumeToken : testutil .EncodeResumeToken (2 ),
511
549
Err : status .Error (codes .Aborted , "Aborted" ),
512
550
})
513
551
514
- ctx := context .Background ()
552
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
553
+ defer cancel ()
515
554
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
516
555
if err != nil {
517
556
t .Fatalf ("begin failed: %v" , err )
@@ -542,7 +581,7 @@ func TestQueryAbortedHalfway_WithDifferentResultsInSecondHalf(t *testing.T) {
542
581
// for the second row. This should not cause the transaction to fail with an
543
582
// ErrAbortedDueToConcurrentModification error as the result has not yet
544
583
// been seen by the user.
545
- server .TestSpanner .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
584
+ _ = server .TestSpanner .PutStatementResult (testutil .SelectFooFromBar , & testutil.StatementResult {
546
585
Type : testutil .StatementResultResultSet ,
547
586
ResultSet : testutil .CreateSingleColumnInt64ResultSet ([]int64 {1 , 3 }, "FOO" ),
548
587
})
@@ -563,21 +602,31 @@ func TestQueryAbortedHalfway_WithDifferentResultsInSecondHalf(t *testing.T) {
563
602
if g , w := val , int64 (3 ); g != w {
564
603
t .Fatalf ("value mismatch\n Got: %v\n Want: %v" , g , w )
565
604
}
605
+ if err := tx .Commit (); err != nil {
606
+ t .Fatalf ("failed to commit transaction: %v" , err )
607
+ }
566
608
567
609
reqs := drainRequestsFromServer (server .TestSpanner )
568
610
execReqs := requestsOfType (reqs , reflect .TypeOf (& sppb.ExecuteSqlRequest {}))
569
611
if g , w := len (execReqs ), 2 ; g != w {
570
612
t .Fatalf ("execute request count mismatch\n Got: %v\n Want: %v" , g , w )
571
613
}
614
+
615
+ // Verify that the db is still usable.
616
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
617
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
618
+ }
572
619
}
573
620
574
621
func TestSecondUpdateAborted (t * testing.T ) {
575
622
t .Parallel ()
576
623
577
- db , server , teardown := setupTestDBConnection ( t )
624
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
578
625
defer teardown ()
626
+ db .SetMaxOpenConns (1 )
579
627
580
- ctx := context .Background ()
628
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
629
+ defer cancel ()
581
630
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
582
631
if err != nil {
583
632
t .Fatalf ("begin failed: %v" , err )
@@ -619,15 +668,22 @@ func TestSecondUpdateAborted(t *testing.T) {
619
668
if g , w := len (commitReqs ), 1 ; g != w {
620
669
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
621
670
}
671
+
672
+ // Verify that the db is still usable.
673
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
674
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
675
+ }
622
676
}
623
677
624
678
func TestSecondBatchUpdateAborted (t * testing.T ) {
625
679
t .Parallel ()
626
680
627
- db , server , teardown := setupTestDBConnection ( t )
681
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
628
682
defer teardown ()
683
+ db .SetMaxOpenConns (1 )
629
684
630
- ctx := context .Background ()
685
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
686
+ defer cancel ()
631
687
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
632
688
if err != nil {
633
689
t .Fatalf ("begin failed: %v" , err )
@@ -671,20 +727,27 @@ func TestSecondBatchUpdateAborted(t *testing.T) {
671
727
if g , w := len (commitReqs ), 1 ; g != w {
672
728
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
673
729
}
730
+
731
+ // Verify that the db is still usable.
732
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
733
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
734
+ }
674
735
}
675
736
676
737
func TestSecondUpdateAborted_FirstStatementWithSameError (t * testing.T ) {
677
738
t .Parallel ()
678
739
679
- db , server , teardown := setupTestDBConnection ( t )
740
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
680
741
defer teardown ()
742
+ db .SetMaxOpenConns (1 )
681
743
682
- ctx := context .Background ()
744
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
745
+ defer cancel ()
683
746
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
684
747
if err != nil {
685
748
t .Fatalf ("begin failed: %v" , err )
686
749
}
687
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
750
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
688
751
Type : testutil .StatementResultError ,
689
752
Err : status .Error (codes .NotFound , "Table not found" ),
690
753
})
@@ -726,6 +789,11 @@ func TestSecondUpdateAborted_FirstStatementWithSameError(t *testing.T) {
726
789
if g , w := len (commitReqs ), 1 ; g != w {
727
790
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
728
791
}
792
+
793
+ // Verify that the db is still usable.
794
+ if _ , err := db .ExecContext (ctx , testutil .UpdateBarSetFoo ); err != nil {
795
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
796
+ }
729
797
}
730
798
731
799
func TestSecondUpdateAborted_FirstResultUpdateCountChanged (t * testing.T ) {
@@ -769,13 +837,14 @@ func TestSecondUpdateAborted_FirstResultFromErrorToOtherError(t *testing.T) {
769
837
func testSecondUpdateAborted_FirstResultChanged (t * testing.T , firstResult * testutil.StatementResult , secondResult * testutil.StatementResult ) {
770
838
t .Parallel ()
771
839
772
- db , server , teardown := setupTestDBConnection ( t )
840
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
773
841
defer teardown ()
774
842
if firstResult != nil {
775
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , firstResult )
843
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , firstResult )
776
844
}
777
845
778
- ctx := context .Background ()
846
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
847
+ defer cancel ()
779
848
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
780
849
if err != nil {
781
850
t .Fatalf ("begin failed: %v" , err )
@@ -784,7 +853,7 @@ func testSecondUpdateAborted_FirstResultChanged(t *testing.T, firstResult *testu
784
853
// the result is different during the retry.
785
854
_ , _ = tx .ExecContext (ctx , testutil .UpdateSingersSetLastName )
786
855
// Update the result to simulate a different result during the retry.
787
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , secondResult )
856
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , secondResult )
788
857
789
858
server .TestSpanner .PutExecutionTime (testutil .MethodExecuteStreamingSql , testutil.SimulatedExecutionTime {
790
859
Errors : []error {status .Error (codes .Aborted , "Aborted" )},
@@ -795,27 +864,37 @@ func testSecondUpdateAborted_FirstResultChanged(t *testing.T, firstResult *testu
795
864
if err != ErrAbortedDueToConcurrentModification {
796
865
t .Fatalf ("update error mismatch\n Got: %v\n Want: %v" , err , ErrAbortedDueToConcurrentModification )
797
866
}
867
+ if err := tx .Rollback (); err != nil {
868
+ t .Fatalf ("failed to rollback transaction: %v" , err )
869
+ }
798
870
reqs := drainRequestsFromServer (server .TestSpanner )
799
871
execReqs := requestsOfType (reqs , reflect .TypeOf (& sppb.ExecuteSqlRequest {}))
800
872
// The server should receive 3 execute statements, as only the first statement is retried.
801
873
if g , w := len (execReqs ), 3 ; g != w {
802
874
t .Fatalf ("execute request count mismatch\n Got: %v\n Want: %v" , g , w )
803
875
}
876
+
877
+ // Verify that the db is still usable.
878
+ if _ , err := db .ExecContext (ctx , testutil .UpdateBarSetFoo ); err != nil {
879
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
880
+ }
804
881
}
805
882
806
883
func TestBatchUpdateAbortedWithError (t * testing.T ) {
807
884
t .Parallel ()
808
885
809
- db , server , teardown := setupTestDBConnection ( t )
886
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
810
887
defer teardown ()
888
+ db .SetMaxOpenConns (1 )
811
889
812
890
// Make sure that one of the DML statements will return an error.
813
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
891
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
814
892
Type : testutil .StatementResultError ,
815
893
Err : status .Error (codes .NotFound , "Table not found" ),
816
894
})
817
895
818
- ctx := context .Background ()
896
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
897
+ defer cancel ()
819
898
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
820
899
if err != nil {
821
900
t .Fatalf ("begin failed: %v" , err )
@@ -858,21 +937,28 @@ func TestBatchUpdateAbortedWithError(t *testing.T) {
858
937
if g , w := len (commitReqs ), 2 ; g != w {
859
938
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
860
939
}
940
+
941
+ // Verify that the db is still usable.
942
+ if _ , err := db .ExecContext (ctx , testutil .UpdateBarSetFoo ); err != nil {
943
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
944
+ }
861
945
}
862
946
863
947
func TestBatchUpdateAbortedWithError_DifferentRowCountDuringRetry (t * testing.T ) {
864
948
t .Parallel ()
865
949
866
- db , server , teardown := setupTestDBConnection ( t )
950
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
867
951
defer teardown ()
952
+ db .SetMaxOpenConns (1 )
868
953
869
954
// Make sure that one of the DML statements will return an error.
870
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
955
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
871
956
Type : testutil .StatementResultError ,
872
957
Err : status .Error (codes .NotFound , "Table not found" ),
873
958
})
874
959
875
- ctx := context .Background ()
960
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
961
+ defer cancel ()
876
962
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
877
963
if err != nil {
878
964
t .Fatalf ("begin failed: %v" , err )
@@ -895,7 +981,7 @@ func TestBatchUpdateAbortedWithError_DifferentRowCountDuringRetry(t *testing.T)
895
981
896
982
// Change the returned row count of the first DML statement. This will cause
897
983
// the retry to fail.
898
- server .TestSpanner .PutStatementResult (testutil .UpdateBarSetFoo , & testutil.StatementResult {
984
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateBarSetFoo , & testutil.StatementResult {
899
985
Type : testutil .StatementResultUpdateCount ,
900
986
UpdateCount : testutil .UpdateBarSetFooRowCount + 1 ,
901
987
})
@@ -916,21 +1002,28 @@ func TestBatchUpdateAbortedWithError_DifferentRowCountDuringRetry(t *testing.T)
916
1002
if g , w := len (commitReqs ), 1 ; g != w {
917
1003
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
918
1004
}
1005
+
1006
+ // Verify that the db is still usable.
1007
+ if _ , err := db .ExecContext (ctx , testutil .UpdateBarSetFoo ); err != nil {
1008
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
1009
+ }
919
1010
}
920
1011
921
1012
func TestBatchUpdateAbortedWithError_DifferentErrorDuringRetry (t * testing.T ) {
922
1013
t .Parallel ()
923
1014
924
- db , server , teardown := setupTestDBConnection ( t )
1015
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
925
1016
defer teardown ()
1017
+ db .SetMaxOpenConns (1 )
926
1018
927
1019
// Make sure that one of the DML statements will return an error.
928
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
1020
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
929
1021
Type : testutil .StatementResultError ,
930
1022
Err : status .Error (codes .NotFound , "Table not found" ),
931
1023
})
932
1024
933
- ctx := context .Background ()
1025
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
1026
+ defer cancel ()
934
1027
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
935
1028
if err != nil {
936
1029
t .Fatalf ("begin failed: %v" , err )
@@ -947,7 +1040,7 @@ func TestBatchUpdateAbortedWithError_DifferentErrorDuringRetry(t *testing.T) {
947
1040
948
1041
// Remove the error for the DML statement and cause a retry. The missing
949
1042
// error for the DML statement should fail the retry.
950
- server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
1043
+ _ = server .TestSpanner .PutStatementResult (testutil .UpdateSingersSetLastName , & testutil.StatementResult {
951
1044
Type : testutil .StatementResultUpdateCount ,
952
1045
UpdateCount : testutil .UpdateSingersSetLastNameRowCount ,
953
1046
})
@@ -968,13 +1061,19 @@ func TestBatchUpdateAbortedWithError_DifferentErrorDuringRetry(t *testing.T) {
968
1061
if g , w := len (commitReqs ), 1 ; g != w {
969
1062
t .Fatalf ("commit request count mismatch\n Got: %v\n Want: %v" , g , w )
970
1063
}
1064
+
1065
+ // Verify that the db is still usable.
1066
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
1067
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
1068
+ }
971
1069
}
972
1070
973
1071
func TestLastInsertId (t * testing.T ) {
974
1072
t .Parallel ()
975
1073
976
- db , server , teardown := setupTestDBConnection ( t )
1074
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
977
1075
defer teardown ()
1076
+ db .SetMaxOpenConns (1 )
978
1077
979
1078
query := "insert into singers (name) values ('foo') then return id"
980
1079
_ = server .TestSpanner .PutStatementResult (query , & testutil.StatementResult {
@@ -983,7 +1082,8 @@ func TestLastInsertId(t *testing.T) {
983
1082
UpdateCount : 1 ,
984
1083
})
985
1084
986
- ctx := context .Background ()
1085
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
1086
+ defer cancel ()
987
1087
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
988
1088
if err != nil {
989
1089
t .Fatalf ("begin failed: %v" , err )
@@ -1005,13 +1105,19 @@ func TestLastInsertId(t *testing.T) {
1005
1105
if err := tx .Commit (); err != nil {
1006
1106
t .Fatalf ("failed to commit: %v" , err )
1007
1107
}
1108
+
1109
+ // Verify that the db is still usable.
1110
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
1111
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
1112
+ }
1008
1113
}
1009
1114
1010
1115
func TestLastInsertIdChanged (t * testing.T ) {
1011
1116
t .Parallel ()
1012
1117
1013
- db , server , teardown := setupTestDBConnection ( t )
1118
+ db , server , teardown := setupTestDBConnectionWithParams ( t , "minSessions=1;maxSessions=1" )
1014
1119
defer teardown ()
1120
+ db .SetMaxOpenConns (1 )
1015
1121
1016
1122
query := "insert into singers (name) values ('foo') then return id"
1017
1123
_ = server .TestSpanner .PutStatementResult (query , & testutil.StatementResult {
@@ -1020,7 +1126,8 @@ func TestLastInsertIdChanged(t *testing.T) {
1020
1126
UpdateCount : 1 ,
1021
1127
})
1022
1128
1023
- ctx := context .Background ()
1129
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
1130
+ defer cancel ()
1024
1131
tx , err := db .BeginTx (ctx , & sql.TxOptions {})
1025
1132
if err != nil {
1026
1133
t .Fatalf ("begin failed: %v" , err )
@@ -1051,6 +1158,11 @@ func TestLastInsertIdChanged(t *testing.T) {
1051
1158
if err := tx .Commit (); err != ErrAbortedDueToConcurrentModification {
1052
1159
t .Fatalf ("commit error mismatch\n Got: %v\n Want: %v" , err , ErrAbortedDueToConcurrentModification )
1053
1160
}
1161
+
1162
+ // Verify that the db is still usable.
1163
+ if _ , err := db .ExecContext (ctx , testutil .UpdateSingersSetLastName ); err != nil {
1164
+ t .Fatalf ("failed to execute statement after transaction: %v" , err )
1165
+ }
1054
1166
}
1055
1167
1056
1168
func firstNonZero (values ... int ) int {
0 commit comments