MCPcopy
hub / github.com/jackc/pgx / TestTxCommitSerializationFailure

Function TestTxCommitSerializationFailure

tx_test.go:151–208  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

149}
150
151func TestTxCommitSerializationFailure(t *testing.T) {
152 t.Parallel()
153
154 c1 := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
155 defer closeConn(t, c1)
156
157 if c1.PgConn().ParameterStatus("crdb_version") != "" {
158 t.Skip("Skipping due to known server issue: (https://github.com/cockroachdb/cockroach/issues/60754)")
159 }
160
161 c2 := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
162 defer closeConn(t, c2)
163
164 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
165 defer cancel()
166
167 c1.Exec(ctx, `drop table if exists tx_serializable_sums`)
168 _, err := c1.Exec(ctx, `create table tx_serializable_sums(num integer);`)
169 if err != nil {
170 t.Fatalf("Unable to create temporary table: %v", err)
171 }
172 defer c1.Exec(ctx, `drop table tx_serializable_sums`)
173
174 tx1, err := c1.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.Serializable})
175 if err != nil {
176 t.Fatalf("Begin failed: %v", err)
177 }
178 defer tx1.Rollback(ctx)
179
180 tx2, err := c2.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.Serializable})
181 if err != nil {
182 t.Fatalf("Begin failed: %v", err)
183 }
184 defer tx2.Rollback(ctx)
185
186 _, err = tx1.Exec(ctx, `insert into tx_serializable_sums(num) select sum(num)::int from tx_serializable_sums`)
187 if err != nil {
188 t.Fatalf("Exec failed: %v", err)
189 }
190
191 _, err = tx2.Exec(ctx, `insert into tx_serializable_sums(num) select sum(num)::int from tx_serializable_sums`)
192 if err != nil {
193 t.Fatalf("Exec failed: %v", err)
194 }
195
196 err = tx1.Commit(ctx)
197 if err != nil {
198 t.Fatalf("Commit failed: %v", err)
199 }
200
201 err = tx2.Commit(ctx)
202 if pgErr, ok := err.(*pgconn.PgError); !ok || pgErr.Code != "40001" {
203 t.Fatalf("Expected serialization error 40001, got %#v", err)
204 }
205
206 ensureConnValid(t, c1)
207 ensureConnValid(t, c2)
208}

Callers

nothing calls this directly

Calls 9

mustConnectStringFunction · 0.85
ParameterStatusMethod · 0.80
PgConnMethod · 0.80
closeConnFunction · 0.70
ensureConnValidFunction · 0.70
ExecMethod · 0.65
RollbackMethod · 0.65
CommitMethod · 0.65
BeginTxMethod · 0.45

Tested by

no test coverage detected