Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/swarmvtab3.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / swarmvtab3.test` 235 lines ยท 5331 bytes Raw Blame History 1 # 2017-07-15 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 file is the "swarmvtab" extension 13 # 14 15 set testdir [file dirname $argv0] 16 source $testdir/tester.tcl 17 set testprefix swarmvtab3 18 do_not_use_codec 19 20 ifcapable !vtab { 21 finish_test 22 return 23 } 24 25 load_static_extension db unionvtab 26 27 set nFile $sqlite_open_file_count 28 29 do_execsql_test 1.0 { 30 CREATE TEMP TABLE swarm(id, tbl, minval, maxval); 31 } 32 33 # Set up 100 databases with filenames "remote_test.dbN", where N is between 34 # 0 and 99. 35 do_test 1.1 { 36 for {set i 0} {$i < 100} {incr i} { 37 set file remote_test.db$i 38 forcedelete $file 39 forcedelete test.db$i 40 sqlite3 rrr $file 41 rrr eval { 42 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 43 INSERT INTO t1 VALUES($i, $i); 44 } 45 rrr close 46 db eval { 47 INSERT INTO swarm VALUES($i, 't1', $i, $i); 48 } 49 set ::dbcache(test.db$i) 0 50 } 51 } {} 52 53 proc missing_db {filename} { 54 set remote "remote_$filename" 55 forcedelete $filename 56 file copy $remote $filename 57 } 58 db func missing_db missing_db 59 60 proc openclose_db {filename bClose} { 61 if {$bClose} { 62 incr ::dbcache($filename) -1 63 } else { 64 incr ::dbcache($filename) 1 65 } 66 if {$::dbcache($filename)==0} { 67 forcedelete $filename 68 } 69 } 70 db func openclose_db openclose_db 71 72 proc check_dbcache {} { 73 set n 0 74 for {set i 0} {$i<100} {incr i} { 75 set exists [file exists test.db$i] 76 if {$exists!=($::dbcache(test.db$i)!=0)} { 77 error "inconsistent ::dbcache and disk ($i) - $exists" 78 } 79 incr n $exists 80 } 81 return $n 82 } 83 84 foreach {tn nMaxOpen cvt} { 85 1 5 { 86 CREATE VIRTUAL TABLE temp.s USING swarmvtab( 87 'SELECT :prefix || id, tbl, minval, minval FROM swarm', 88 :prefix='test.db', 89 missing=missing_db, 90 openclose=openclose_db, 91 maxopen=5 92 ) 93 } 94 95 2 3 { 96 CREATE VIRTUAL TABLE temp.s USING swarmvtab( 97 'SELECT :prefix || id, tbl, minval, minval FROM swarm', 98 :prefix='test.db', 99 missing = 'missing_db', 100 openclose=[openclose_db], 101 maxopen = 3 102 ) 103 } 104 105 3 1 { 106 CREATE VIRTUAL TABLE temp.s USING swarmvtab( 107 'SELECT :prefix||''.''||:suffix||id, tbl, minval, minval FROM swarm', 108 :prefix=test, :suffix=db, 109 missing = 'missing_db', 110 openclose=[openclose_db], 111 maxopen = 1 112 ) 113 } 114 115 } { 116 execsql { DROP TABLE IF EXISTS s } 117 118 do_execsql_test 1.$tn.1 $cvt 119 120 do_execsql_test 1.$tn.2 { 121 SELECT b FROM s WHERE a<10; 122 } {0 1 2 3 4 5 6 7 8 9} 123 124 do_test 1.$tn.3 { check_dbcache } $nMaxOpen 125 126 do_execsql_test 1.$tn.4 { 127 SELECT b FROM s WHERE (b%10)=0; 128 } {0 10 20 30 40 50 60 70 80 90} 129 130 do_test 1.$tn.5 { check_dbcache } $nMaxOpen 131 } 132 133 execsql { DROP TABLE IF EXISTS s } 134 for {set i 0} {$i < 100} {incr i} { 135 forcedelete remote_test.db$i 136 } 137 138 #---------------------------------------------------------------------------- 139 # 140 do_execsql_test 2.0 { 141 DROP TABLE IF EXISTS swarm; 142 CREATE TEMP TABLE swarm(file, tbl, minval, maxval, ctx); 143 } 144 145 catch { array unset ::dbcache } 146 147 # Set up 100 databases with filenames "remote_test.dbN", where N is a 148 # random integer between 0 and 1,000,000 149 # 0 and 99. 150 do_test 2.1 { 151 catch { array unset ctx_used } 152 for {set i 0} {$i < 100} {incr i} { 153 while 1 { 154 set ctx [expr abs(int(rand() *`1000000`))] 155 if {[info exists ctx_used($ctx)]==0} break 156 } 157 set ctx_used($ctx) 1 158 159 set file test_remote.db$ctx 160 forcedelete $file 161 forcedelete test.db$i 162 sqlite3 rrr $file 163 rrr eval { 164 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 165 INSERT INTO t1 VALUES($i, $i); 166 } 167 rrr close 168 db eval { 169 INSERT INTO swarm VALUES('test.db' || $i, 't1', $i, $i, $file) 170 } 171 set ::dbcache(test.db$i) 0 172 } 173 } {} 174 175 proc missing_db {filename ctx} { 176 file copy $ctx $filename 177 } 178 db func missing_db missing_db 179 180 proc openclose_db {filename ctx bClose} { 181 if {$bClose} { 182 incr ::dbcache($filename) -1 183 } else { 184 incr ::dbcache($filename) 1 185 } 186 if {$::dbcache($filename)==0} { 187 forcedelete $filename 188 } 189 } 190 db func openclose_db openclose_db 191 192 proc check_dbcache {} { 193 set n 0 194 foreach k [array names ::dbcache] { 195 set exists [file exists $k] 196 if {$exists!=($::dbcache($k)!=0)} { 197 error "inconsistent ::dbcache and disk ($k) - $exists" 198 } 199 incr n $exists 200 } 201 return $n 202 } 203 204 foreach {tn nMaxOpen cvt} { 205 2 5 { 206 CREATE VIRTUAL TABLE temp.s USING swarmvtab( 207 'SELECT file, tbl, minval, minval, ctx FROM swarm', 208 missing=missing_db, 209 openclose=openclose_db, 210 maxopen=5 211 ) 212 } 213 } { 214 execsql { DROP TABLE IF EXISTS s } 215 216 do_execsql_test 3.$tn.1 $cvt 217 218 do_execsql_test 3.$tn.2 { 219 SELECT b FROM s WHERE a<10; 220 } {0 1 2 3 4 5 6 7 8 9} 221 222 do_test 3.$tn.3 { check_dbcache } $nMaxOpen 223 224 do_execsql_test 3.$tn.4 { 225 SELECT b FROM s WHERE (b%10)=0; 226 } {0 10 20 30 40 50 60 70 80 90} 227 228 do_test 3.$tn.5 { check_dbcache } $nMaxOpen 229 } 230 231 db close 232 forcedelete {*}[glob test.db*] 233 forcedelete {*}[glob test_remote.db*] 234 235 finish_test