drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 1 | # 2011 July 11 |
| 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. The |
| 12 | # focus of this script is recovery from transient manditory locks |
| 13 | # that sometimes appear on database files due to anti-virus software. |
| 14 | # |
| 15 | |
| 16 | if {$tcl_platform(platform)!="windows"} return |
| 17 | |
| 18 | set testdir [file dirname $argv0] |
| 19 | source $testdir/tester.tcl |
| 20 | |
| 21 | set testprefix win32lock |
| 22 | |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 23 | db close |
| 24 | sqlite3_shutdown |
| 25 | test_sqlite3_log xLog |
| 26 | proc xLog {error_code msg} { |
mistachkin | 6b98d67 | 2014-05-30 16:42:35 +0000 | [diff] [blame] | 27 | lappend ::log $msg |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 28 | } |
| 29 | sqlite3 db test.db |
drh | 9b4c59f | 2013-04-15 17:03:42 +0000 | [diff] [blame] | 30 | db eval {PRAGMA mmap_size=0} |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 31 | |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 32 | do_test win32lock-1.1 { |
| 33 | db eval { |
| 34 | PRAGMA cache_size=10; |
| 35 | CREATE TABLE t1(x,y); |
| 36 | INSERT INTO t1 VALUES(1,randomblob(100000)); |
| 37 | INSERT INTO t1 VALUES(2,randomblob(50000)); |
| 38 | INSERT INTO t1 VALUES(3,randomblob(25000)); |
| 39 | INSERT INTO t1 VALUES(4,randomblob(12500)); |
| 40 | SELECT x, length(y) FROM t1 ORDER BY rowid; |
| 41 | } |
| 42 | } {1 100000 2 50000 3 25000 4 12500} |
| 43 | |
| 44 | unset -nocomplain delay1 rc msg |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 45 | set old_pending_byte [sqlite3_test_control_pending_byte 0x40000000] |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 46 | |
| 47 | set win32_lock_ok [list] |
| 48 | set win32_lock_error [list] |
| 49 | set delay1 25 |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 50 | while {1} { |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 51 | lock_win32_file test.db 0 $::delay1 |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 52 | set ::log {} |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 53 | set rc [catch {db eval {SELECT x, length(y) FROM t1 ORDER BY rowid}} msg] |
| 54 | if {$rc} { |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 55 | lappend win32_lock_error $::delay1 |
| 56 | do_test win32lock-1.2-$delay1-error { |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 57 | set ::msg |
| 58 | } {disk I/O error} |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 59 | } else { |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 60 | lappend win32_lock_ok $::delay1 |
| 61 | do_test win32lock-1.2-$delay1-ok { |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 62 | set ::msg |
| 63 | } {1 100000 2 50000 3 25000 4 12500} |
mistachkin | 4e6b49b | 2011-07-28 19:16:41 +0000 | [diff] [blame] | 64 | if {[info exists ::log] && $::log!=""} { |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 65 | do_test win32lock-1.2-$delay1-log1 { |
| 66 | regsub {\d+} $::log # x |
mistachkin | 56d89cb | 2015-03-26 18:24:26 +0000 | [diff] [blame] | 67 | regsub { at line \d+} $x "" x |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 68 | set x |
| 69 | } {{delayed #ms for lock/sharing conflict}} |
| 70 | } |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 71 | } |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 72 | if {[llength $win32_lock_ok] && [llength $win32_lock_error]} break |
| 73 | incr delay1 25 |
mistachkin | f2d25f2 | 2011-08-25 04:09:12 +0000 | [diff] [blame] | 74 | if {$delay1 > 12500} { |
| 75 | puts "Timed out waiting for \"ok\" and \"error\" results." |
| 76 | break |
| 77 | } |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 78 | sqlite3_sleep 10 |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 79 | } |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 80 | |
| 81 | do_test win32lock-2.0 { |
| 82 | file_control_win32_av_retry db -1 -1 |
| 83 | } {0 10 25} |
| 84 | do_test win32lock-2.1 { |
| 85 | file_control_win32_av_retry db 1 1 |
| 86 | } {0 1 1} |
| 87 | |
mistachkin | 85e444c | 2011-08-02 23:45:53 +0000 | [diff] [blame] | 88 | # |
| 89 | # NOTE: It is known that the win32lock-2.2-* tests may fail if the system is |
| 90 | # experiencing heavy load (i.e. they are very timing sensitive). This is |
| 91 | # primarily due to the AV retry delay being set to 1 millisecond in the |
| 92 | # win32lock-2.1 test (above). While it is important to test this corner |
| 93 | # case for the AV retry logic, a failure of this test should probably not |
| 94 | # be interpreted as a bug in SQLite or these test cases. |
| 95 | # |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 96 | set win32_lock_ok [list] |
| 97 | set win32_lock_error [list] |
| 98 | set delay1 1 |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 99 | while {1} { |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 100 | lock_win32_file test.db 0 $::delay1 |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 101 | set ::log {} |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 102 | set rc [catch {db eval {SELECT x, length(y) FROM t1 ORDER BY rowid}} msg] |
| 103 | if {$rc} { |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 104 | lappend win32_lock_error $::delay1 |
| 105 | do_test win32lock-2.2-$delay1-error { |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 106 | set ::msg |
| 107 | } {disk I/O error} |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 108 | } else { |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 109 | lappend win32_lock_ok $::delay1 |
| 110 | do_test win32lock-2.2-$delay1-ok { |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 111 | set ::msg |
| 112 | } {1 100000 2 50000 3 25000 4 12500} |
mistachkin | 1b466a6 | 2011-08-02 20:19:48 +0000 | [diff] [blame] | 113 | if {[info exists ::log] && $::log!=""} { |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 114 | do_test win32lock-2.2-$delay1-log1 { |
| 115 | regsub {\d+} $::log # x |
mistachkin | 56d89cb | 2015-03-26 18:24:26 +0000 | [diff] [blame] | 116 | regsub { at line \d+} $x "" x |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 117 | set x |
| 118 | } {{delayed #ms for lock/sharing conflict}} |
| 119 | } |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 120 | } |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 121 | if {[llength $win32_lock_ok] && [llength $win32_lock_error]} break |
| 122 | incr delay1 1 |
mistachkin | f2d25f2 | 2011-08-25 04:09:12 +0000 | [diff] [blame] | 123 | if {$delay1 > 500} { |
| 124 | puts "Timed out waiting for \"ok\" and \"error\" results." |
| 125 | break |
| 126 | } |
mistachkin | 176f1b4 | 2011-08-02 23:34:00 +0000 | [diff] [blame] | 127 | sqlite3_sleep 10 |
drh | d0cdf01 | 2011-07-13 16:03:46 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | file_control_win32_av_retry db 10 25 |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 131 | sqlite3_test_control_pending_byte $old_pending_byte |
mistachkin | 4d60be5 | 2011-08-26 05:40:31 +0000 | [diff] [blame] | 132 | db close |
mistachkin | 6b98d67 | 2014-05-30 16:42:35 +0000 | [diff] [blame] | 133 | forcedelete test.db |
| 134 | |
| 135 | sqlite3 db test.db |
| 136 | sqlite3 db2 test.db |
| 137 | |
| 138 | do_test win32lock-3.0 { |
| 139 | db eval { |
| 140 | CREATE TABLE t1(x); |
| 141 | INSERT INTO t1 VALUES(1); |
| 142 | INSERT INTO t1 VALUES(2); |
| 143 | INSERT INTO t1 VALUES(3); |
| 144 | } |
| 145 | } {} |
| 146 | |
| 147 | do_test win32lock-3.1 { |
| 148 | db eval { |
| 149 | BEGIN EXCLUSIVE; |
| 150 | INSERT INTO t1 VALUES(4); |
| 151 | } |
| 152 | } {} |
| 153 | |
| 154 | do_test win32lock-3.2 { |
| 155 | catchsql { |
| 156 | BEGIN EXCLUSIVE; |
| 157 | INSERT INTO t1 VALUES(5); |
| 158 | COMMIT; |
| 159 | } db2 |
| 160 | } {1 {database is locked}} |
| 161 | |
| 162 | do_test win32lock-3.3 { |
| 163 | db eval { |
| 164 | COMMIT; |
| 165 | } |
| 166 | } {} |
| 167 | |
| 168 | do_test win32lock-3.4 { |
| 169 | set handle [lindex [file_control_win32_set_handle db 0] end] |
| 170 | list [catchsql { |
| 171 | BEGIN EXCLUSIVE; |
| 172 | INSERT INTO t1 VALUES(6); |
| 173 | COMMIT; |
mistachkin | ff9fcd5 | 2014-05-30 16:54:09 +0000 | [diff] [blame] | 174 | }] [file_control_win32_set_handle db $handle] [sqlite3_extended_errcode db] |
| 175 | } {{1 {disk I/O error}} {0 0} SQLITE_IOERR_LOCK} |
mistachkin | 6b98d67 | 2014-05-30 16:42:35 +0000 | [diff] [blame] | 176 | |
| 177 | db2 close |
| 178 | db close |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 179 | sqlite3_shutdown |
mistachkin | 6b98d67 | 2014-05-30 16:42:35 +0000 | [diff] [blame] | 180 | test_sqlite3_log |
drh | a32ad84 | 2011-07-12 13:51:05 +0000 | [diff] [blame] | 181 | sqlite3_initialize |
drh | 80084ca | 2011-07-11 23:45:44 +0000 | [diff] [blame] | 182 | finish_test |