1# 2014 March 25. 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# May you do good and not evil. 7# May you find forgiveness for yourself and forgive others. 8# May you share freely, never taking more than you give. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. 12# 13# Specifically, it tests the effects of fault injection on the sorter 14# module (code in vdbesort.c). 15# 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19set testprefix sortfault 20 21do_execsql_test 1.0 { 22 PRAGMA cache_size = 5; 23} 24 25foreach {tn mmap_limit nWorker tmpstore threadsmode fakeheap lookaside} { 26 1 0 0 file multithread false false 27 2 100000 0 file multithread false false 28 3 100000 1 file multithread false false 29 4 2000000 0 file singlethread false true 30} { 31 if {$sqlite_options(threadsafe)} { set threadsmode singlethread } 32 33 catch { db close } 34 sqlite3_shutdown 35 sqlite3_config_worker_threads $nWorker 36 sqlite3_config $threadsmode 37 if { $lookaside } { 38 sqlite3_config_lookaside 100 500 39 } else { 40 sqlite3_config_lookaside 0 0 41 } 42 sqlite3_initialize 43 sorter_test_fakeheap $fakeheap 44 45 set str [string repeat a 1000] 46 puts $threadsmode 47 48 do_faultsim_test 1.$tn -prep { 49 sqlite3 db test.db 50 sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $::mmap_limit 51 execsql { PRAGMA cache_size = 5 } 52 } -body { 53 execsql { 54 WITH r(x,y) AS ( 55 SELECT 1, $::str 56 UNION ALL 57 SELECT x+1, $::str FROM r 58 LIMIT 200 59 ) 60 SELECT count(x), length(y) FROM r GROUP BY (x%5) 61 } 62 } -test { 63 faultsim_test_result {0 {40 1000 40 1000 40 1000 40 1000 40 1000}} 64 } 65 66 do_faultsim_test 2.$tn -faults oom* -prep { 67 sqlite3 db test.db 68 sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $::mmap_limit 69 add_test_utf16bin_collate db 70 execsql { PRAGMA cache_size = 5 } 71 } -body { 72 execsql { 73 WITH r(x,y) AS ( 74 SELECT 100, $::str 75 UNION ALL 76 SELECT x-1, $::str FROM r 77 LIMIT 100 78 ) 79 SELECT count(x), length(y) FROM r GROUP BY y COLLATE utf16bin, (x%5) 80 } 81 } -test { 82 faultsim_test_result {0 {20 1000 20 1000 20 1000 20 1000 20 1000}} 83 } 84 85 if {$mmap_limit > 1000000} { 86 set str2 [string repeat $str 10] 87 88 sqlite3_memdebug_vfs_oom_test 0 89 sqlite3 db test.db 90 sqlite3_test_control SQLITE_TESTCTRL_SORTER_MMAP db $::mmap_limit 91 execsql { PRAGMA cache_size = 5 } 92 93 do_faultsim_test 3.$tn -faults oom-trans* -body { 94 execsql { 95 WITH r(x,y) AS ( 96 SELECT 300, $::str2 97 UNION ALL 98 SELECT x-1, $::str2 FROM r 99 LIMIT 300 100 ) 101 SELECT count(x), length(y) FROM r GROUP BY y, (x%5) 102 } 103 } -test { 104 faultsim_test_result {0 {60 10000 60 10000 60 10000 60 10000 60 10000}} 105 } 106 107 sqlite3_memdebug_vfs_oom_test 1 108 } 109} 110 111catch { db close } 112sqlite3_shutdown 113sqlite3_config_worker_threads 0 114set t(0) singlethread 115set t(1) multithread 116set t(2) serialized 117sqlite3_config $t($sqlite_options(threadsafe)) 118sqlite3_config_lookaside 100 500 119sqlite3_initialize 120 121#------------------------------------------------------------------------- 122# 123reset_db 124do_execsql_test 4.0 { 125 CREATE TABLE t1(a, b, c); 126 INSERT INTO t1 VALUES(1, 2, 3); 127} 128do_test 4.1 { 129 for {set i 0} {$i < 256} {incr i} { 130 execsql { 131 INSERT INTO t1 SELECT 132 ((a<<3) + b) & 2147483647, 133 ((b<<3) + c) & 2147483647, 134 ((c<<3) + a) & 2147483647 135 FROM t1 ORDER BY rowid DESC LIMIT 1; 136 } 137 } 138} {} 139 140faultsim_save_and_close 141 142do_faultsim_test 4.2 -faults oom* -prep { 143 faultsim_restore_and_reopen 144} -body { 145 execsql { CREATE UNIQUE INDEX i1 ON t1(a,b,c) } 146} -test { 147 faultsim_test_result {0 {}} 148} 149 150#------------------------------------------------------------------------- 151# 152reset_db 153set a [string repeat a 500] 154set b [string repeat b 500] 155set c [string repeat c 500] 156do_execsql_test 5.0 { 157 CREATE TABLE t1(a, b, c); 158 INSERT INTO t1 VALUES($a, $b, $c); 159 INSERT INTO t1 VALUES($c, $b, $a); 160} 161 162do_faultsim_test 5.1 -faults oom* -body { 163 execsql { SELECT * FROM t1 ORDER BY a } 164} -test { 165 faultsim_test_result [list 0 [list $::a $::b $::c $::c $::b $::a]] 166} 167 168finish_test 169 170