|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package spannerdriver |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "database/sql" |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "cloud.google.com/go/spanner/apiv1/spannerpb" |
| 24 | + "github.com/googleapis/go-sql-spanner/testutil" |
| 25 | +) |
| 26 | + |
| 27 | +func TestBeginTx(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + db, server, teardown := setupTestDBConnection(t) |
| 31 | + defer teardown() |
| 32 | + ctx := context.Background() |
| 33 | + |
| 34 | + tx, _ := db.BeginTx(ctx, &sql.TxOptions{}) |
| 35 | + _, _ = tx.ExecContext(ctx, testutil.UpdateBarSetFoo) |
| 36 | + _ = tx.Rollback() |
| 37 | + |
| 38 | + requests := drainRequestsFromServer(server.TestSpanner) |
| 39 | + beginRequests := requestsOfType(requests, reflect.TypeOf(&spannerpb.BeginTransactionRequest{})) |
| 40 | + if g, w := len(beginRequests), 1; g != w { |
| 41 | + t.Fatalf("begin requests count mismatch\n Got: %v\nWant: %v", g, w) |
| 42 | + } |
| 43 | + request := beginRequests[0].(*spannerpb.BeginTransactionRequest) |
| 44 | + if g, w := request.Options.GetIsolationLevel(), spannerpb.TransactionOptions_ISOLATION_LEVEL_UNSPECIFIED; g != w { |
| 45 | + t.Fatalf("begin isolation level mismatch\n Got: %v\nWant: %v", g, w) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestBeginTxWithIsolationLevel(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + db, server, teardown := setupTestDBConnection(t) |
| 53 | + defer teardown() |
| 54 | + ctx := context.Background() |
| 55 | + |
| 56 | + for _, level := range []sql.IsolationLevel{ |
| 57 | + sql.LevelDefault, |
| 58 | + sql.LevelSnapshot, |
| 59 | + sql.LevelRepeatableRead, |
| 60 | + sql.LevelSerializable, |
| 61 | + } { |
| 62 | + originalLevel := level |
| 63 | + for _, disableRetryAborts := range []bool{true, false} { |
| 64 | + if disableRetryAborts { |
| 65 | + level = WithDisableRetryAborts(originalLevel) |
| 66 | + } else { |
| 67 | + level = originalLevel |
| 68 | + } |
| 69 | + tx, _ := db.BeginTx(ctx, &sql.TxOptions{ |
| 70 | + Isolation: level, |
| 71 | + }) |
| 72 | + _, _ = tx.ExecContext(ctx, testutil.UpdateBarSetFoo) |
| 73 | + _ = tx.Rollback() |
| 74 | + |
| 75 | + requests := drainRequestsFromServer(server.TestSpanner) |
| 76 | + beginRequests := requestsOfType(requests, reflect.TypeOf(&spannerpb.BeginTransactionRequest{})) |
| 77 | + if g, w := len(beginRequests), 1; g != w { |
| 78 | + t.Fatalf("begin requests count mismatch\n Got: %v\nWant: %v", g, w) |
| 79 | + } |
| 80 | + request := beginRequests[0].(*spannerpb.BeginTransactionRequest) |
| 81 | + wantIsolationLevel, _ := toProtoIsolationLevel(originalLevel) |
| 82 | + if g, w := request.Options.GetIsolationLevel(), wantIsolationLevel; g != w { |
| 83 | + t.Fatalf("begin isolation level mismatch\n Got: %v\nWant: %v", g, w) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestBeginTxWithInvalidIsolationLevel(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + db, _, teardown := setupTestDBConnection(t) |
| 93 | + defer teardown() |
| 94 | + ctx := context.Background() |
| 95 | + |
| 96 | + for _, level := range []sql.IsolationLevel{ |
| 97 | + sql.LevelReadUncommitted, |
| 98 | + sql.LevelReadCommitted, |
| 99 | + sql.LevelWriteCommitted, |
| 100 | + sql.LevelLinearizable, |
| 101 | + } { |
| 102 | + originalLevel := level |
| 103 | + for _, disableRetryAborts := range []bool{true, false} { |
| 104 | + if disableRetryAborts { |
| 105 | + level = WithDisableRetryAborts(originalLevel) |
| 106 | + } else { |
| 107 | + level = originalLevel |
| 108 | + } |
| 109 | + _, err := db.BeginTx(ctx, &sql.TxOptions{ |
| 110 | + Isolation: level, |
| 111 | + }) |
| 112 | + if err == nil { |
| 113 | + t.Fatalf("BeginTx should have failed with invalid isolation level: %v", level) |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments