sqlite/test/thread001.test
danielk1977 d9b5b1177d Upgrade thread001.test to test with multiple database handles. (CVS 4417)
FossilOrigin-Name: 6ee2b8ffc4310c8e329f634f3ade058b33c53a2a
2007-09-10 06:23:53 +00:00

164 lines
4.3 KiB
Plaintext

# 2007 September 7
#
# 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.
#
#***********************************************************************
#
# $Id: thread001.test,v 1.3 2007/09/10 06:23:54 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
if {[info commands sqlthread] eq ""} {
puts -nonewline "Skipping thread-safety tests - "
puts " not running a threadsafe sqlite/tcl build"
puts -nonewline "Both SQLITE_THREADSAFE and TCL_THREADS must be defined when"
puts " building testfixture"
finish_test
return
}
set ::NTHREAD 10
# The following script is sourced by every thread spawned using
# [sqlthread spawn]:
set thread_procs {
# Execute the supplied SQL using database handle $::DB.
#
proc execsql {sql} {
set res [list]
set ::STMT [sqlite3_prepare $::DB $sql -1 dummy_tail]
while {[sqlite3_step $::STMT] eq "SQLITE_ROW"} {
for {set i 0} {$i < [sqlite3_column_count $::STMT]} {incr i} {
lappend res [sqlite3_column_text $::STMT 0]
}
}
set rc [sqlite3_finalize $::STMT]
if {$rc ne "SQLITE_OK"} {
error [sqlite3_errmsg $::DB]
}
set res
}
proc do_test {name script result} {
set res [eval $script]
if {$res ne $result} {
error "$name failed: expected \"$result\" got \"$res\""
}
}
}
proc thread_spawn {varname args} {
sqlthread spawn $varname [join $args ;]
}
#########################################################################
# End of infrastruture. Start of test cases.
#########################################################################
# Run this test twice: Once with all threads using the same database
# connection, and once with each using it's own connection.
#
foreach {dbconfig tn} [list "set ::DB $::DB" 1 "" 2] {
# Empty the database.
#
catchsql { DROP TABLE ab; }
# Set up a database and a schema. The database contains a single
# table with two columns. The first column ("a") is an INTEGER PRIMARY
# KEY. The second contains the md5sum of all rows in the table with
# a smaller value stored in column "a".
#
do_test thread001.$tn.1 {
execsql {
CREATE TABLE ab(a INTEGER PRIMARY KEY, b);
CREATE INDEX ab_i ON ab(b);
INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab;
SELECT count(*) FROM ab;
}
} {1}
do_test thread001.$tn.2 {
execsql {
SELECT
(SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
(SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
}
} {1}
do_test thread001.$tn.3 {
execsql { PRAGMA integrity_check }
} {ok}
set thread_program {
set needToClose 0
if {![info exists ::DB]} {
set ::DB [sqlthread open test.db]
set needToClose 1
}
for {set i 0} {$i < 100} {incr i} {
# Test that the invariant is true.
do_test t1 {
execsql {
SELECT
(SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
(SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
}
} {1}
# Add another row to the database.
execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }
}
if {$needToClose} {
sqlite3_close $::DB
}
list OK
}
# Kick off $::NTHREAD threads:
#
array unset finished
for {set i 0} {$i < $::NTHREAD} {incr i} {
thread_spawn finished($i) $dbconfig $thread_procs $thread_program
}
# Wait for all threads to finish, then check they all returned "OK".
#
for {set i 0} {$i < $::NTHREAD} {incr i} {
if {![info exists finished($i)]} {
vwait finished($i)
}
do_test thread001.$tn.4.$i {
set ::finished($i)
} OK
}
# Check the database still looks Ok.
#
do_test thread001.$tn.5 {
execsql { SELECT count(*) FROM ab; }
} [expr {1 + $::NTHREAD*100}]
do_test thread001.$tn.6 {
execsql {
SELECT
(SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
(SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
}
} {1}
do_test thread001.$tn.7 {
execsql { PRAGMA integrity_check }
} {ok}
}
finish_test