@@ -16,7 +16,6 @@ import (
16
16
"database/sql/driver"
17
17
"fmt"
18
18
"reflect"
19
- "sync"
20
19
"testing"
21
20
"time"
22
21
)
@@ -476,93 +475,48 @@ func TestContextBeginIsolationLevel(t *testing.T) {
476
475
ctx , cancel := context .WithCancel (context .Background ())
477
476
defer cancel ()
478
477
479
- // Waitgroup syncing BeginTx
480
- beginWg := sync.WaitGroup {}
481
- beginWg .Add (2 )
482
-
483
- // Waitgroup syncing insert in writer transaction
484
- insertWg := sync.WaitGroup {}
485
- insertWg .Add (1 )
486
-
487
- // Waitgroup syncing writer transaction commit before reader reads
488
- readWg := sync.WaitGroup {}
489
- readWg .Add (1 )
490
-
491
- // Waitgroup syncing commit in writer transaction
492
- commitWg := sync.WaitGroup {}
493
- commitWg .Add (1 )
494
-
495
- // Waitgroup syncing end of both goroutines
496
- testDoneWg := sync.WaitGroup {}
497
- testDoneWg .Add (2 )
498
-
499
- repeatableReadGoroutine := func () {
500
- tx , err := dbt .db .BeginTx (ctx , & sql.TxOptions {
501
- Isolation : sql .LevelRepeatableRead ,
502
- })
503
- if err != nil {
504
- dbt .Fatal (err )
505
- }
506
- beginWg .Done ()
507
- // Wait until other session will begin it's transaction
508
- beginWg .Wait ()
509
-
510
- _ , err = tx .ExecContext (ctx , "INSERT INTO test VALUES (1)" )
511
- if err != nil {
512
- dbt .Fatal (err )
513
- }
514
- insertWg .Done ()
478
+ tx1 , err := dbt .db .BeginTx (ctx , & sql.TxOptions {
479
+ Isolation : sql .LevelRepeatableRead ,
480
+ })
481
+ if err != nil {
482
+ dbt .Fatal (err )
483
+ }
515
484
516
- // Wait until reader transaction finish reading
517
- readWg .Wait ()
485
+ tx2 , err := dbt .db .BeginTx (ctx , & sql.TxOptions {
486
+ Isolation : sql .LevelReadCommitted ,
487
+ })
488
+ if err != nil {
489
+ dbt .Fatal (err )
490
+ }
518
491
519
- err = tx .Commit ()
520
- if err != nil {
521
- dbt .Fatal (err )
522
- }
523
- commitWg .Done ()
492
+ _ , err = tx1 .ExecContext (ctx , "INSERT INTO test VALUES (1)" )
493
+ if err != nil {
494
+ dbt .Fatal (err )
495
+ }
524
496
525
- testDoneWg .Done ()
497
+ var v int
498
+ row := tx2 .QueryRowContext (ctx , "SELECT COUNT(*) FROM test" )
499
+ if err := row .Scan (& v ); err != nil {
500
+ dbt .Fatal (err )
501
+ }
502
+ // Because writer transaction wasn't commited yet, it should be available
503
+ if v != 0 {
504
+ dbt .Errorf ("expected val to be 0, got %d" , v )
526
505
}
527
506
528
- readCommitedGoroutine := func () {
529
- tx , err := dbt .db .BeginTx (ctx , & sql.TxOptions {
530
- Isolation : sql .LevelReadCommitted ,
531
- })
532
- if err != nil {
533
- dbt .Fatal (err )
534
- }
535
- beginWg .Done ()
536
- // Wait until writer transaction will begin
537
- beginWg .Wait ()
538
- // Wait until writer transaction will insert value
539
- insertWg .Wait ()
540
- var v int
541
- row := tx .QueryRowContext (ctx , "SELECT COUNT(*) FROM test" )
542
- if err := row .Scan (& v ); err != nil {
543
- dbt .Fatal (err )
544
- }
545
- // Because writer transaction wasn't commited yet, it should be available
546
- if v != 0 {
547
- dbt .Errorf ("expected val to be 0, got %d" , v )
548
- }
549
- readWg .Done ()
550
- // Wait until writer transaction will commit
551
- commitWg .Wait ()
552
- row = tx .QueryRowContext (ctx , "SELECT COUNT(*) FROM test" )
553
- if err := row .Scan (& v ); err != nil {
554
- dbt .Fatal (err )
555
- }
556
- // Data written by writer transaction is already commited, it should be selectable
557
- if v != 1 {
558
- dbt .Errorf ("expected val to be 1, got %d" , v )
559
- }
560
- tx .Commit ()
561
- testDoneWg .Done ()
507
+ err = tx1 .Commit ()
508
+ if err != nil {
509
+ dbt .Fatal (err )
562
510
}
563
511
564
- go repeatableReadGoroutine ()
565
- go readCommitedGoroutine ()
566
- testDoneWg .Wait ()
512
+ row = tx2 .QueryRowContext (ctx , "SELECT COUNT(*) FROM test" )
513
+ if err := row .Scan (& v ); err != nil {
514
+ dbt .Fatal (err )
515
+ }
516
+ // Data written by writer transaction is already commited, it should be selectable
517
+ if v != 1 {
518
+ dbt .Errorf ("expected val to be 1, got %d" , v )
519
+ }
520
+ tx2 .Commit ()
567
521
})
568
522
}
0 commit comments