os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread001.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/thread001.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,138 @@
     1.4 +# 2007 September 7
     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 +# $Id: thread001.test,v 1.5 2008/07/12 14:52:20 drh Exp $
    1.16 +
    1.17 +set testdir [file dirname $argv0]
    1.18 +
    1.19 +source $testdir/tester.tcl
    1.20 +source $testdir/thread_common.tcl
    1.21 +if {[info commands sqlthread] eq ""} {
    1.22 +  return
    1.23 +}
    1.24 +
    1.25 +set ::NTHREAD 10
    1.26 +
    1.27 +# Run this test three times: 
    1.28 +# 
    1.29 +#    1) All threads use the same database handle.
    1.30 +#    2) All threads use their own database handles.
    1.31 +#    3) All threads use their own database handles, shared-cache is enabled.
    1.32 +#
    1.33 +foreach {tn same_db shared_cache} [list \
    1.34 +         1  1       0                   \
    1.35 +         2  0       0                   \
    1.36 +         3  0       1                   \
    1.37 +] {
    1.38 +  # Empty the database.
    1.39 +  #
    1.40 +  catchsql { DROP TABLE ab; }
    1.41 +
    1.42 +  do_test thread001.$tn.0 {
    1.43 +    db close
    1.44 +    sqlite3_enable_shared_cache $shared_cache
    1.45 +    sqlite3_enable_shared_cache $shared_cache
    1.46 +  } $shared_cache
    1.47 +  sqlite3 db test.db
    1.48 +
    1.49 +  set dbconfig ""
    1.50 +  if {$same_db} {
    1.51 +    set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
    1.52 +  }
    1.53 +
    1.54 +  # Set up a database and a schema. The database contains a single
    1.55 +  # table with two columns. The first column ("a") is an INTEGER PRIMARY 
    1.56 +  # KEY. The second contains the md5sum of all rows in the table with
    1.57 +  # a smaller value stored in column "a".
    1.58 +  #
    1.59 +  do_test thread001.$tn.1 {
    1.60 +    execsql {
    1.61 +      CREATE TABLE ab(a INTEGER PRIMARY KEY, b);
    1.62 +      CREATE INDEX ab_i ON ab(b);
    1.63 +      INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab;
    1.64 +      SELECT count(*) FROM ab;
    1.65 +    }
    1.66 +  } {1}
    1.67 +  do_test thread001.$tn.2 {
    1.68 +    execsql {
    1.69 +      SELECT 
    1.70 +        (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
    1.71 +        (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
    1.72 +    }
    1.73 +  } {1}
    1.74 +  do_test thread001.$tn.3 {
    1.75 +    execsql { PRAGMA integrity_check }
    1.76 +  } {ok}
    1.77 +  
    1.78 +  set thread_program {
    1.79 +    set needToClose 0
    1.80 +    if {![info exists ::DB]} {
    1.81 +      set ::DB [sqlthread open test.db]
    1.82 +      set needToClose 1
    1.83 +    }
    1.84 +  
    1.85 +    for {set i 0} {$i < 100} {incr i} {
    1.86 +      # Test that the invariant is true.
    1.87 +      do_test t1 {
    1.88 +        execsql {
    1.89 +          SELECT 
    1.90 +            (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
    1.91 +            (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
    1.92 +        }
    1.93 +      } {1}
    1.94 +  
    1.95 +      # Add another row to the database.
    1.96 +      execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }
    1.97 +    }
    1.98 +  
    1.99 +    if {$needToClose} {
   1.100 +      sqlite3_close $::DB
   1.101 +    }
   1.102 +  
   1.103 +    list OK
   1.104 +  }
   1.105 +  
   1.106 +  # Kick off $::NTHREAD threads:
   1.107 +  #
   1.108 +  array unset finished
   1.109 +  for {set i 0} {$i < $::NTHREAD} {incr i} {
   1.110 +    thread_spawn finished($i) $dbconfig $thread_procs $thread_program
   1.111 +  }
   1.112 +  
   1.113 +  # Wait for all threads to finish,  then check they all returned "OK".
   1.114 +  #
   1.115 +  for {set i 0} {$i < $::NTHREAD} {incr i} {
   1.116 +    if {![info exists finished($i)]} {
   1.117 +      vwait finished($i)
   1.118 +    }
   1.119 +    do_test thread001.$tn.4.$i {
   1.120 +      set ::finished($i)
   1.121 +    } OK
   1.122 +  }
   1.123 +  
   1.124 +  # Check the database still looks Ok.
   1.125 +  #
   1.126 +  do_test thread001.$tn.5 {
   1.127 +    execsql { SELECT count(*) FROM ab; }
   1.128 +  } [expr {1 + $::NTHREAD*100}]
   1.129 +  do_test thread001.$tn.6 {
   1.130 +    execsql {
   1.131 +      SELECT 
   1.132 +        (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
   1.133 +        (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
   1.134 +    }
   1.135 +  } {1}
   1.136 +  do_test thread001.$tn.7 {
   1.137 +    execsql { PRAGMA integrity_check }
   1.138 +  } {ok}
   1.139 +}
   1.140 +
   1.141 +finish_test