os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread002.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread002.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,109 @@
     1.4 +# 2007 September 10
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +#
    1.15 +#   This test attempts to deadlock SQLite in shared-cache mode.
    1.16 +#     
    1.17 +#
    1.18 +# $Id: thread002.test,v 1.3 2008/07/12 14:52:20 drh Exp $
    1.19 +
    1.20 +set testdir [file dirname $argv0]
    1.21 +
    1.22 +source $testdir/tester.tcl
    1.23 +source $testdir/thread_common.tcl
    1.24 +if {[info commands sqlthread] eq ""} {
    1.25 +  finish_test
    1.26 +  return
    1.27 +}
    1.28 +ifcapable !attach { 
    1.29 +  finish_test
    1.30 +  return
    1.31 +}
    1.32 +
    1.33 +db close
    1.34 +sqlite3_enable_shared_cache 1
    1.35 +
    1.36 +set ::NTHREAD 10
    1.37 +
    1.38 +do_test thread002.1 {
    1.39 +  # Create 3 databases with identical schemas:
    1.40 +  for {set ii 0} {$ii < 3} {incr ii} {
    1.41 +    file delete -force test${ii}.db
    1.42 +    sqlite3 db test${ii}.db
    1.43 +    execsql {
    1.44 +      CREATE TABLE t1(k, v);
    1.45 +      CREATE INDEX t1_i ON t1(v);
    1.46 +      INSERT INTO t1(v) VALUES(1.0);
    1.47 +    }
    1.48 +    db close
    1.49 +  }
    1.50 +} {}
    1.51 +
    1.52 +set thread_program {
    1.53 +  set ::DB [sqlite3_open test.db]
    1.54 +  for {set ii 1} {$ii <= 3} {incr ii} {
    1.55 +    set T [lindex $order [expr $ii-1]]
    1.56 +    execsql "ATTACH 'test${T}.db' AS aux${ii}"
    1.57 +  }
    1.58 +
    1.59 +  for {set ii 0} {$ii < 100} {incr ii} {
    1.60 +    execsql { SELECT * FROM aux1.t1 }
    1.61 +    execsql { INSERT INTO aux1.t1(v) SELECT sum(v) FROM aux2.t1 }
    1.62 +  
    1.63 +    execsql { SELECT * FROM aux2.t1 }
    1.64 +    execsql { INSERT INTO aux2.t1(v) SELECT sum(v) FROM aux3.t1 }
    1.65 +  
    1.66 +    execsql { SELECT * FROM aux3.t1 }
    1.67 +    execsql { INSERT INTO aux3.t1(v) SELECT sum(v) FROM aux1.t1 }
    1.68 +
    1.69 +    execsql { CREATE TABLE aux1.t2(a,b) }
    1.70 +    execsql { DROP TABLE aux1.t2 }
    1.71 +
    1.72 +    # if {($ii%10)==0} {puts -nonewline . ; flush stdout}
    1.73 +    puts -nonewline . ; flush stdout
    1.74 +  }
    1.75 +
    1.76 +  sqlite3_close $::DB
    1.77 +  list OK
    1.78 +}
    1.79 +
    1.80 +set order_list [list {0 1 2} {0 2 1} {1 0 2} {1 2 0} {2 0 1} {2 1 0}]
    1.81 +
    1.82 +array unset finished
    1.83 +for {set ii 0} {$ii < $::NTHREAD} {incr ii} {
    1.84 +  set order [lindex $order_list [expr $ii%6]]
    1.85 +  thread_spawn finished($ii) $thread_procs "set order {$order}" $thread_program
    1.86 +}
    1.87 +
    1.88 +# Wait for all threads to finish,  then check they all returned "OK".
    1.89 +#
    1.90 +for {set i 0} {$i < $::NTHREAD} {incr i} {
    1.91 +  if {![info exists finished($i)]} {
    1.92 +    vwait finished($i)
    1.93 +  }
    1.94 +  do_test thread001.2.$i {
    1.95 +    set ::finished($i)
    1.96 +  } OK
    1.97 +}
    1.98 +
    1.99 +# Check all three databases are Ok.
   1.100 +for {set ii 0} {$ii < 3} {incr ii} {
   1.101 +  do_test thread002.3.$ii {
   1.102 +    sqlite3 db test${ii}.db
   1.103 +    set res [list                         \
   1.104 +      [execsql {SELECT count(*) FROM t1}] \
   1.105 +      [execsql {PRAGMA integrity_check}]  \
   1.106 +    ]
   1.107 +    db close
   1.108 +    set res
   1.109 +  } [list [expr 1 + $::NTHREAD*100] ok]
   1.110 +}
   1.111 +
   1.112 +finish_test