sl@0: # 2007 September 10 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # This file contains tests that attempt to break the pcache module sl@0: # by bombarding it with simultaneous requests from multiple threads. sl@0: # sl@0: # $Id: thread003.test,v 1.4 2008/08/30 09:10:17 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: sl@0: source $testdir/tester.tcl sl@0: source $testdir/thread_common.tcl sl@0: if {[info commands sqlthread] eq ""} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Set up a couple of different databases full of pseudo-randomly sl@0: # generated data. sl@0: # sl@0: do_test thread003.1.1 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, c); sl@0: } sl@0: for {set ii 0} {$ii < 5000} {incr ii} { sl@0: execsql {INSERT INTO t1 VALUES($ii, randomblob(200), randomblob(200))} sl@0: } sl@0: execsql { sl@0: CREATE INDEX i1 ON t1(a, b); sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: do_test thread003.1.2 { sl@0: expr {([file size test.db] / 1024) > 2000} sl@0: } {1} sl@0: do_test thread003.1.3 { sl@0: db close sl@0: file delete -force test2.db sl@0: sqlite3 db test2.db sl@0: } {} sl@0: do_test thread003.1.4 { sl@0: execsql { sl@0: BEGIN; sl@0: CREATE TABLE t1(a, b, c); sl@0: } sl@0: for {set ii 0} {$ii < 5000} {incr ii} { sl@0: execsql {INSERT INTO t1 VALUES($ii, randomblob(200), randomblob(200))} sl@0: } sl@0: execsql { sl@0: CREATE INDEX i1 ON t1(a, b); sl@0: COMMIT; sl@0: } sl@0: } {} sl@0: do_test thread003.1.5 { sl@0: expr {([file size test.db] / 1024) > 2000} sl@0: } {1} sl@0: do_test thread003.1.6 { sl@0: db close sl@0: } {} sl@0: sl@0: sl@0: # This test opens a connection on each of the large (>2MB) database files sl@0: # created by the previous block. The connections do not share a cache. sl@0: # Both "cache_size" parameters are set to 15, so there is a maximum of sl@0: # 30 pages available globally. sl@0: # sl@0: # Then, in separate threads, the databases are randomly queried over and sl@0: # over again. This will force the connections to recycle clean pages from sl@0: # each other. If there is a thread-safety problem, a segfault or assertion sl@0: # failure may eventually occur. sl@0: # sl@0: set nSecond 30 sl@0: puts "Starting thread003.2 (should run for ~$nSecond seconds)" sl@0: do_test thread003.2 { sl@0: foreach zFile {test.db test2.db} { sl@0: set SCRIPT [format { sl@0: set iEnd [expr {[clock_seconds] + %d}] sl@0: set ::DB [sqlthread open %s] sl@0: sl@0: # Set the cache size to 15 pages per cache. 30 available globally. sl@0: execsql { PRAGMA cache_size = 15 } sl@0: sl@0: while {[clock_seconds] < $iEnd} { sl@0: set iQuery [expr {int(rand()*5000)}] sl@0: execsql " SELECT * FROM t1 WHERE a = $iQuery " sl@0: } sl@0: sl@0: sqlite3_close $::DB sl@0: expr 1 sl@0: } $nSecond $zFile] sl@0: sl@0: unset -nocomplain finished($zFile) sl@0: thread_spawn finished($zFile) $thread_procs $SCRIPT sl@0: } sl@0: foreach zFile {test.db test2.db} { sl@0: if {![info exists finished($zFile)]} { sl@0: vwait finished($zFile) sl@0: } sl@0: } sl@0: expr 0 sl@0: } {0} sl@0: sl@0: # This test is the same as the test above, except that each thread also sl@0: # writes to the database. This causes pages to be moved back and forth sl@0: # between the caches internal dirty and clean lists, which is another sl@0: # opportunity for a thread-related bug to present itself. sl@0: # sl@0: set nSecond 30 sl@0: puts "Starting thread003.3 (should run for ~$nSecond seconds)" sl@0: do_test thread003.3 { sl@0: foreach zFile {test.db test2.db} { sl@0: set SCRIPT [format { sl@0: set iStart [clock_seconds] sl@0: set iEnd [expr {[clock_seconds] + %d}] sl@0: set ::DB [sqlthread open %s] sl@0: sl@0: # Set the cache size to 15 pages per cache. 30 available globally. sl@0: execsql { PRAGMA cache_size = 15 } sl@0: sl@0: while {[clock_seconds] < $iEnd} { sl@0: set iQuery [expr {int(rand()*5000)}] sl@0: execsql "SELECT * FROM t1 WHERE a = $iQuery" sl@0: execsql "UPDATE t1 SET b = randomblob(200) sl@0: WHERE a < $iQuery AND a > $iQuery + 20 sl@0: " sl@0: } sl@0: sl@0: sqlite3_close $::DB sl@0: expr 1 sl@0: } $nSecond $zFile] sl@0: sl@0: unset -nocomplain finished($zFile) sl@0: thread_spawn finished($zFile) $thread_procs $SCRIPT sl@0: } sl@0: foreach zFile {test.db test2.db} { sl@0: if {![info exists finished($zFile)]} { sl@0: vwait finished($zFile) sl@0: } sl@0: } sl@0: expr 0 sl@0: } {0} sl@0: sl@0: # In this test case, one thread is continually querying the database. sl@0: # The other thread does not have a database connection, but calls sl@0: # sqlite3_release_memory() over and over again. sl@0: # sl@0: set nSecond 30 sl@0: puts "Starting thread003.3 (should run for ~$nSecond seconds)" sl@0: unset -nocomplain finished(1) sl@0: unset -nocomplain finished(2) sl@0: do_test thread003.4 { sl@0: thread_spawn finished(1) $thread_procs [format { sl@0: set iEnd [expr {[clock_seconds] + %d}] sl@0: set ::DB [sqlthread open test.db] sl@0: sl@0: # Set the cache size to 15 pages per cache. 30 available globally. sl@0: execsql { PRAGMA cache_size = 15 } sl@0: sl@0: while {[clock_seconds] < $iEnd} { sl@0: set iQuery [expr {int(rand()*5000)}] sl@0: execsql "SELECT * FROM t1 WHERE a = $iQuery" sl@0: } sl@0: sl@0: sqlite3_close $::DB sl@0: expr 1 sl@0: } $nSecond] sl@0: thread_spawn finished(2) [format { sl@0: set iEnd [expr {[clock_seconds] + %d}] sl@0: sl@0: while {[clock_seconds] < $iEnd} { sl@0: sqlite3_release_memory 1000 sl@0: } sl@0: } $nSecond] sl@0: sl@0: foreach ii {1 2} { sl@0: if {![info exists finished($ii)]} { sl@0: vwait finished($ii) sl@0: } sl@0: } sl@0: expr 0 sl@0: } {0} sl@0: sl@0: finish_test sl@0: sl@0: