1762e584eSdrh# 2005 October 3 2762e584eSdrh# 3762e584eSdrh# The author disclaims copyright to this source code. In place of 4762e584eSdrh# a legal notice, here is a blessing: 5762e584eSdrh# 6762e584eSdrh# May you do good and not evil. 7762e584eSdrh# May you find forgiveness for yourself and forgive others. 8762e584eSdrh# May you share freely, never taking more than you give. 9762e584eSdrh# 10762e584eSdrh#*********************************************************************** 11762e584eSdrh# This file implements regression tests for SQLite library. 12762e584eSdrh# 13762e584eSdrh# This file implements tests the ability of the library to open 14762e584eSdrh# many different databases at the same time without leaking memory. 15762e584eSdrh# 16aebf413dSaswift# $Id: manydb.test,v 1.4 2008/11/21 00:10:35 aswift Exp $ 17762e584eSdrh 18762e584eSdrhset testdir [file dirname $argv0] 19762e584eSdrhsource $testdir/tester.tcl 20762e584eSdrh 21762e584eSdrhset N 300 22aebf413dSaswift# if we're using proxy locks, we use 5 filedescriptors for a db 23aebf413dSaswift# that is open and in the middle of writing changes, normally 24aebf413dSaswift# sqlite uses 3 (proxy locking adds the conch and the local lock) 25aebf413dSaswiftset using_proxy 0 26aebf413dSaswiftforeach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] { 27aebf413dSaswift set using_proxy value 28aebf413dSaswift} 29aebf413dSaswiftset num_fd_per_openwrite_db 3 30aebf413dSaswiftif {$using_proxy>0} { 31aebf413dSaswift set num_fd_per_openwrite_db 5 32aebf413dSaswift} 33762e584eSdrh 34aa2289f8Sdanielk1977# First test how many file descriptors are available for use. To open a 35aa2289f8Sdanielk1977# database for writing SQLite requires 3 file descriptors (the database, the 36aa2289f8Sdanielk1977# journal and the directory). 377a91dd86Sdrhset filehandles {} 38aa2289f8Sdanielk1977catch { 39aa2289f8Sdanielk1977 for {set i 0} {$i<($N * 3)} {incr i} { 40aa2289f8Sdanielk1977 lappend filehandles [open testfile.1 w] 41aa2289f8Sdanielk1977 } 42aa2289f8Sdanielk1977} 43aa2289f8Sdanielk1977foreach fd $filehandles { 44aa2289f8Sdanielk1977 close $fd 45aa2289f8Sdanielk1977} 46aa2289f8Sdanielk1977catch { 47*fda06befSmistachkin forcedelete testfile.1 48aa2289f8Sdanielk1977} 49aebf413dSaswiftset N [expr $i / $num_fd_per_openwrite_db] 50aa2289f8Sdanielk1977 51762e584eSdrh# Create a bunch of random database names 52762e584eSdrh# 53762e584eSdrhunset -nocomplain dbname 54762e584eSdrhunset -nocomplain used 55762e584eSdrhfor {set i 0} {$i<$N} {incr i} { 56762e584eSdrh while 1 { 57762e584eSdrh set name test-[format %08x [expr {int(rand()*0x7fffffff)}]].db 58762e584eSdrh if {[info exists used($name)]} continue 59762e584eSdrh set dbname($i) $name 60762e584eSdrh set used($name) $i 61762e584eSdrh break 62762e584eSdrh } 63762e584eSdrh} 64762e584eSdrh 65762e584eSdrh# Create a bunch of databases 66762e584eSdrh# 67762e584eSdrhfor {set i 0} {$i<$N} {incr i} { 68762e584eSdrh do_test manydb-1.$i { 69762e584eSdrh sqlite3 db$i $dbname($i) 70762e584eSdrh execsql { 71762e584eSdrh CREATE TABLE t1(a,b); 72762e584eSdrh BEGIN; 73762e584eSdrh INSERT INTO t1 VALUES(1,2); 74762e584eSdrh } db$i 75762e584eSdrh } {} 76762e584eSdrh} 77762e584eSdrh 78762e584eSdrh# Finish the transactions 79762e584eSdrh# 80762e584eSdrhfor {set i 0} {$i<$N} {incr i} { 81762e584eSdrh do_test manydb-2.$i { 82762e584eSdrh execsql { 83762e584eSdrh COMMIT; 84762e584eSdrh SELECT * FROM t1; 85762e584eSdrh } db$i 86762e584eSdrh } {1 2} 87762e584eSdrh} 88762e584eSdrh 89762e584eSdrh 90762e584eSdrh# Close the databases and erase the files. 91762e584eSdrh# 92762e584eSdrhfor {set i 0} {$i<$N} {incr i} { 93762e584eSdrh do_test manydb-3.$i { 94762e584eSdrh db$i close 95*fda06befSmistachkin forcedelete $dbname($i) 96762e584eSdrh } {} 97762e584eSdrh} 98762e584eSdrh 99762e584eSdrh 100762e584eSdrh 101762e584eSdrh 102762e584eSdrhfinish_test 103