xref: /sqlite-3.40.0/test/server1.test (revision bdd6da23)
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.2 2006/01/10 02:30:33 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# Read from table t1.  Do not finalize the statement.  This
100# will leave the lock pending.
101#
102do_test server1-2.1 {
103  client_halt *
104  client_create A test.db
105  client_compile A {SELECT a FROM t1}
106  client_step A
107  client_result A
108} SQLITE_ROW
109
110# Read from the same table from another thread.  This is allows.
111#
112do_test server1-2.2 {
113  client_create B test.db
114  client_compile B {SELECT b FROM t1}
115  client_step B
116  client_result B
117} SQLITE_ROW
118
119# Write to a different table from another thread.  This is allowed
120# becaus in server mode with a shared cache we have table-level locking.
121#
122do_test server1-2.3 {
123  client_create C test.db
124  client_compile C {INSERT INTO t2 VALUES(98,99)}
125  client_step C
126  client_result C
127  client_finalize C
128  client_result C
129} SQLITE_OK
130
131# But we cannot insert into table t1 because threads A and B have it locked.
132#
133do_test server1-2.4 {
134  client_compile C {INSERT INTO t1 VALUES(98,99)}
135  client_step C
136  client_result C
137  client_finalize C
138  client_result C
139} SQLITE_LOCKED
140do_test server1-2.5 {
141  client_finalize B
142  client_compile C {INSERT INTO t1 VALUES(98,99)}
143  client_step C
144  client_result C
145  client_finalize C
146  client_result C
147} SQLITE_LOCKED
148
149# Insert into t1 is successful after finishing the other two threads.
150do_test server1-2.6 {
151  client_finalize A
152  client_compile C {INSERT INTO t1 VALUES(98,99)}
153  client_step C
154  client_result C
155  client_finalize C
156  client_result C
157} SQLITE_OK
158
159client_halt *
160finish_test
161