xref: /sqlite-3.40.0/test/server1.test (revision 7910e76d)
1# 2006 January 09
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 testing the server mode of SQLite.
13#
14# This file is derived from thread1.test
15#
16# $Id: server1.test,v 1.1 2006/01/09 23:50:11 drh Exp $
17
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Skip this whole file if the server testing code is not enabled
23#
24if {[llength [info command client_step]]==0 || [sqlite3 -has-codec]} {
25  finish_test
26  return
27}
28
29# Create some data to work with
30#
31do_test server1-1.1 {
32  execsql {
33    CREATE TABLE t1(a,b);
34    INSERT INTO t1 VALUES(1,'abcdefgh');
35    INSERT INTO t1 SELECT a+1, b||b FROM t1;
36    INSERT INTO t1 SELECT a+2, b||b FROM t1;
37    INSERT INTO t1 SELECT a+4, b||b FROM t1;
38    SELECT count(*), max(length(b)) FROM t1;
39  }
40} {8 64}
41
42# Interleave two threads on read access.  Then make sure a third
43# thread can write the database.  In other words:
44#
45#    read-lock A
46#    read-lock B
47#    unlock A
48#    unlock B
49#    write-lock C
50#
51do_test server1-1.2 {
52  client_create A test.db
53  client_create B test.db
54  client_create C test.db
55  client_compile A {SELECT a FROM t1}
56  client_step A
57  client_result A
58} SQLITE_ROW
59do_test server1-1.3 {
60  client_argc A
61} 1
62do_test server1-1.4 {
63  client_argv A 0
64} 1
65do_test server1-1.5 {
66  client_compile B {SELECT b FROM t1}
67  client_step B
68  client_result B
69} SQLITE_ROW
70do_test server1-1.6 {
71  client_argc B
72} 1
73do_test server1-1.7 {
74  client_argv B 0
75} abcdefgh
76do_test server1-1.8 {
77  client_finalize A
78  client_result A
79} SQLITE_OK
80do_test server1-1.9 {
81  client_finalize B
82  client_result B
83} SQLITE_OK
84do_test server1-1.10 {
85  client_compile C {CREATE TABLE t2(x,y)}
86  client_step C
87  client_result C
88} SQLITE_DONE
89do_test server1-1.11 {
90  client_finalize C
91  client_result C
92} SQLITE_OK
93do_test server1-1.12 {
94  catchsql {SELECT name FROM sqlite_master}
95  execsql {SELECT name FROM sqlite_master}
96} {t1 t2}
97
98
99#
100# The following tests - server1-2.* - test the following scenario:
101#
102# 1:  Read-lock thread A
103# 2:  Read-lock thread B
104# 3:  Attempt to write in thread C -> SQLITE_BUSY
105# 4:  Check db write failed from main thread.
106# 5:  Unlock from thread A.
107# 6:  Attempt to write in thread C -> SQLITE_BUSY
108# 7:  Check db write failed from main thread.
109# 8:  Unlock from thread B.
110# 9:  Attempt to write in thread C -> SQLITE_DONE
111# 10: Finalize the write from thread C
112# 11: Check db write succeeded from main thread.
113#
114do_test server1-2.1 {
115  client_halt *
116  client_create A test.db
117  client_compile A {SELECT a FROM t1}
118  client_step A
119  client_result A
120} SQLITE_ROW
121do_test server1-2.2 {
122  client_create B test.db
123  client_compile B {SELECT b FROM t1}
124  client_step B
125  client_result B
126} SQLITE_ROW
127do_test server1-2.3 {
128  client_create C test.db
129  client_compile C {INSERT INTO t2 VALUES(98,99)}
130  client_step C
131  client_result C
132  client_finalize C
133  client_result C
134} SQLITE_BUSY
135
136do_test server1-2.4 {
137  execsql {SELECT * FROM t2}
138} {}
139
140do_test server1-2.5 {
141  client_finalize A
142  client_result A
143} SQLITE_OK
144do_test server1-2.6 {
145  client_compile C {INSERT INTO t2 VALUES(98,99)}
146  client_step C
147  client_result C
148  client_finalize C
149  client_result C
150} SQLITE_BUSY
151do_test server1-2.7 {
152  execsql {SELECT * FROM t2}
153} {}
154do_test server1-2.8 {
155  client_finalize B
156  client_result B
157} SQLITE_OK
158do_test server1-2.9 {
159  client_compile C {INSERT INTO t2 VALUES(98,99)}
160  client_step C
161  client_result C
162} SQLITE_DONE
163do_test server1-2.10 {
164  client_finalize C
165  client_result C
166} SQLITE_OK
167do_test server1-2.11 {
168  execsql {SELECT * FROM t2}
169} {98 99}
170
171client_halt *
172finish_test
173