2007-09-07 15:29:25 +04:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
2007-09-10 11:35:47 +04:00
|
|
|
# $Id: thread001.test,v 1.4 2007/09/10 07:35:47 danielk1977 Exp $
|
2007-09-07 15:29:25 +04:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
|
2007-09-10 11:35:47 +04:00
|
|
|
source $testdir/tester.tcl
|
|
|
|
source $testdir/thread_common.tcl
|
2007-09-07 15:29:25 +04:00
|
|
|
if {[info commands sqlthread] eq ""} {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2007-09-10 10:23:53 +04:00
|
|
|
set ::NTHREAD 10
|
2007-09-07 15:29:25 +04:00
|
|
|
|
2007-09-10 11:35:47 +04:00
|
|
|
# Run this test three times:
|
|
|
|
#
|
|
|
|
# 1) All threads use the same database handle.
|
|
|
|
# 2) All threads use their own database handles.
|
|
|
|
# 3) All threads use their own database handles, shared-cache is enabled.
|
2007-09-07 15:29:25 +04:00
|
|
|
#
|
2007-09-10 11:35:47 +04:00
|
|
|
foreach {tn same_db shared_cache} [list \
|
|
|
|
1 1 0 \
|
|
|
|
2 0 0 \
|
|
|
|
3 0 1 \
|
|
|
|
] {
|
2007-09-10 10:23:53 +04:00
|
|
|
# Empty the database.
|
|
|
|
#
|
|
|
|
catchsql { DROP TABLE ab; }
|
2007-09-07 15:29:25 +04:00
|
|
|
|
2007-09-10 11:35:47 +04:00
|
|
|
do_test thread001.$tn.0 {
|
|
|
|
db close
|
|
|
|
sqlite3_enable_shared_cache $shared_cache
|
|
|
|
sqlite3_enable_shared_cache $shared_cache
|
|
|
|
} $shared_cache
|
|
|
|
sqlite3 db test.db
|
|
|
|
|
|
|
|
set dbconfig ""
|
|
|
|
if {$same_db} {
|
|
|
|
set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
|
|
|
|
}
|
|
|
|
|
2007-09-10 10:23:53 +04:00
|
|
|
# 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
|
2007-09-07 15:29:25 +04:00
|
|
|
}
|
2007-09-10 10:23:53 +04:00
|
|
|
|
|
|
|
# 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}
|
|
|
|
}
|
2007-09-07 15:29:25 +04:00
|
|
|
|
|
|
|
finish_test
|
|
|
|
|