summaryrefslogtreecommitdiffstats
path: root/test/atomic2.test
blob: 5b786586bf4462e00d2d2c5dbe7b4e1ba990fcdd (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
83
84
85
86
87
88
89
90
91
92
93
94
95
# 2018-07-15
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing that if an IO error is encountered
# as part of an atomic F2FS commit, an attempt is made to commit the
# transaction using a legacy journal commit.
#

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

db close
if {[atomic_batch_write test.db]==0} {
  puts "No f2fs atomic-batch-write support. Skipping tests..."
  finish_test
  return
}

reset_db

do_execsql_test 1.0 {
  CREATE TABLE t1(x, y);
  CREATE INDEX i1x ON t1(x);
  CREATE INDEX i2x ON t1(y);

  WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 )
  INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s;
}

set setup [list \
  -injectstart at_injectstart \
  -injectstop  at_injectstop  \
]

set ::at_fail  0
set ::at_nfail 0

proc at_injectstart {iFail} {
  set ::at_fail $iFail
  set ::at_nfail 0
}
proc at_injectstop {} {
  set ::at_fail 0
  return $::at_nfail
}

proc at_vfs_callback {method file z args} {
  if {$::at_fail>0} {
    incr ::at_fail -1
    if {$::at_fail==0} {
      incr ::at_nfail
      return SQLITE_IOERR
    } elseif {$method=="xFileControl" && $z=="COMMIT_ATOMIC_WRITE"} {
      set ::at_fail 0
    }
  }
  return SQLITE_OK
}

testvfs tvfs -default 1
tvfs script at_vfs_callback
tvfs filter {xFileControl xWrite}

faultsim_save_and_close

do_one_faultsim_test 2.0 {*}$setup -prep {
  faultsim_restore_and_reopen
} -body {
  execsql {
    WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100 )
    INSERT INTO t1 SELECT randomblob(400), randomblob(400) FROM s;
  }
} -test {
  faultsim_test_result {0 {}}

  set res [execsql {SELECT count(*) FROM t1; PRAGMA integrity_check}]
  if {$res!="200 ok"} {
    error "expected {200 ok}, got $res"
  }
}

db close
tvfs delete

finish_test