| 1 | package registry |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // 6b-N1: a claim transaction whose ROLLBACK (or NORMAL restore) fails |
| 10 | // discards its pooled connection. database/sql then opens a replacement, and |
| 11 | // that replacement must carry Open's pragmas: busy_timeout 5000, WAL and |
| 12 | // synchronous NORMAL, not SQLite's defaults (busy_timeout 0, synchronous |
| 13 | // FULL). |
| 14 | func TestClaimTransactionReplacementConnectionKeepsOpenPragmas(t *testing.T) { |
| 15 | r, err := Open(filepath.Join(t.TempDir(), "registry.db")) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | defer r.Close() |
| 20 | ctx := context.Background() |
| 21 | // A connection-local marker: it survives only while the pool keeps the |
| 22 | // same connection, so its loss proves the replacement happened. |
| 23 | if _, err := r.sql.Exec("PRAGMA cache_size=-1234"); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | tx, err := r.beginClaimTransaction(ctx, true) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | // End the transaction behind the wrapper so its ROLLBACK fails and the |
| 31 | // connection is discarded with driver.ErrBadConn. |
| 32 | if _, err := tx.ExecContext(ctx, "COMMIT"); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | if err := tx.Rollback(); err == nil { |
| 36 | t.Fatal("ROLLBACK after COMMIT succeeded; the discard path was not exercised") |
| 37 | } |
| 38 | var cache, busy, sync int |
| 39 | var mode string |
| 40 | if err := r.sql.QueryRow("PRAGMA cache_size").Scan(&cache); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if cache == -1234 { |
| 44 | t.Fatal("the failed claim transaction's connection was kept; expected a replacement") |
| 45 | } |
| 46 | if err := r.sql.QueryRow("PRAGMA busy_timeout").Scan(&busy); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if err := r.sql.QueryRow("PRAGMA synchronous").Scan(&sync); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err := r.sql.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if busy != 5000 || sync != 1 || mode != "wal" { |
| 56 | t.Fatalf("replacement connection pragmas: busy_timeout=%d synchronous=%d journal_mode=%s, want 5000, 1 (NORMAL), wal", busy, sync, mode) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // The first connection keeps exactly main's pragmas. |
| 61 | func TestOpenPragmasUnchangedOnFirstConnection(t *testing.T) { |
| 62 | r, err := Open(filepath.Join(t.TempDir(), "registry.db")) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | defer r.Close() |
| 67 | var busy, sync int |
| 68 | var mode string |
| 69 | if err := r.sql.QueryRow("PRAGMA busy_timeout").Scan(&busy); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if err := r.sql.QueryRow("PRAGMA synchronous").Scan(&sync); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := r.sql.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if busy != 5000 || sync != 1 || mode != "wal" { |
| 79 | t.Fatalf("first connection pragmas: busy_timeout=%d synchronous=%d journal_mode=%s", busy, sync, mode) |
| 80 | } |
| 81 | } |
| 82 | |