summaryrefslogtreecommitdiffstats
path: root/test/rollbackfault.test
blob: 4d060b43c8b8a10b2d9dd1f8594c5370443cd525 (plain)
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
# 2014-11-12
#
# 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.
#
#***********************************************************************
#
# Test that errors encountered during a ROLLBACK operation correctly 
# affect ongoing SQL statements.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl
set testprefix rollbackfault


proc int2hex {i} { format %.2X $i }
db func int2hex int2hex
do_execsql_test 1.0 {
  SELECT int2hex(0), int2hex(100), int2hex(255)
} {00 64 FF}
do_execsql_test 1.1 {
  CREATE TABLE t1(i, h);
  CREATE INDEX i1 ON t1(h);
  WITH data(a, b) AS (
    SELECT 1, int2hex(1)
      UNION ALL
    SELECT a+1, int2hex(a+1) FROM data WHERE a<40
  )
  INSERT INTO t1 SELECT * FROM data;
} {}

foreach f {oom ioerr} {
  do_faultsim_test 1.2 -faults $f* -prep {
    set sql1 { SELECT i FROM t1 WHERE (i%2)==0 }
    set sql2 { SELECT i FROM t1 WHERE (i%2)==0 ORDER BY h }
    set ::s1 [sqlite3_prepare db $sql1 -1 dummy]
    set ::s2 [sqlite3_prepare db $sql2 -1 dummy]
  
    for {set i 0} {$i < 10} {incr i} { sqlite3_step $::s1 }
    for {set i 0} {$i < 3}  {incr i} { sqlite3_step $::s2 }
  
    execsql {
      BEGIN; DELETE FROM t1 WHERE (i%2)
    }
  } -body {
    execsql { ROLLBACK }
  } -test {
  
    set res1 [list]
    set res2 [list]
    while {"SQLITE_ROW" == [sqlite3_step $::s1]} {
      lappend res1 [sqlite3_column_text $::s1 0]
    }
    while {"SQLITE_ROW" == [sqlite3_step $::s2]} {
      lappend res2 [sqlite3_column_text $::s2 0]
    }
    set rc1 [sqlite3_finalize $::s1]
    set rc2 [sqlite3_finalize $::s2]
  
    catchsql { ROLLBACK }
  
    if {$rc1=="SQLITE_OK" && $rc2=="SQLITE_OK" 
     && $res1=="22 24 26 28 30 32 34 36 38 40"
     && $res2=="8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40"
    } {
      # This is Ok.
    } elseif {$rc1!="SQLITE_OK" && $rc2!="SQLITE_OK" && $res1=="" &&$res2==""} {
      # Also Ok.
    } else {
      error "statements don't look right"
    }
  }
}


finish_test