os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread001.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2007 September 7
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
#
sl@0
    12
# $Id: thread001.test,v 1.5 2008/07/12 14:52:20 drh Exp $
sl@0
    13
sl@0
    14
set testdir [file dirname $argv0]
sl@0
    15
sl@0
    16
source $testdir/tester.tcl
sl@0
    17
source $testdir/thread_common.tcl
sl@0
    18
if {[info commands sqlthread] eq ""} {
sl@0
    19
  return
sl@0
    20
}
sl@0
    21
sl@0
    22
set ::NTHREAD 10
sl@0
    23
sl@0
    24
# Run this test three times: 
sl@0
    25
# 
sl@0
    26
#    1) All threads use the same database handle.
sl@0
    27
#    2) All threads use their own database handles.
sl@0
    28
#    3) All threads use their own database handles, shared-cache is enabled.
sl@0
    29
#
sl@0
    30
foreach {tn same_db shared_cache} [list \
sl@0
    31
         1  1       0                   \
sl@0
    32
         2  0       0                   \
sl@0
    33
         3  0       1                   \
sl@0
    34
] {
sl@0
    35
  # Empty the database.
sl@0
    36
  #
sl@0
    37
  catchsql { DROP TABLE ab; }
sl@0
    38
sl@0
    39
  do_test thread001.$tn.0 {
sl@0
    40
    db close
sl@0
    41
    sqlite3_enable_shared_cache $shared_cache
sl@0
    42
    sqlite3_enable_shared_cache $shared_cache
sl@0
    43
  } $shared_cache
sl@0
    44
  sqlite3 db test.db
sl@0
    45
sl@0
    46
  set dbconfig ""
sl@0
    47
  if {$same_db} {
sl@0
    48
    set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
sl@0
    49
  }
sl@0
    50
sl@0
    51
  # Set up a database and a schema. The database contains a single
sl@0
    52
  # table with two columns. The first column ("a") is an INTEGER PRIMARY 
sl@0
    53
  # KEY. The second contains the md5sum of all rows in the table with
sl@0
    54
  # a smaller value stored in column "a".
sl@0
    55
  #
sl@0
    56
  do_test thread001.$tn.1 {
sl@0
    57
    execsql {
sl@0
    58
      CREATE TABLE ab(a INTEGER PRIMARY KEY, b);
sl@0
    59
      CREATE INDEX ab_i ON ab(b);
sl@0
    60
      INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab;
sl@0
    61
      SELECT count(*) FROM ab;
sl@0
    62
    }
sl@0
    63
  } {1}
sl@0
    64
  do_test thread001.$tn.2 {
sl@0
    65
    execsql {
sl@0
    66
      SELECT 
sl@0
    67
        (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
sl@0
    68
        (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
sl@0
    69
    }
sl@0
    70
  } {1}
sl@0
    71
  do_test thread001.$tn.3 {
sl@0
    72
    execsql { PRAGMA integrity_check }
sl@0
    73
  } {ok}
sl@0
    74
  
sl@0
    75
  set thread_program {
sl@0
    76
    set needToClose 0
sl@0
    77
    if {![info exists ::DB]} {
sl@0
    78
      set ::DB [sqlthread open test.db]
sl@0
    79
      set needToClose 1
sl@0
    80
    }
sl@0
    81
  
sl@0
    82
    for {set i 0} {$i < 100} {incr i} {
sl@0
    83
      # Test that the invariant is true.
sl@0
    84
      do_test t1 {
sl@0
    85
        execsql {
sl@0
    86
          SELECT 
sl@0
    87
            (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
sl@0
    88
            (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
sl@0
    89
        }
sl@0
    90
      } {1}
sl@0
    91
  
sl@0
    92
      # Add another row to the database.
sl@0
    93
      execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }
sl@0
    94
    }
sl@0
    95
  
sl@0
    96
    if {$needToClose} {
sl@0
    97
      sqlite3_close $::DB
sl@0
    98
    }
sl@0
    99
  
sl@0
   100
    list OK
sl@0
   101
  }
sl@0
   102
  
sl@0
   103
  # Kick off $::NTHREAD threads:
sl@0
   104
  #
sl@0
   105
  array unset finished
sl@0
   106
  for {set i 0} {$i < $::NTHREAD} {incr i} {
sl@0
   107
    thread_spawn finished($i) $dbconfig $thread_procs $thread_program
sl@0
   108
  }
sl@0
   109
  
sl@0
   110
  # Wait for all threads to finish,  then check they all returned "OK".
sl@0
   111
  #
sl@0
   112
  for {set i 0} {$i < $::NTHREAD} {incr i} {
sl@0
   113
    if {![info exists finished($i)]} {
sl@0
   114
      vwait finished($i)
sl@0
   115
    }
sl@0
   116
    do_test thread001.$tn.4.$i {
sl@0
   117
      set ::finished($i)
sl@0
   118
    } OK
sl@0
   119
  }
sl@0
   120
  
sl@0
   121
  # Check the database still looks Ok.
sl@0
   122
  #
sl@0
   123
  do_test thread001.$tn.5 {
sl@0
   124
    execsql { SELECT count(*) FROM ab; }
sl@0
   125
  } [expr {1 + $::NTHREAD*100}]
sl@0
   126
  do_test thread001.$tn.6 {
sl@0
   127
    execsql {
sl@0
   128
      SELECT 
sl@0
   129
        (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
sl@0
   130
        (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
sl@0
   131
    }
sl@0
   132
  } {1}
sl@0
   133
  do_test thread001.$tn.7 {
sl@0
   134
    execsql { PRAGMA integrity_check }
sl@0
   135
  } {ok}
sl@0
   136
}
sl@0
   137
sl@0
   138
finish_test