package console import ( "database/sql/driver" "errors" "testing" ) func TestPlan9Fix1CleanupErrorPreservedAlongsideCallbackFailure(t *testing.T) { s := openGatedStore(t) seedGateApp(t, s) callbackErr := errors.New("callback failed with an open transaction") err := s.withClaimSQL(t.Context(), []claimSQLScope{gateScope}, allowAllClaimSQL, func(v *Store) error { tx, err := v.db.BeginTx(t.Context(), nil) if err != nil { return err } if _, err := tx.Exec(`UPDATE audit SET action='uncommitted' WHERE account_id=7 AND app='blog'`); err != nil { return err } return callbackErr }) if !errors.Is(err, callbackErr) || !errors.Is(err, ErrClaimSQLScope) { t.Fatalf("callback or forced-cleanup failure lost: %v", err) } } func TestPlan9Fix1UnbindReportsWrittenTransactionStillAwaitingRollback(t *testing.T) { s := openGatedStore(t) seedGateApp(t, s) err := s.withClaimSQL(t.Context(), []claimSQLScope{gateScope}, allowAllClaimSQL, func(v *Store) error { return v.db.(*claimBoundDB).conn.Raw(func(dc any) error { c := dc.(*claimConn) // Model awaitDone having marked the public transaction done while its // driver rollback is still queued. closeHandles cannot join that handle. if _, err := c.BeginTx(t.Context(), driver.TxOptions{}); err != nil { return err } _, err := c.ExecContext(t.Context(), `UPDATE audit SET action='rollback-pending' WHERE account_id=7 AND app='blog'`, nil) return err }) }) if !errors.Is(err, ErrClaimSQLScope) { t.Fatalf("scope lost writes while reporting success: %v", err) } if countWhere(t, s, "audit", "action='rollback-pending'") != 0 { t.Fatal("abandoned transaction committed") } }