blob: c809217094a9eca1ffed717de72cfdcf00e903bd [file] [log] [blame]
drh7910e762006-01-09 23:50:11 +00001# 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#
drhdf12a9b2007-08-29 18:20:16 +000016# $Id: server1.test,v 1.5 2007/08/29 18:20:17 drh Exp $
drh7910e762006-01-09 23:50:11 +000017
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
drh8ab114c2018-08-28 21:12:02 +000029# This test does not work on older PPC Macs due to problems in the
30# pthreads library. So skip it.
31#
32if {$tcl_platform(machine)=="Power Macintosh" &&
33 $tcl_platform(byteOrder)=="bigEndian"} {
34 finish_test
35 return
36}
37
drh029b44b2006-01-15 00:13:15 +000038# The sample server implementation does not work right when memory
39# management is enabled.
40#
drh1b1e8a82011-08-31 19:40:58 +000041ifcapable (memorymanage||mutex_noop) {
drh029b44b2006-01-15 00:13:15 +000042 finish_test
43 return
44}
45
drh7910e762006-01-09 23:50:11 +000046# Create some data to work with
47#
48do_test server1-1.1 {
49 execsql {
50 CREATE TABLE t1(a,b);
51 INSERT INTO t1 VALUES(1,'abcdefgh');
52 INSERT INTO t1 SELECT a+1, b||b FROM t1;
53 INSERT INTO t1 SELECT a+2, b||b FROM t1;
54 INSERT INTO t1 SELECT a+4, b||b FROM t1;
55 SELECT count(*), max(length(b)) FROM t1;
56 }
57} {8 64}
58
59# Interleave two threads on read access. Then make sure a third
60# thread can write the database. In other words:
61#
62# read-lock A
63# read-lock B
64# unlock A
65# unlock B
66# write-lock C
67#
68do_test server1-1.2 {
69 client_create A test.db
70 client_create B test.db
71 client_create C test.db
72 client_compile A {SELECT a FROM t1}
73 client_step A
74 client_result A
75} SQLITE_ROW
76do_test server1-1.3 {
77 client_argc A
78} 1
79do_test server1-1.4 {
80 client_argv A 0
81} 1
82do_test server1-1.5 {
83 client_compile B {SELECT b FROM t1}
84 client_step B
85 client_result B
86} SQLITE_ROW
87do_test server1-1.6 {
88 client_argc B
89} 1
90do_test server1-1.7 {
91 client_argv B 0
92} abcdefgh
93do_test server1-1.8 {
94 client_finalize A
95 client_result A
96} SQLITE_OK
97do_test server1-1.9 {
98 client_finalize B
99 client_result B
100} SQLITE_OK
101do_test server1-1.10 {
102 client_compile C {CREATE TABLE t2(x,y)}
103 client_step C
104 client_result C
105} SQLITE_DONE
106do_test server1-1.11 {
107 client_finalize C
108 client_result C
109} SQLITE_OK
110do_test server1-1.12 {
111 catchsql {SELECT name FROM sqlite_master}
112 execsql {SELECT name FROM sqlite_master}
113} {t1 t2}
114
115
drhbdd6da22006-01-10 02:30:33 +0000116# Read from table t1. Do not finalize the statement. This
117# will leave the lock pending.
drh7910e762006-01-09 23:50:11 +0000118#
119do_test server1-2.1 {
120 client_halt *
121 client_create A test.db
122 client_compile A {SELECT a FROM t1}
123 client_step A
124 client_result A
125} SQLITE_ROW
drhbdd6da22006-01-10 02:30:33 +0000126
127# Read from the same table from another thread. This is allows.
128#
drh7910e762006-01-09 23:50:11 +0000129do_test server1-2.2 {
130 client_create B test.db
131 client_compile B {SELECT b FROM t1}
132 client_step B
133 client_result B
134} SQLITE_ROW
drhbdd6da22006-01-10 02:30:33 +0000135
136# Write to a different table from another thread. This is allowed
drhdf12a9b2007-08-29 18:20:16 +0000137# because in server mode with a shared cache we have table-level locking.
drhbdd6da22006-01-10 02:30:33 +0000138#
drh7910e762006-01-09 23:50:11 +0000139do_test server1-2.3 {
140 client_create C test.db
141 client_compile C {INSERT INTO t2 VALUES(98,99)}
142 client_step C
143 client_result C
144 client_finalize C
145 client_result C
drhbdd6da22006-01-10 02:30:33 +0000146} SQLITE_OK
drh7910e762006-01-09 23:50:11 +0000147
drhbdd6da22006-01-10 02:30:33 +0000148# But we cannot insert into table t1 because threads A and B have it locked.
149#
drh7910e762006-01-09 23:50:11 +0000150do_test server1-2.4 {
drhbdd6da22006-01-10 02:30:33 +0000151 client_compile C {INSERT INTO t1 VALUES(98,99)}
152 client_step C
153 client_result C
154 client_finalize C
155 client_result C
156} SQLITE_LOCKED
drh7910e762006-01-09 23:50:11 +0000157do_test server1-2.5 {
drh7910e762006-01-09 23:50:11 +0000158 client_finalize B
drh02d9eca2006-01-10 20:36:39 +0000159 client_wait B
drhbdd6da22006-01-10 02:30:33 +0000160 client_compile C {INSERT INTO t1 VALUES(98,99)}
drh7910e762006-01-09 23:50:11 +0000161 client_step C
162 client_result C
drhbdd6da22006-01-10 02:30:33 +0000163 client_finalize C
164 client_result C
165} SQLITE_LOCKED
166
167# Insert into t1 is successful after finishing the other two threads.
168do_test server1-2.6 {
169 client_finalize A
drh02d9eca2006-01-10 20:36:39 +0000170 client_wait A
drhbdd6da22006-01-10 02:30:33 +0000171 client_compile C {INSERT INTO t1 VALUES(98,99)}
172 client_step C
173 client_result C
drh7910e762006-01-09 23:50:11 +0000174 client_finalize C
175 client_result C
176} SQLITE_OK
drh7910e762006-01-09 23:50:11 +0000177
178client_halt *
drhdf12a9b2007-08-29 18:20:16 +0000179sqlite3_enable_shared_cache 0
drh7910e762006-01-09 23:50:11 +0000180finish_test