summaryrefslogtreecommitdiff
path: root/test/temptrigfault.test
blob: acf075d9c118a8b50421c390bdff59e235a1d765 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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