xref: /sqlite-3.40.0/test/lock.test (revision 7c68d60b)
1# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA  02111-1307, USA.
17#
18# Author contact information:
19#   [email protected]
20#   http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library.  The
24# focus of this script is database locks.
25#
26# $Id: lock.test,v 1.2 2000/08/04 13:51:11 drh Exp $
27
28set testdir [file dirname $argv0]
29source $testdir/tester.tcl
30
31
32# Create a largish table
33#
34do_test lock-1.0 {
35  execsql {CREATE TABLE big(f1 int, f2 int, f3 int)}
36  set f [open ./testdata1.txt w]
37  for {set i 1} {$i<=500} {incr i} {
38    puts $f "$i\t[expr {$i*2}]\t[expr {$i*3}]"
39  }
40  close $f
41  execsql {COPY big FROM './testdata1.txt'}
42  file delete -force ./testdata1.txt
43} {}
44
45do_test lock-1.1 {
46  # Create a background query that gives us a read lock on the big table
47  #
48  set f [open slow.sql w]
49  puts $f "SELECT a.f1, b.f1 FROM big AS a, big AS B"
50  puts $f "WHERE a.f1+b.f1==0.5;"
51  close $f
52  set ::lock_pid [exec ./sqlite testdb <slow.sql &]
53  after 250
54  set v {}
55} {}
56
57do_test lock-1.2 {
58  # Now try to update the database
59  #
60  set v [catch {execsql {UPDATE big SET f2='xyz' WHERE f1=11}} msg]
61  lappend v $msg
62} {1 {table big is locked}}
63
64do_test lock-1.3 {
65  # Try to update the database in a separate process
66  #
67  set f [open update.sql w]
68  puts $f ".timeout 0"
69  puts $f "UPDATE big SET f2='xyz' WHERE f1=11;"
70  puts $f "SELECT f2 FROM big WHERE f1=11;"
71  close $f
72  exec ./sqlite testdb <update.sql
73} "SQL error: table big is locked\n22"
74
75do_test lock-1.4 {
76  # Try to update the database using a timeout
77  #
78  set f [open update.sql w]
79  puts $f ".timeout 1000"
80  puts $f "UPDATE big SET f2='xyz' WHERE f1=11;"
81  puts $f "SELECT f2 FROM big WHERE f1=11;"
82  close $f
83  exec ./sqlite testdb <update.sql
84} "SQL error: table big is locked\n22"
85
86do_test lock-1.5 {
87  # Try to update the database using a timeout
88  #
89  set f [open update.sql w]
90  puts $f ".timeout 10000"
91  puts $f "UPDATE big SET f2='xyz' WHERE f1=11;"
92  puts $f "SELECT f2 FROM big WHERE f1=11;"
93  close $f
94  exec ./sqlite testdb <update.sql
95} {xyz}
96
97catch {exec ps -uax | grep $::lock_pid}
98catch {exec kill -HUP $::lock_pid}
99catch {exec kill -9 $::lock_pid}
100
101finish_test
102