2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2003 December 18
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this script is multithreading behavior
2N/A#
2N/A# $Id: thread1.test,v 1.3 2004/02/11 02:18:07 drh Exp $
2N/A
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Skip this whole file if the thread testing code is not enabled
2N/A#
2N/Aif {[llength [info command thread_step]]==0 || [sqlite -has-codec]} {
2N/A finish_test
2N/A return
2N/A}
2N/A
2N/A# Create some data to work with
2N/A#
2N/Ado_test thread1-1.1 {
2N/A execsql {
2N/A CREATE TABLE t1(a,b);
2N/A INSERT INTO t1 VALUES(1,'abcdefgh');
2N/A INSERT INTO t1 SELECT a+1, b||b FROM t1;
2N/A INSERT INTO t1 SELECT a+2, b||b FROM t1;
2N/A INSERT INTO t1 SELECT a+4, b||b FROM t1;
2N/A SELECT count(*), max(length(b)) FROM t1;
2N/A }
2N/A} {8 64}
2N/A
2N/A# Interleave two threads on read access. Then make sure a third
2N/A# thread can write the database. In other words:
2N/A#
2N/A# read-lock A
2N/A# read-lock B
2N/A# unlock A
2N/A# unlock B
2N/A# write-lock C
2N/A#
2N/A# At one point, the write-lock of C would fail on Linux.
2N/A#
2N/Ado_test thread1-1.2 {
2N/A thread_create A test.db
2N/A thread_create B test.db
2N/A thread_create C test.db
2N/A thread_compile A {SELECT a FROM t1}
2N/A thread_step A
2N/A thread_result A
2N/A} SQLITE_ROW
2N/Ado_test thread1-1.3 {
2N/A thread_argc A
2N/A} 1
2N/Ado_test thread1-1.4 {
2N/A thread_argv A 0
2N/A} 1
2N/Ado_test thread1-1.5 {
2N/A thread_compile B {SELECT b FROM t1}
2N/A thread_step B
2N/A thread_result B
2N/A} SQLITE_ROW
2N/Ado_test thread1-1.6 {
2N/A thread_argc B
2N/A} 1
2N/Ado_test thread1-1.7 {
2N/A thread_argv B 0
2N/A} abcdefgh
2N/Ado_test thread1-1.8 {
2N/A thread_finalize A
2N/A thread_result A
2N/A} SQLITE_OK
2N/Ado_test thread1-1.9 {
2N/A thread_finalize B
2N/A thread_result B
2N/A} SQLITE_OK
2N/Ado_test thread1-1.10 {
2N/A thread_compile C {CREATE TABLE t2(x,y)}
2N/A thread_step C
2N/A thread_result C
2N/A} SQLITE_DONE
2N/Ado_test thread1-1.11 {
2N/A thread_finalize C
2N/A thread_result C
2N/A} SQLITE_OK
2N/Ado_test thread1-1.12 {
2N/A catchsql {SELECT name FROM sqlite_master}
2N/A execsql {SELECT name FROM sqlite_master}
2N/A} {t1 t2}
2N/A
2N/A
2N/A# Under this scenario:
2N/A#
2N/A# read-lock A
2N/A# read-lock B
2N/A# unlock A
2N/A# write-lock C
2N/A#
2N/A# Make sure the write-lock fails with SQLITE_BUSY
2N/A#
2N/Ado_test thread1-2.1 {
2N/A thread_halt *
2N/A thread_create A test.db
2N/A thread_compile A {SELECT a FROM t1}
2N/A thread_step A
2N/A thread_result A
2N/A} SQLITE_ROW
2N/Ado_test thread1-2.2 {
2N/A thread_create B test.db
2N/A thread_compile B {SELECT b FROM t1}
2N/A thread_step B
2N/A thread_result B
2N/A} SQLITE_ROW
2N/Ado_test thread1-2.3 {
2N/A thread_create C test.db
2N/A thread_compile C {INSERT INTO t2 VALUES(98,99)}
2N/A thread_step C
2N/A thread_result C
2N/A} SQLITE_BUSY
2N/Ado_test thread1-2.4 {
2N/A execsql {SELECT * FROM t2}
2N/A} {}
2N/Ado_test thread1-2.5 {
2N/A thread_finalize A
2N/A thread_result A
2N/A} SQLITE_OK
2N/Ado_test thread1-2.6 {
2N/A thread_step C
2N/A thread_result C
2N/A} SQLITE_BUSY
2N/Ado_test thread1-2.7 {
2N/A execsql {SELECT * FROM t2}
2N/A} {}
2N/Ado_test thread1-2.8 {
2N/A thread_finalize B
2N/A thread_result B
2N/A} SQLITE_OK
2N/Ado_test thread1-2.9 {
2N/A thread_step C
2N/A thread_result C
2N/A} SQLITE_DONE
2N/Ado_test thread1-2.10 {
2N/A execsql {SELECT * FROM t2}
2N/A} {98 99}
2N/Ado_test thread1-2.11 {
2N/A thread_finalize C
2N/A thread_result C
2N/A} SQLITE_OK
2N/A
2N/Athread_halt *
2N/Afinish_test