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.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
2009-03-26 17:48:07 +03:00
|
|
|
# $Id: thread001.test,v 1.10 2009/03/26 14:48:07 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
|
2009-03-26 17:48:07 +03:00
|
|
|
if {[run_thread_tests]==0} { finish_test ; return }
|
2007-09-07 15:29:25 +04:00
|
|
|
|
2009-02-04 14:57:46 +03:00
|
|
|
set ::enable_shared_cache [sqlite3_enable_shared_cache]
|
|
|
|
|
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
|
|
|
#
|
2009-01-19 20:40:12 +03: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
|
2011-08-30 23:52:32 +04:00
|
|
|
sqlite3 db test.db -fullmutex 1 -key xyzzy
|
2007-09-10 11:35:47 +04:00
|
|
|
|
|
|
|
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}
|
2009-01-19 20:40:12 +03:00
|
|
|
|
2007-09-10 10:23:53 +04:00
|
|
|
set thread_program {
|
2009-01-19 20:40:12 +03:00
|
|
|
#sqlthread parent {puts STARTING..}
|
2007-09-10 10:23:53 +04:00
|
|
|
set needToClose 0
|
|
|
|
if {![info exists ::DB]} {
|
2011-08-30 23:52:32 +04:00
|
|
|
set ::DB [sqlthread open test.db xyzzy]
|
2009-01-19 20:40:12 +03:00
|
|
|
#sqlthread parent "puts \"OPEN $::DB\""
|
2007-09-10 10:23:53 +04:00
|
|
|
set needToClose 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for {set i 0} {$i < 100} {incr i} {
|
|
|
|
# Test that the invariant is true.
|
|
|
|
do_test t1 {
|
|
|
|
execsql {
|
|
|
|
SELECT
|
2012-12-05 20:44:13 +04:00
|
|
|
(SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
|
2007-09-10 10:23:53 +04:00
|
|
|
(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} {
|
2009-01-19 20:40:12 +03:00
|
|
|
#sqlthread parent "puts \"CLOSE $::DB\""
|
2007-09-10 10:23:53 +04:00
|
|
|
sqlite3_close $::DB
|
|
|
|
}
|
2009-01-19 20:40:12 +03:00
|
|
|
#sqlthread parent "puts \"DONE\""
|
2007-09-10 10:23:53 +04:00
|
|
|
|
|
|
|
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
|
2012-12-05 20:44:13 +04:00
|
|
|
(SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
|
2007-09-10 10:23:53 +04:00
|
|
|
(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
|
|
|
|
2009-02-04 14:57:46 +03:00
|
|
|
sqlite3_enable_shared_cache $::enable_shared_cache
|
2018-03-16 10:48:43 +03:00
|
|
|
catch { db close }
|
2009-02-12 20:06:41 +03:00
|
|
|
set sqlite_open_file_count 0
|
2007-09-07 15:29:25 +04:00
|
|
|
finish_test
|