# 2025 November 11 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix temptrigfault forcedelete test.db2 do_execsql_test 1.0 { CREATE TABLE t1(x, y); ATTACH 'test.db2' AS aux; CREATE TABLE aux.t1(x, y); } do_faultsim_test 1.1 -faults oom* -prep { } -body { execsql { CREATE TEMP TRIGGER tmptrig AFTER INSERT ON t1 BEGIN INSERT INTO aux.t1 VALUES(new.x, new.y); END; } } -test { faultsim_test_result {0 {}} catchsql { DROP TRIGGER tmptrig } } do_execsql_test 2.0 { CREATE TEMP TRIGGER tmptrig AFTER INSERT ON t1 BEGIN INSERT INTO aux.t1 VALUES(new.x, new.y); END; } do_faultsim_test 2 -faults oom* -prep { } -body { execsql { INSERT INTO t1 VALUES('x', 'y'); } } -test { faultsim_test_result {0 {}} } # A multi-file commit is atomic only if a super-journal is used, and # journal_mode=MEMORY (or OFF) never uses one. So when a simulated OOM # strikes part way through a commit, main and aux can disagree under those # journal modes. The checks that compare the two files are only valid # when commits are atomic. # set isAtomic [expr {[db one {PRAGMA main.journal_mode}] ni {memory off}}] do_test 3.0.1 { set r1 [db eval {SELECT * FROM t1}] set r2 [db eval {SELECT * FROM aux.t1}] db eval { DELETE FROM t1; DELETE FROM aux.t1; } expr {!$isAtomic || $r1==$r2} } 1 do_execsql_test 3.0.2 { CREATE TEMP TRIGGER tmptrig2 AFTER INSERT ON aux.t1 BEGIN INSERT INTO t1 VALUES(new.x||'2', new.y||'2'); END; } do_faultsim_test 3 -faults oom* -prep { } -body { execsql { INSERT INTO aux.t1 VALUES('aaa', 'bbb'); } } -test { faultsim_test_result {0 {}} } proc repeatlist {list n} { set ret [list] for {set i 0} {$i < $n} {incr i} { set ret [concat $ret $list] } set ret } # Each iteration of test 3 that succeeded inserted one row into aux.t1 # and, via the triggers, one row into main.t1 and a second row into aux.t1. # How many iterations succeed depends on how many simulated OOM faults land # in the benign-malloc phase of the multi-file commit, which varies from # one build to the next. So check that the changes were applied atomically # rather than for a fixed number of rows. # # Without a super-journal (see 3.0.1) the row counts may differ, so only # the row contents are checked in that case. # set nRow [db one {SELECT count(*) FROM main.t1}] do_test 3.1.0 { expr {$nRow>=2 || !$isAtomic} } 1 if {$isAtomic} { do_execsql_test 3.1.1 { SELECT * FROM main.t1; } [repeatlist {aaa2 bbb2} $nRow] do_execsql_test 3.1.2 { SELECT * FROM aux.t1; } [repeatlist {aaa bbb aaa2 bbb2} $nRow] } faultsim_save_and_close do_faultsim_test 4 -faults oom* -prep { faultsim_restore_and_reopen execsql { ATTACH 'test.db2' AS aux; } } -body { execsql { CREATE TEMP TRIGGER xyz AFTER DELETE ON main.t1 BEGIN DELETE FROM aux.t1 WHERE rowid=old.rowid; END; DELETE FROM t1 WHERE rowid=2; } } -test { faultsim_test_result {0 {}} } faultsim_save_and_close do_faultsim_test 5 -faults oom* -prep { faultsim_restore_and_reopen execsql { ATTACH 'test.db2' AS aux; } } -body { execsql { CREATE TEMP TRIGGER xyz AFTER UPDATE ON aux.t1 BEGIN UPDATE main.t1 SET x=new.x, y=new.y WHERE rowid=new.rowid; END; UPDATE aux.t1 SET x=x||x WHERE rowid=1+abs(random() % 5); } } -test { faultsim_test_result {0 {}} } finish_test