xref: /sqlite-3.40.0/test/thread1.test (revision ef5ecb41)
1# 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#
14# $Id: thread1.test,v 1.5 2004/06/10 05:59:25 danielk1977 Exp $
15
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#
22if {[llength [info command thread_step]]==0 || [sqlite -has-codec]} {
23  finish_test
24  return
25}
26
27# Create some data to work with
28#
29do_test thread1-1.1 {
30  execsql {
31    CREATE TABLE t1(a,b);
32    INSERT INTO t1 VALUES(1,'abcdefgh');
33    INSERT INTO t1 SELECT a+1, b||b FROM t1;
34    INSERT INTO t1 SELECT a+2, b||b FROM t1;
35    INSERT INTO t1 SELECT a+4, b||b FROM t1;
36    SELECT count(*), max(length(b)) FROM t1;
37  }
38} {8 64}
39
40# Interleave two threads on read access.  Then make sure a third
41# thread can write the database.  In other words:
42#
43#    read-lock A
44#    read-lock B
45#    unlock A
46#    unlock B
47#    write-lock C
48#
49# At one point, the write-lock of C would fail on Linux.
50#
51do_test thread1-1.2 {
52  thread_create A test.db
53  thread_create B test.db
54  thread_create C test.db
55  thread_compile A {SELECT a FROM t1}
56  thread_step A
57  thread_result A
58} SQLITE_ROW
59do_test thread1-1.3 {
60  thread_argc A
61} 1
62do_test thread1-1.4 {
63  thread_argv A 0
64} 1
65do_test thread1-1.5 {
66  thread_compile B {SELECT b FROM t1}
67  thread_step B
68  thread_result B
69} SQLITE_ROW
70do_test thread1-1.6 {
71  thread_argc B
72} 1
73do_test thread1-1.7 {
74  thread_argv B 0
75} abcdefgh
76do_test thread1-1.8 {
77  thread_finalize A
78  thread_result A
79} SQLITE_OK
80do_test thread1-1.9 {
81  thread_finalize B
82  thread_result B
83} SQLITE_OK
84do_test thread1-1.10 {
85  thread_compile C {CREATE TABLE t2(x,y)}
86  thread_step C
87  thread_result C
88} SQLITE_DONE
89do_test thread1-1.11 {
90  thread_finalize C
91  thread_result C
92} SQLITE_OK
93do_test thread1-1.12 {
94  catchsql {SELECT name FROM sqlite_master}
95  execsql {SELECT name FROM sqlite_master}
96} {t1 t2}
97
98
99# Under this scenario:
100#
101#    read-lock A
102#    read-lock B
103#    unlock A
104#    write-lock C
105#
106# Make sure the write-lock fails with SQLITE_BUSY
107#
108do_test thread1-2.1 {
109  thread_halt *
110  thread_create A test.db
111  thread_compile A {SELECT a FROM t1}
112  thread_step A
113  thread_result A
114} SQLITE_ROW
115do_test thread1-2.2 {
116  thread_create B test.db
117  thread_compile B {SELECT b FROM t1}
118  thread_step B
119  thread_result B
120} SQLITE_ROW
121do_test thread1-2.3 {
122  thread_create C test.db
123  thread_compile C {INSERT INTO t2 VALUES(98,99)}
124  thread_step C
125  thread_result C
126  thread_finalize C
127  thread_result C
128} SQLITE_BUSY
129
130do_test thread1-2.4 {
131  execsql {SELECT * FROM t2}
132} {}
133
134do_test thread1-2.5 {
135  thread_finalize A
136  thread_result A
137} SQLITE_OK
138do_test thread1-2.6 {
139  thread_compile C {INSERT INTO t2 VALUES(98,99)}
140  thread_step C
141  thread_result C
142  thread_finalize C
143  thread_result C
144} SQLITE_BUSY
145do_test thread1-2.7 {
146  execsql {SELECT * FROM t2}
147} {}
148do_test thread1-2.8 {
149  thread_finalize B
150  thread_result B
151} SQLITE_OK
152do_test thread1-2.9 {
153  thread_compile C {INSERT INTO t2 VALUES(98,99)}
154  thread_step C
155  thread_result C
156} SQLITE_DONE
157do_test thread1-2.10 {
158  thread_finalize C
159  thread_result C
160} SQLITE_OK
161do_test thread1-2.11 {
162  execsql {SELECT * FROM t2}
163} {98 99}
164
165thread_halt *
166finish_test
167