blob: 70bcc22c224b7dc3b3d18bd58f4ad5085ed35f2b [file] [log] [blame]
drha6064dc2003-12-19 02:52:05 +00001# 2003 December 18
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# This file implements regression tests for SQLite library. The
12# focus of this script is multithreading behavior
13#
drh18472fa2008-10-07 15:25:48 +000014# $Id: thread1.test,v 1.8 2008/10/07 15:25:49 drh Exp $
drha6064dc2003-12-19 02:52:05 +000015
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Skip this whole file if the thread testing code is not enabled
21#
drh18472fa2008-10-07 15:25:48 +000022ifcapable !mutex {
23 finish_test
24 return
25}
drhef4ac8f2004-06-19 00:16:31 +000026if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
drha6064dc2003-12-19 02:52:05 +000027 finish_test
28 return
29}
30
31# Create some data to work with
32#
33do_test thread1-1.1 {
34 execsql {
35 CREATE TABLE t1(a,b);
36 INSERT INTO t1 VALUES(1,'abcdefgh');
37 INSERT INTO t1 SELECT a+1, b||b FROM t1;
38 INSERT INTO t1 SELECT a+2, b||b FROM t1;
39 INSERT INTO t1 SELECT a+4, b||b FROM t1;
40 SELECT count(*), max(length(b)) FROM t1;
41 }
42} {8 64}
43
44# Interleave two threads on read access. Then make sure a third
drhacf01e72003-12-19 08:40:22 +000045# thread can write the database. In other words:
46#
47# read-lock A
48# read-lock B
49# unlock A
50# unlock B
51# write-lock C
52#
53# At one point, the write-lock of C would fail on Linux.
drha6064dc2003-12-19 02:52:05 +000054#
55do_test thread1-1.2 {
56 thread_create A test.db
57 thread_create B test.db
58 thread_create C test.db
59 thread_compile A {SELECT a FROM t1}
60 thread_step A
61 thread_result A
62} SQLITE_ROW
63do_test thread1-1.3 {
64 thread_argc A
65} 1
66do_test thread1-1.4 {
67 thread_argv A 0
68} 1
69do_test thread1-1.5 {
70 thread_compile B {SELECT b FROM t1}
71 thread_step B
72 thread_result B
73} SQLITE_ROW
74do_test thread1-1.6 {
75 thread_argc B
76} 1
77do_test thread1-1.7 {
78 thread_argv B 0
79} abcdefgh
80do_test thread1-1.8 {
81 thread_finalize A
82 thread_result A
83} SQLITE_OK
84do_test thread1-1.9 {
85 thread_finalize B
86 thread_result B
87} SQLITE_OK
88do_test thread1-1.10 {
89 thread_compile C {CREATE TABLE t2(x,y)}
90 thread_step C
91 thread_result C
92} SQLITE_DONE
93do_test thread1-1.11 {
94 thread_finalize C
95 thread_result C
96} SQLITE_OK
drhacf01e72003-12-19 08:40:22 +000097do_test thread1-1.12 {
98 catchsql {SELECT name FROM sqlite_master}
99 execsql {SELECT name FROM sqlite_master}
100} {t1 t2}
drha6064dc2003-12-19 02:52:05 +0000101
102
drhacf01e72003-12-19 08:40:22 +0000103#
danielk1977f9d19a62004-06-14 08:26:35 +0000104# The following tests - thread1-2.* - test the following scenario:
drhacf01e72003-12-19 08:40:22 +0000105#
danielk1977f9d19a62004-06-14 08:26:35 +0000106# 1: Read-lock thread A
107# 2: Read-lock thread B
108# 3: Attempt to write in thread C -> SQLITE_BUSY
109# 4: Check db write failed from main thread.
110# 5: Unlock from thread A.
111# 6: Attempt to write in thread C -> SQLITE_BUSY
112# 7: Check db write failed from main thread.
113# 8: Unlock from thread B.
114# 9: Attempt to write in thread C -> SQLITE_DONE
115# 10: Finalize the write from thread C
116# 11: Check db write succeeded from main thread.
drhacf01e72003-12-19 08:40:22 +0000117#
118do_test thread1-2.1 {
119 thread_halt *
120 thread_create A test.db
121 thread_compile A {SELECT a FROM t1}
122 thread_step A
123 thread_result A
124} SQLITE_ROW
125do_test thread1-2.2 {
126 thread_create B test.db
127 thread_compile B {SELECT b FROM t1}
128 thread_step B
129 thread_result B
130} SQLITE_ROW
131do_test thread1-2.3 {
132 thread_create C test.db
133 thread_compile C {INSERT INTO t2 VALUES(98,99)}
134 thread_step C
135 thread_result C
danielk19770de0bb32004-06-10 05:59:24 +0000136 thread_finalize C
137 thread_result C
drhacf01e72003-12-19 08:40:22 +0000138} SQLITE_BUSY
danielk19770de0bb32004-06-10 05:59:24 +0000139
drhacf01e72003-12-19 08:40:22 +0000140do_test thread1-2.4 {
141 execsql {SELECT * FROM t2}
142} {}
danielk19770de0bb32004-06-10 05:59:24 +0000143
drhacf01e72003-12-19 08:40:22 +0000144do_test thread1-2.5 {
145 thread_finalize A
146 thread_result A
147} SQLITE_OK
148do_test thread1-2.6 {
danielk19770de0bb32004-06-10 05:59:24 +0000149 thread_compile C {INSERT INTO t2 VALUES(98,99)}
drhacf01e72003-12-19 08:40:22 +0000150 thread_step C
151 thread_result C
danielk19770de0bb32004-06-10 05:59:24 +0000152 thread_finalize C
153 thread_result C
drhacf01e72003-12-19 08:40:22 +0000154} SQLITE_BUSY
155do_test thread1-2.7 {
156 execsql {SELECT * FROM t2}
157} {}
158do_test thread1-2.8 {
159 thread_finalize B
160 thread_result B
161} SQLITE_OK
162do_test thread1-2.9 {
danielk19770de0bb32004-06-10 05:59:24 +0000163 thread_compile C {INSERT INTO t2 VALUES(98,99)}
drhacf01e72003-12-19 08:40:22 +0000164 thread_step C
165 thread_result C
166} SQLITE_DONE
167do_test thread1-2.10 {
drhacf01e72003-12-19 08:40:22 +0000168 thread_finalize C
169 thread_result C
170} SQLITE_OK
danielk1977ecb2a962004-06-02 06:30:16 +0000171do_test thread1-2.11 {
172 execsql {SELECT * FROM t2}
173} {98 99}
drhacf01e72003-12-19 08:40:22 +0000174
drha6064dc2003-12-19 02:52:05 +0000175thread_halt *
176finish_test