xref: /onnv-gate/usr/src/lib/libsqlite/test/thread1.test (revision 4520:7dbeadedd7fe)
1*4520Snw141292
2*4520Snw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292
4*4520Snw141292# 2003 December 18
5*4520Snw141292#
6*4520Snw141292# The author disclaims copyright to this source code.  In place of
7*4520Snw141292# a legal notice, here is a blessing:
8*4520Snw141292#
9*4520Snw141292#    May you do good and not evil.
10*4520Snw141292#    May you find forgiveness for yourself and forgive others.
11*4520Snw141292#    May you share freely, never taking more than you give.
12*4520Snw141292#
13*4520Snw141292#***********************************************************************
14*4520Snw141292# This file implements regression tests for SQLite library.  The
15*4520Snw141292# focus of this script is multithreading behavior
16*4520Snw141292#
17*4520Snw141292# $Id: thread1.test,v 1.3 2004/02/11 02:18:07 drh Exp $
18*4520Snw141292
19*4520Snw141292
20*4520Snw141292set testdir [file dirname $argv0]
21*4520Snw141292source $testdir/tester.tcl
22*4520Snw141292
23*4520Snw141292# Skip this whole file if the thread testing code is not enabled
24*4520Snw141292#
25*4520Snw141292if {[llength [info command thread_step]]==0 || [sqlite -has-codec]} {
26*4520Snw141292  finish_test
27*4520Snw141292  return
28*4520Snw141292}
29*4520Snw141292
30*4520Snw141292# Create some data to work with
31*4520Snw141292#
32*4520Snw141292do_test thread1-1.1 {
33*4520Snw141292  execsql {
34*4520Snw141292    CREATE TABLE t1(a,b);
35*4520Snw141292    INSERT INTO t1 VALUES(1,'abcdefgh');
36*4520Snw141292    INSERT INTO t1 SELECT a+1, b||b FROM t1;
37*4520Snw141292    INSERT INTO t1 SELECT a+2, b||b FROM t1;
38*4520Snw141292    INSERT INTO t1 SELECT a+4, b||b FROM t1;
39*4520Snw141292    SELECT count(*), max(length(b)) FROM t1;
40*4520Snw141292  }
41*4520Snw141292} {8 64}
42*4520Snw141292
43*4520Snw141292# Interleave two threads on read access.  Then make sure a third
44*4520Snw141292# thread can write the database.  In other words:
45*4520Snw141292#
46*4520Snw141292#    read-lock A
47*4520Snw141292#    read-lock B
48*4520Snw141292#    unlock A
49*4520Snw141292#    unlock B
50*4520Snw141292#    write-lock C
51*4520Snw141292#
52*4520Snw141292# At one point, the write-lock of C would fail on Linux.
53*4520Snw141292#
54*4520Snw141292do_test thread1-1.2 {
55*4520Snw141292  thread_create A test.db
56*4520Snw141292  thread_create B test.db
57*4520Snw141292  thread_create C test.db
58*4520Snw141292  thread_compile A {SELECT a FROM t1}
59*4520Snw141292  thread_step A
60*4520Snw141292  thread_result A
61*4520Snw141292} SQLITE_ROW
62*4520Snw141292do_test thread1-1.3 {
63*4520Snw141292  thread_argc A
64*4520Snw141292} 1
65*4520Snw141292do_test thread1-1.4 {
66*4520Snw141292  thread_argv A 0
67*4520Snw141292} 1
68*4520Snw141292do_test thread1-1.5 {
69*4520Snw141292  thread_compile B {SELECT b FROM t1}
70*4520Snw141292  thread_step B
71*4520Snw141292  thread_result B
72*4520Snw141292} SQLITE_ROW
73*4520Snw141292do_test thread1-1.6 {
74*4520Snw141292  thread_argc B
75*4520Snw141292} 1
76*4520Snw141292do_test thread1-1.7 {
77*4520Snw141292  thread_argv B 0
78*4520Snw141292} abcdefgh
79*4520Snw141292do_test thread1-1.8 {
80*4520Snw141292  thread_finalize A
81*4520Snw141292  thread_result A
82*4520Snw141292} SQLITE_OK
83*4520Snw141292do_test thread1-1.9 {
84*4520Snw141292  thread_finalize B
85*4520Snw141292  thread_result B
86*4520Snw141292} SQLITE_OK
87*4520Snw141292do_test thread1-1.10 {
88*4520Snw141292  thread_compile C {CREATE TABLE t2(x,y)}
89*4520Snw141292  thread_step C
90*4520Snw141292  thread_result C
91*4520Snw141292} SQLITE_DONE
92*4520Snw141292do_test thread1-1.11 {
93*4520Snw141292  thread_finalize C
94*4520Snw141292  thread_result C
95*4520Snw141292} SQLITE_OK
96*4520Snw141292do_test thread1-1.12 {
97*4520Snw141292  catchsql {SELECT name FROM sqlite_master}
98*4520Snw141292  execsql {SELECT name FROM sqlite_master}
99*4520Snw141292} {t1 t2}
100*4520Snw141292
101*4520Snw141292
102*4520Snw141292# Under this scenario:
103*4520Snw141292#
104*4520Snw141292#    read-lock A
105*4520Snw141292#    read-lock B
106*4520Snw141292#    unlock A
107*4520Snw141292#    write-lock C
108*4520Snw141292#
109*4520Snw141292# Make sure the write-lock fails with SQLITE_BUSY
110*4520Snw141292#
111*4520Snw141292do_test thread1-2.1 {
112*4520Snw141292  thread_halt *
113*4520Snw141292  thread_create A test.db
114*4520Snw141292  thread_compile A {SELECT a FROM t1}
115*4520Snw141292  thread_step A
116*4520Snw141292  thread_result A
117*4520Snw141292} SQLITE_ROW
118*4520Snw141292do_test thread1-2.2 {
119*4520Snw141292  thread_create B test.db
120*4520Snw141292  thread_compile B {SELECT b FROM t1}
121*4520Snw141292  thread_step B
122*4520Snw141292  thread_result B
123*4520Snw141292} SQLITE_ROW
124*4520Snw141292do_test thread1-2.3 {
125*4520Snw141292  thread_create C test.db
126*4520Snw141292  thread_compile C {INSERT INTO t2 VALUES(98,99)}
127*4520Snw141292  thread_step C
128*4520Snw141292  thread_result C
129*4520Snw141292} SQLITE_BUSY
130*4520Snw141292do_test thread1-2.4 {
131*4520Snw141292  execsql {SELECT * FROM t2}
132*4520Snw141292} {}
133*4520Snw141292do_test thread1-2.5 {
134*4520Snw141292  thread_finalize A
135*4520Snw141292  thread_result A
136*4520Snw141292} SQLITE_OK
137*4520Snw141292do_test thread1-2.6 {
138*4520Snw141292  thread_step C
139*4520Snw141292  thread_result C
140*4520Snw141292} SQLITE_BUSY
141*4520Snw141292do_test thread1-2.7 {
142*4520Snw141292  execsql {SELECT * FROM t2}
143*4520Snw141292} {}
144*4520Snw141292do_test thread1-2.8 {
145*4520Snw141292  thread_finalize B
146*4520Snw141292  thread_result B
147*4520Snw141292} SQLITE_OK
148*4520Snw141292do_test thread1-2.9 {
149*4520Snw141292  thread_step C
150*4520Snw141292  thread_result C
151*4520Snw141292} SQLITE_DONE
152*4520Snw141292do_test thread1-2.10 {
153*4520Snw141292  execsql {SELECT * FROM t2}
154*4520Snw141292} {98 99}
155*4520Snw141292do_test thread1-2.11 {
156*4520Snw141292  thread_finalize C
157*4520Snw141292  thread_result C
158*4520Snw141292} SQLITE_OK
159*4520Snw141292
160*4520Snw141292thread_halt *
161*4520Snw141292finish_test
162