xref: /sqlite-3.40.0/test/lock6.test (revision 14d14607)
1aebf413dSaswift# 2008 October 6
2aebf413dSaswift#
3aebf413dSaswift# The author disclaims copyright to this source code.  In place of
4aebf413dSaswift# a legal notice, here is a blessing:
5aebf413dSaswift#
6aebf413dSaswift#    May you do good and not evil.
7aebf413dSaswift#    May you find forgiveness for yourself and forgive others.
8aebf413dSaswift#    May you share freely, never taking more than you give.
9aebf413dSaswift#
10aebf413dSaswift#***********************************************************************
11aebf413dSaswift# This file implements regression tests for SQLite library.  The
12aebf413dSaswift# focus of this script is database locks.
13aebf413dSaswift#
14c7a3bb94Sdrh# $Id: lock6.test,v 1.3 2009/02/05 16:31:46 drh Exp $
15aebf413dSaswift
16aebf413dSaswift
17aebf413dSaswiftset testdir [file dirname $argv0]
18aebf413dSaswiftsource $testdir/tester.tcl
19aebf413dSaswift
20aebf413dSaswift# Launch another testfixture process to be controlled by this one. A
21aebf413dSaswift# channel name is returned that may be passed as the first argument to proc
22aebf413dSaswift# 'testfixture' to execute a command. The child testfixture process is shut
23aebf413dSaswift# down by closing the channel.
24aebf413dSaswiftproc launch_testfixture {} {
25aebf413dSaswift  set prg [info nameofexec]
26aebf413dSaswift  if {$prg eq ""} {
27aebf413dSaswift    set prg [file join . testfixture]
28aebf413dSaswift  }
29aebf413dSaswift  set chan [open "|$prg tf_main2.tcl" r+]
30aebf413dSaswift  fconfigure $chan -buffering line
31aebf413dSaswift  return $chan
32aebf413dSaswift}
33aebf413dSaswift
34aebf413dSaswift# Execute a command in a child testfixture process, connected by two-way
35aebf413dSaswift# channel $chan. Return the result of the command, or an error message.
36aebf413dSaswiftproc testfixture {chan cmd} {
37aebf413dSaswift  puts $chan $cmd
38aebf413dSaswift  puts $chan OVER
39aebf413dSaswift  set r ""
40aebf413dSaswift  while { 1 } {
41aebf413dSaswift    set line [gets $chan]
42aebf413dSaswift    if { $line == "OVER" } {
43aebf413dSaswift      return $r
44aebf413dSaswift    }
45aebf413dSaswift    append r $line
46aebf413dSaswift  }
47aebf413dSaswift}
48aebf413dSaswift
49aebf413dSaswift# Write the main loop for the child testfixture processes into file
50aebf413dSaswift# tf_main2.tcl. The parent (this script) interacts with the child processes
51aebf413dSaswift# via a two way pipe. The parent writes a script to the stdin of the child
52aebf413dSaswift# process, followed by the word "OVER" on a line of its own. The child
53aebf413dSaswift# process evaluates the script and writes the results to stdout, followed
54aebf413dSaswift# by an "OVER" of its own.
55aebf413dSaswiftset f [open tf_main2.tcl w]
56aebf413dSaswiftputs $f {
57aebf413dSaswift  set l [open log w]
58aebf413dSaswift  set script ""
59aebf413dSaswift  while {![eof stdin]} {
60aebf413dSaswift    flush stdout
61aebf413dSaswift    set line [gets stdin]
62aebf413dSaswift    puts $l "READ $line"
63aebf413dSaswift    if { $line == "OVER" } {
64aebf413dSaswift      catch {eval $script} result
65aebf413dSaswift      puts $result
66aebf413dSaswift      puts $l "WRITE $result"
67aebf413dSaswift      puts OVER
68aebf413dSaswift      puts $l "WRITE OVER"
69aebf413dSaswift      flush stdout
70aebf413dSaswift      set script ""
71aebf413dSaswift    } else {
72aebf413dSaswift      append script $line
73aebf413dSaswift      append script " ; "
74aebf413dSaswift    }
75aebf413dSaswift  }
76aebf413dSaswift  close $l
77aebf413dSaswift}
78aebf413dSaswiftclose $f
79aebf413dSaswift
80aebf413dSaswift
81838cce43Sdanielk1977ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
82aebf413dSaswift  set sqlite_hostid_num 1
83aebf413dSaswift
84aebf413dSaswift  set using_proxy 0
85aebf413dSaswift  foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
86aebf413dSaswift    set using_proxy $value
87aebf413dSaswift  }
88aebf413dSaswift
89aebf413dSaswift  # Test the lock_proxy_file pragmas.
90aebf413dSaswift  #
91aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) "1"
92aebf413dSaswift
93aebf413dSaswift  do_test lock6-1.1 {
94aebf413dSaswift    set ::tf1 [launch_testfixture]
95c7a3bb94Sdrh    testfixture $::tf1 "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
96aebf413dSaswift    testfixture $::tf1 {
97aebf413dSaswift      set sqlite_hostid_num 2
98aebf413dSaswift      sqlite3 db test.db -key xyzzy
99aebf413dSaswift      set lockpath [db eval {
100aebf413dSaswift        PRAGMA lock_proxy_file=":auto:";
101aebf413dSaswift        select * from sqlite_master;
102aebf413dSaswift        PRAGMA lock_proxy_file;
103aebf413dSaswift      }]
104aebf413dSaswift      string match "*test.db:auto:" $lockpath
105aebf413dSaswift    }
106aebf413dSaswift  } {1}
107aebf413dSaswift
108aebf413dSaswift  set sqlite_hostid_num 3
109aebf413dSaswift  do_test lock6-1.2 {
110aebf413dSaswift    execsql {pragma lock_status}
111aebf413dSaswift  } {main unlocked temp closed}
112aebf413dSaswift
113aebf413dSaswift  sqlite3_soft_heap_limit 0
114aebf413dSaswift  do_test lock6-1.3 {
115*14d14607Sdan    list [catch {
116aebf413dSaswift      sqlite3 db test.db
117*14d14607Sdan      execsql { select * from sqlite_master }
118*14d14607Sdan    } msg] $msg
119aebf413dSaswift  } {1 {database is locked}}
120aebf413dSaswift
121aebf413dSaswift  do_test lock6-1.4 {
122aebf413dSaswift    set lockpath [execsql {
123aebf413dSaswift      PRAGMA lock_proxy_file=":auto:";
124aebf413dSaswift      PRAGMA lock_proxy_file;
125aebf413dSaswift    } db]
126aebf413dSaswift    set lockpath
127aebf413dSaswift  } {{:auto: (not held)}}
128aebf413dSaswift
129aebf413dSaswift  do_test lock6-1.4.1 {
130aebf413dSaswift    catchsql {
131aebf413dSaswift      PRAGMA lock_proxy_file="notmine";
132aebf413dSaswift      select * from sqlite_master;
133aebf413dSaswift    } db
134aebf413dSaswift  } {1 {database is locked}}
135aebf413dSaswift
136aebf413dSaswift  do_test lock6-1.4.2 {
137aebf413dSaswift    execsql {
138aebf413dSaswift      PRAGMA lock_proxy_file;
139aebf413dSaswift    } db
140aebf413dSaswift  } {notmine}
141aebf413dSaswift
142aebf413dSaswift  do_test lock6-1.5 {
143aebf413dSaswift    testfixture $::tf1 {
144aebf413dSaswift      db eval {
145aebf413dSaswift        BEGIN;
146aebf413dSaswift        SELECT * FROM sqlite_master;
147aebf413dSaswift      }
148aebf413dSaswift    }
149aebf413dSaswift  } {}
150aebf413dSaswift
151aebf413dSaswift  catch {testfixture $::tf1 {db close}}
152aebf413dSaswift
153aebf413dSaswift  do_test lock6-1.6 {
154aebf413dSaswift    execsql {
155aebf413dSaswift      PRAGMA lock_proxy_file="mine";
156aebf413dSaswift      select * from sqlite_master;
157aebf413dSaswift    } db
158aebf413dSaswift  } {}
159aebf413dSaswift
160aebf413dSaswift  catch {close $::tf1}
161aebf413dSaswift  set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
162aebf413dSaswift  set sqlite_hostid_num 0
163aebf413dSaswift
164c1a60c51Sdan  sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
165aebf413dSaswift}
166aebf413dSaswift
167aebf413dSaswiftfinish_test
168