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