xref: /sqlite-3.40.0/test/server1.test (revision 8ab114c6)
17910e76dSdrh# 2006 January 09
27910e76dSdrh#
37910e76dSdrh# The author disclaims copyright to this source code.  In place of
47910e76dSdrh# a legal notice, here is a blessing:
57910e76dSdrh#
67910e76dSdrh#    May you do good and not evil.
77910e76dSdrh#    May you find forgiveness for yourself and forgive others.
87910e76dSdrh#    May you share freely, never taking more than you give.
97910e76dSdrh#
107910e76dSdrh#***********************************************************************
117910e76dSdrh# This file implements regression tests for SQLite library.  The
127910e76dSdrh# focus of this script is testing the server mode of SQLite.
137910e76dSdrh#
147910e76dSdrh# This file is derived from thread1.test
157910e76dSdrh#
16df12a9bcSdrh# $Id: server1.test,v 1.5 2007/08/29 18:20:17 drh Exp $
177910e76dSdrh
187910e76dSdrh
197910e76dSdrhset testdir [file dirname $argv0]
207910e76dSdrhsource $testdir/tester.tcl
217910e76dSdrh
227910e76dSdrh# Skip this whole file if the server testing code is not enabled
237910e76dSdrh#
247910e76dSdrhif {[llength [info command client_step]]==0 || [sqlite3 -has-codec]} {
257910e76dSdrh  finish_test
267910e76dSdrh  return
277910e76dSdrh}
287910e76dSdrh
29*8ab114c6Sdrh# This test does not work on older PPC Macs due to problems in the
30*8ab114c6Sdrh# pthreads library.  So skip it.
31*8ab114c6Sdrh#
32*8ab114c6Sdrhif {$tcl_platform(machine)=="Power Macintosh" &&
33*8ab114c6Sdrh    $tcl_platform(byteOrder)=="bigEndian"} {
34*8ab114c6Sdrh  finish_test
35*8ab114c6Sdrh  return
36*8ab114c6Sdrh}
37*8ab114c6Sdrh
38029b44bdSdrh# The sample server implementation does not work right when memory
39029b44bdSdrh# management is enabled.
40029b44bdSdrh#
411b1e8a8bSdrhifcapable (memorymanage||mutex_noop) {
42029b44bdSdrh  finish_test
43029b44bdSdrh  return
44029b44bdSdrh}
45029b44bdSdrh
467910e76dSdrh# Create some data to work with
477910e76dSdrh#
487910e76dSdrhdo_test server1-1.1 {
497910e76dSdrh  execsql {
507910e76dSdrh    CREATE TABLE t1(a,b);
517910e76dSdrh    INSERT INTO t1 VALUES(1,'abcdefgh');
527910e76dSdrh    INSERT INTO t1 SELECT a+1, b||b FROM t1;
537910e76dSdrh    INSERT INTO t1 SELECT a+2, b||b FROM t1;
547910e76dSdrh    INSERT INTO t1 SELECT a+4, b||b FROM t1;
557910e76dSdrh    SELECT count(*), max(length(b)) FROM t1;
567910e76dSdrh  }
577910e76dSdrh} {8 64}
587910e76dSdrh
597910e76dSdrh# Interleave two threads on read access.  Then make sure a third
607910e76dSdrh# thread can write the database.  In other words:
617910e76dSdrh#
627910e76dSdrh#    read-lock A
637910e76dSdrh#    read-lock B
647910e76dSdrh#    unlock A
657910e76dSdrh#    unlock B
667910e76dSdrh#    write-lock C
677910e76dSdrh#
687910e76dSdrhdo_test server1-1.2 {
697910e76dSdrh  client_create A test.db
707910e76dSdrh  client_create B test.db
717910e76dSdrh  client_create C test.db
727910e76dSdrh  client_compile A {SELECT a FROM t1}
737910e76dSdrh  client_step A
747910e76dSdrh  client_result A
757910e76dSdrh} SQLITE_ROW
767910e76dSdrhdo_test server1-1.3 {
777910e76dSdrh  client_argc A
787910e76dSdrh} 1
797910e76dSdrhdo_test server1-1.4 {
807910e76dSdrh  client_argv A 0
817910e76dSdrh} 1
827910e76dSdrhdo_test server1-1.5 {
837910e76dSdrh  client_compile B {SELECT b FROM t1}
847910e76dSdrh  client_step B
857910e76dSdrh  client_result B
867910e76dSdrh} SQLITE_ROW
877910e76dSdrhdo_test server1-1.6 {
887910e76dSdrh  client_argc B
897910e76dSdrh} 1
907910e76dSdrhdo_test server1-1.7 {
917910e76dSdrh  client_argv B 0
927910e76dSdrh} abcdefgh
937910e76dSdrhdo_test server1-1.8 {
947910e76dSdrh  client_finalize A
957910e76dSdrh  client_result A
967910e76dSdrh} SQLITE_OK
977910e76dSdrhdo_test server1-1.9 {
987910e76dSdrh  client_finalize B
997910e76dSdrh  client_result B
1007910e76dSdrh} SQLITE_OK
1017910e76dSdrhdo_test server1-1.10 {
1027910e76dSdrh  client_compile C {CREATE TABLE t2(x,y)}
1037910e76dSdrh  client_step C
1047910e76dSdrh  client_result C
1057910e76dSdrh} SQLITE_DONE
1067910e76dSdrhdo_test server1-1.11 {
1077910e76dSdrh  client_finalize C
1087910e76dSdrh  client_result C
1097910e76dSdrh} SQLITE_OK
1107910e76dSdrhdo_test server1-1.12 {
1117910e76dSdrh  catchsql {SELECT name FROM sqlite_master}
1127910e76dSdrh  execsql {SELECT name FROM sqlite_master}
1137910e76dSdrh} {t1 t2}
1147910e76dSdrh
1157910e76dSdrh
116bdd6da23Sdrh# Read from table t1.  Do not finalize the statement.  This
117bdd6da23Sdrh# will leave the lock pending.
1187910e76dSdrh#
1197910e76dSdrhdo_test server1-2.1 {
1207910e76dSdrh  client_halt *
1217910e76dSdrh  client_create A test.db
1227910e76dSdrh  client_compile A {SELECT a FROM t1}
1237910e76dSdrh  client_step A
1247910e76dSdrh  client_result A
1257910e76dSdrh} SQLITE_ROW
126bdd6da23Sdrh
127bdd6da23Sdrh# Read from the same table from another thread.  This is allows.
128bdd6da23Sdrh#
1297910e76dSdrhdo_test server1-2.2 {
1307910e76dSdrh  client_create B test.db
1317910e76dSdrh  client_compile B {SELECT b FROM t1}
1327910e76dSdrh  client_step B
1337910e76dSdrh  client_result B
1347910e76dSdrh} SQLITE_ROW
135bdd6da23Sdrh
136bdd6da23Sdrh# Write to a different table from another thread.  This is allowed
137df12a9bcSdrh# because in server mode with a shared cache we have table-level locking.
138bdd6da23Sdrh#
1397910e76dSdrhdo_test server1-2.3 {
1407910e76dSdrh  client_create C test.db
1417910e76dSdrh  client_compile C {INSERT INTO t2 VALUES(98,99)}
1427910e76dSdrh  client_step C
1437910e76dSdrh  client_result C
1447910e76dSdrh  client_finalize C
1457910e76dSdrh  client_result C
146bdd6da23Sdrh} SQLITE_OK
1477910e76dSdrh
148bdd6da23Sdrh# But we cannot insert into table t1 because threads A and B have it locked.
149bdd6da23Sdrh#
1507910e76dSdrhdo_test server1-2.4 {
151bdd6da23Sdrh  client_compile C {INSERT INTO t1 VALUES(98,99)}
152bdd6da23Sdrh  client_step C
153bdd6da23Sdrh  client_result C
154bdd6da23Sdrh  client_finalize C
155bdd6da23Sdrh  client_result C
156bdd6da23Sdrh} SQLITE_LOCKED
1577910e76dSdrhdo_test server1-2.5 {
1587910e76dSdrh  client_finalize B
15902d9eca5Sdrh  client_wait B
160bdd6da23Sdrh  client_compile C {INSERT INTO t1 VALUES(98,99)}
1617910e76dSdrh  client_step C
1627910e76dSdrh  client_result C
163bdd6da23Sdrh  client_finalize C
164bdd6da23Sdrh  client_result C
165bdd6da23Sdrh} SQLITE_LOCKED
166bdd6da23Sdrh
167bdd6da23Sdrh# Insert into t1 is successful after finishing the other two threads.
168bdd6da23Sdrhdo_test server1-2.6 {
169bdd6da23Sdrh  client_finalize A
17002d9eca5Sdrh  client_wait A
171bdd6da23Sdrh  client_compile C {INSERT INTO t1 VALUES(98,99)}
172bdd6da23Sdrh  client_step C
173bdd6da23Sdrh  client_result C
1747910e76dSdrh  client_finalize C
1757910e76dSdrh  client_result C
1767910e76dSdrh} SQLITE_OK
1777910e76dSdrh
1787910e76dSdrhclient_halt *
179df12a9bcSdrhsqlite3_enable_shared_cache 0
1807910e76dSdrhfinish_test
181