MCPcopy
hub / github.com/go-sql-driver/mysql / TestContextBeginIsolationLevel

Function TestContextBeginIsolationLevel

driver_test.go:2954–3004  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2952}
2953
2954func TestContextBeginIsolationLevel(t *testing.T) {
2955 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {
2956 dbt.mustExec("CREATE TABLE " + tbl + " (v INTEGER)")
2957 ctx, cancel := context.WithCancel(context.Background())
2958 defer cancel()
2959
2960 tx1, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
2961 Isolation: sql.LevelRepeatableRead,
2962 })
2963 if err != nil {
2964 dbt.Fatal(err)
2965 }
2966
2967 tx2, err := dbt.db.BeginTx(ctx, &sql.TxOptions{
2968 Isolation: sql.LevelReadCommitted,
2969 })
2970 if err != nil {
2971 dbt.Fatal(err)
2972 }
2973
2974 _, err = tx1.ExecContext(ctx, "INSERT INTO "+tbl+" VALUES (1)")
2975 if err != nil {
2976 dbt.Fatal(err)
2977 }
2978
2979 var v int
2980 row := tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+tbl)
2981 if err := row.Scan(&v); err != nil {
2982 dbt.Fatal(err)
2983 }
2984 // Because writer transaction wasn't committed yet, it should be available
2985 if v != 0 {
2986 dbt.Errorf("expected val to be 0, got %d", v)
2987 }
2988
2989 err = tx1.Commit()
2990 if err != nil {
2991 dbt.Fatal(err)
2992 }
2993
2994 row = tx2.QueryRowContext(ctx, "SELECT COUNT(*) FROM "+tbl)
2995 if err := row.Scan(&v); err != nil {
2996 dbt.Fatal(err)
2997 }
2998 // Data written by writer transaction is already committed, it should be selectable
2999 if v != 1 {
3000 dbt.Errorf("expected val to be 1, got %d", v)
3001 }
3002 tx2.Commit()
3003 })
3004}
3005
3006func TestContextBeginReadOnly(t *testing.T) {
3007 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {

Callers

nothing calls this directly

Calls 6

runTestsParallelFunction · 0.85
mustExecMethod · 0.80
BeginTxMethod · 0.80
ScanMethod · 0.80
CommitMethod · 0.80
ExecContextMethod · 0.45

Tested by

no test coverage detected