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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
# 2017-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 the "swarmvtab" extension
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix swarmvtab3
do_not_use_codec
ifcapable !vtab {
finish_test
return
}
load_static_extension db unionvtab
set nFile $sqlite_open_file_count
do_execsql_test 1.0 {
CREATE TEMP TABLE swarm(id, tbl, minval, maxval);
}
# Set up 100 databases with filenames "remote_test.dbN", where N is between
# 0 and 99.
do_test 1.1 {
for {set i 0} {$i < 100} {incr i} {
set file remote_test.db$i
forcedelete $file
forcedelete test.db$i
sqlite3 rrr $file
rrr eval {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
INSERT INTO t1 VALUES($i, $i);
}
rrr close
db eval {
INSERT INTO swarm VALUES($i, 't1', $i, $i);
}
set ::dbcache(test.db$i) 0
}
} {}
proc missing_db {filename} {
set remote "remote_$filename"
forcedelete $filename
file copy $remote $filename
}
db func missing_db missing_db
proc openclose_db {filename bClose} {
if {$bClose} {
incr ::dbcache($filename) -1
} else {
incr ::dbcache($filename) 1
}
if {$::dbcache($filename)==0} {
forcedelete $filename
}
}
db func openclose_db openclose_db
proc check_dbcache {} {
set n 0
for {set i 0} {$i<100} {incr i} {
set exists [file exists test.db$i]
if {$exists!=($::dbcache(test.db$i)!=0)} {
error "inconsistent ::dbcache and disk ($i) - $exists"
}
incr n $exists
}
return $n
}
foreach {tn nMaxOpen cvt} {
1 5 {
CREATE VIRTUAL TABLE temp.s USING swarmvtab(
'SELECT :prefix || id, tbl, minval, minval FROM swarm',
:prefix='test.db',
missing=missing_db,
openclose=openclose_db,
maxopen=5
)
}
2 3 {
CREATE VIRTUAL TABLE temp.s USING swarmvtab(
'SELECT :prefix || id, tbl, minval, minval FROM swarm',
:prefix='test.db',
missing = 'missing_db',
openclose=[openclose_db],
maxopen = 3
)
}
3 1 {
CREATE VIRTUAL TABLE temp.s USING swarmvtab(
'SELECT :prefix||''.''||:suffix||id, tbl, minval, minval FROM swarm',
:prefix=test, :suffix=db,
missing = 'missing_db',
openclose=[openclose_db],
maxopen = 1
)
}
} {
execsql { DROP TABLE IF EXISTS s }
do_execsql_test 1.$tn.1 $cvt
do_execsql_test 1.$tn.2 {
SELECT b FROM s WHERE a<10;
} {0 1 2 3 4 5 6 7 8 9}
do_test 1.$tn.3 { check_dbcache } $nMaxOpen
do_execsql_test 1.$tn.4 {
SELECT b FROM s WHERE (b%10)=0;
} {0 10 20 30 40 50 60 70 80 90}
do_test 1.$tn.5 { check_dbcache } $nMaxOpen
}
execsql { DROP TABLE IF EXISTS s }
for {set i 0} {$i < 100} {incr i} {
forcedelete remote_test.db$i
}
#----------------------------------------------------------------------------
#
do_execsql_test 2.0 {
DROP TABLE IF EXISTS swarm;
CREATE TEMP TABLE swarm(file, tbl, minval, maxval, ctx);
}
catch { array unset ::dbcache }
# Set up 100 databases with filenames "remote_test.dbN", where N is a
# random integer between 0 and 1,000,000
# 0 and 99.
do_test 2.1 {
catch { array unset ctx_used }
for {set i 0} {$i < 100} {incr i} {
while 1 {
set ctx [expr abs(int(rand() *1000000))]
if {[info exists ctx_used($ctx)]==0} break
}
set ctx_used($ctx) 1
set file test_remote.db$ctx
forcedelete $file
forcedelete test.db$i
sqlite3 rrr $file
rrr eval {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
INSERT INTO t1 VALUES($i, $i);
}
rrr close
db eval {
INSERT INTO swarm VALUES('test.db' || $i, 't1', $i, $i, $file)
}
set ::dbcache(test.db$i) 0
}
} {}
proc missing_db {filename ctx} {
file copy $ctx $filename
}
db func missing_db missing_db
proc openclose_db {filename ctx bClose} {
if {$bClose} {
incr ::dbcache($filename) -1
} else {
incr ::dbcache($filename) 1
}
if {$::dbcache($filename)==0} {
forcedelete $filename
}
}
db func openclose_db openclose_db
proc check_dbcache {} {
set n 0
foreach k [array names ::dbcache] {
set exists [file exists $k]
if {$exists!=($::dbcache($k)!=0)} {
error "inconsistent ::dbcache and disk ($k) - $exists"
}
incr n $exists
}
return $n
}
foreach {tn nMaxOpen cvt} {
2 5 {
CREATE VIRTUAL TABLE temp.s USING swarmvtab(
'SELECT file, tbl, minval, minval, ctx FROM swarm',
missing=missing_db,
openclose=openclose_db,
maxopen=5
)
}
} {
execsql { DROP TABLE IF EXISTS s }
do_execsql_test 3.$tn.1 $cvt
do_execsql_test 3.$tn.2 {
SELECT b FROM s WHERE a<10;
} {0 1 2 3 4 5 6 7 8 9}
do_test 3.$tn.3 { check_dbcache } $nMaxOpen
do_execsql_test 3.$tn.4 {
SELECT b FROM s WHERE (b%10)=0;
} {0 10 20 30 40 50 60 70 80 90}
do_test 3.$tn.5 { check_dbcache } $nMaxOpen
}
db close
forcedelete {*}[glob test.db*]
forcedelete {*}[glob test_remote.db*]
finish_test
|