xref: /sqlite-3.40.0/test/sortfault.test (revision 3bd1791d)
122ace891Sdan# 2014 March 25.
222ace891Sdan#
322ace891Sdan# The author disclaims copyright to this source code.  In place of
422ace891Sdan# a legal notice, here is a blessing:
522ace891Sdan#
622ace891Sdan#    May you do good and not evil.
722ace891Sdan#    May you find forgiveness for yourself and forgive others.
822ace891Sdan#    May you share freely, never taking more than you give.
922ace891Sdan#
1022ace891Sdan#***********************************************************************
1122ace891Sdan# This file implements regression tests for SQLite library.
1222ace891Sdan#
130d51def2Sdan# Specifically, it tests the effects of fault injection on the sorter
140d51def2Sdan# module (code in vdbesort.c).
150d51def2Sdan#
1622ace891Sdan
1722ace891Sdanset testdir [file dirname $argv0]
1822ace891Sdansource $testdir/tester.tcl
1922ace891Sdanset testprefix sortfault
20*3bd1791dSdrhdb close
21*3bd1791dSdrhsqlite3_shutdown
22*3bd1791dSdrhsqlite3_config_pmasz 10
23*3bd1791dSdrhsqlite3_initialize
24*3bd1791dSdrhsqlite3 db test.db
25*3bd1791dSdrh
2622ace891Sdan
2722ace891Sdando_execsql_test 1.0 {
2822ace891Sdan  PRAGMA cache_size = 5;
2922ace891Sdan}
3022ace891Sdan
31d94d4ee7Sdanforeach {tn mmap_limit nWorker tmpstore threadsmode fakeheap lookaside} {
32d94d4ee7Sdan          1          0       0     file multithread    false     false
33d94d4ee7Sdan          2     100000       0     file multithread    false     false
34d94d4ee7Sdan          3     100000       1     file multithread    false     false
35d94d4ee7Sdan          4    2000000       0     file singlethread   false      true
360d51def2Sdan} {
37d94d4ee7Sdan  if {$sqlite_options(threadsafe)} { set threadsmode singlethread }
38d94d4ee7Sdan
39028696c4Sdrh  db eval "PRAGMA threads=$nWorker"
40e18e90ebSdan  sqlite3_config $threadsmode
41d94d4ee7Sdan  if { $lookaside } {
42d94d4ee7Sdan    sqlite3_config_lookaside 100 500
43d94d4ee7Sdan  } else {
44d94d4ee7Sdan    sqlite3_config_lookaside 0 0
45d94d4ee7Sdan  }
46e18e90ebSdan  sqlite3_initialize
47e18e90ebSdan  sorter_test_fakeheap $fakeheap
48e18e90ebSdan
49e18e90ebSdan  set str [string repeat a 1000]
50d94d4ee7Sdan  puts $threadsmode
51e18e90ebSdan
520d51def2Sdan  do_faultsim_test 1.$tn -prep {
5322ace891Sdan    sqlite3 db test.db
540d51def2Sdan    sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $::mmap_limit
55e18e90ebSdan    execsql { PRAGMA cache_size = 5 }
5622ace891Sdan  } -body {
5722ace891Sdan    execsql {
5822ace891Sdan      WITH r(x,y) AS (
59e18e90ebSdan          SELECT 1, $::str
6022ace891Sdan          UNION ALL
61e18e90ebSdan          SELECT x+1, $::str FROM r
62e18e90ebSdan          LIMIT 200
6322ace891Sdan      )
6422ace891Sdan      SELECT count(x), length(y) FROM r GROUP BY (x%5)
6522ace891Sdan    }
6622ace891Sdan  } -test {
67e18e90ebSdan    faultsim_test_result {0 {40 1000 40 1000 40 1000 40 1000 40 1000}}
6822ace891Sdan  }
69f7f425d6Sdan
70d94d4ee7Sdan  do_faultsim_test 2.$tn -faults oom* -prep {
71f7f425d6Sdan    sqlite3 db test.db
72f7f425d6Sdan    sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $::mmap_limit
73f7f425d6Sdan    add_test_utf16bin_collate db
74f7f425d6Sdan    execsql { PRAGMA cache_size = 5 }
75f7f425d6Sdan  } -body {
76f7f425d6Sdan    execsql {
77f7f425d6Sdan      WITH r(x,y) AS (
78f7f425d6Sdan          SELECT 100, $::str
79f7f425d6Sdan          UNION ALL
80f7f425d6Sdan          SELECT x-1, $::str FROM r
81f7f425d6Sdan          LIMIT 100
82f7f425d6Sdan      )
83f7f425d6Sdan      SELECT count(x), length(y) FROM r GROUP BY y COLLATE utf16bin, (x%5)
84f7f425d6Sdan    }
85f7f425d6Sdan  } -test {
86f7f425d6Sdan    faultsim_test_result {0 {20 1000 20 1000 20 1000 20 1000 20 1000}}
87f7f425d6Sdan  }
88f7f425d6Sdan
89d94d4ee7Sdan  if {$mmap_limit > 1000000} {
90d94d4ee7Sdan    set str2 [string repeat $str 10]
91d94d4ee7Sdan
92d94d4ee7Sdan    sqlite3_memdebug_vfs_oom_test 0
93d94d4ee7Sdan    sqlite3 db test.db
94d94d4ee7Sdan    sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $::mmap_limit
95d94d4ee7Sdan    execsql { PRAGMA cache_size = 5 }
96d94d4ee7Sdan
97d94d4ee7Sdan    do_faultsim_test 3.$tn -faults oom-trans* -body {
98d94d4ee7Sdan      execsql {
99d94d4ee7Sdan        WITH r(x,y) AS (
100d94d4ee7Sdan            SELECT 300, $::str2
101d94d4ee7Sdan            UNION ALL
102d94d4ee7Sdan            SELECT x-1, $::str2 FROM r
103d94d4ee7Sdan            LIMIT 300
104d94d4ee7Sdan        )
105d94d4ee7Sdan        SELECT count(x), length(y) FROM r GROUP BY y, (x%5)
106d94d4ee7Sdan      }
107d94d4ee7Sdan    } -test {
108d94d4ee7Sdan      faultsim_test_result {0 {60 10000 60 10000 60 10000 60 10000 60 10000}}
109d94d4ee7Sdan    }
110d94d4ee7Sdan
111d94d4ee7Sdan    sqlite3_memdebug_vfs_oom_test 1
112d94d4ee7Sdan  }
1130d51def2Sdan}
11422ace891Sdan
115e18e90ebSdancatch { db close }
116e18e90ebSdansqlite3_shutdown
117e18e90ebSdanset t(0) singlethread
118e18e90ebSdanset t(1) multithread
119e18e90ebSdanset t(2) serialized
120e18e90ebSdansqlite3_config $t($sqlite_options(threadsafe))
121d94d4ee7Sdansqlite3_config_lookaside 100 500
122e18e90ebSdansqlite3_initialize
123d94d4ee7Sdan
124d94d4ee7Sdan#-------------------------------------------------------------------------
125d94d4ee7Sdan#
126d94d4ee7Sdanreset_db
127d94d4ee7Sdando_execsql_test 4.0 {
128d94d4ee7Sdan  CREATE TABLE t1(a, b, c);
129d94d4ee7Sdan  INSERT INTO t1 VALUES(1, 2, 3);
130d94d4ee7Sdan}
131d94d4ee7Sdando_test 4.1 {
132d94d4ee7Sdan  for {set i 0} {$i < 256} {incr i} {
133d94d4ee7Sdan    execsql {
134d94d4ee7Sdan      INSERT INTO t1 SELECT
135d94d4ee7Sdan        ((a<<3) + b) & 2147483647,
136d94d4ee7Sdan        ((b<<3) + c) & 2147483647,
137d94d4ee7Sdan        ((c<<3) + a) & 2147483647
138d94d4ee7Sdan      FROM t1 ORDER BY rowid DESC LIMIT 1;
139d94d4ee7Sdan    }
140d94d4ee7Sdan  }
141d94d4ee7Sdan} {}
142d94d4ee7Sdan
143d94d4ee7Sdanfaultsim_save_and_close
144d94d4ee7Sdan
145d94d4ee7Sdando_faultsim_test 4.2 -faults oom* -prep {
146d94d4ee7Sdan  faultsim_restore_and_reopen
147d94d4ee7Sdan} -body {
148d94d4ee7Sdan  execsql { CREATE UNIQUE INDEX i1 ON t1(a,b,c) }
149d94d4ee7Sdan} -test {
150d94d4ee7Sdan  faultsim_test_result {0 {}}
151d94d4ee7Sdan}
152d94d4ee7Sdan
153d94d4ee7Sdan#-------------------------------------------------------------------------
154d94d4ee7Sdan#
155d94d4ee7Sdanreset_db
156d94d4ee7Sdanset a [string repeat a 500]
157d94d4ee7Sdanset b [string repeat b 500]
158d94d4ee7Sdanset c [string repeat c 500]
159d94d4ee7Sdando_execsql_test 5.0 {
160d94d4ee7Sdan  CREATE TABLE t1(a, b, c);
161d94d4ee7Sdan  INSERT INTO t1 VALUES($a, $b, $c);
162d94d4ee7Sdan  INSERT INTO t1 VALUES($c, $b, $a);
163d94d4ee7Sdan}
164d94d4ee7Sdan
165d94d4ee7Sdando_faultsim_test 5.1 -faults oom* -body {
166d94d4ee7Sdan  execsql { SELECT * FROM t1 ORDER BY a }
167d94d4ee7Sdan} -test {
168d94d4ee7Sdan  faultsim_test_result [list 0 [list $::a $::b $::c $::c $::b $::a]]
169d94d4ee7Sdan}
170d94d4ee7Sdan
17122ace891Sdanfinish_test
172