• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 2010 June 30
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 file is testing the sqlite3_unlock_notify() API.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
18# This script only runs if shared-cache and unlock-notify are available.
19#
20ifcapable !unlock_notify||!shared_cache {
21  finish_test
22  return
23}
24
25set esc [sqlite3_enable_shared_cache 1]
26
27sqlite3 db  test.db
28file delete -force test.db2 test.db2-journal test.db2-wal
29sqlite3 db2 test.db2
30
31do_test notify3-1.1 {
32  execsql {
33    CREATE TABLE t1(a, b);
34    INSERT INTO t1 VALUES('t1 A', 't1 B');
35  }
36} {}
37do_test notify3-1.2 {
38  execsql {
39    CREATE TABLE t2(a, b);
40    INSERT INTO t2 VALUES('t2 A', 't2 B');
41  } db2
42} {}
43
44do_test notify3-1.3 {
45  execsql {
46    BEGIN EXCLUSIVE;
47    INSERT INTO t2 VALUES('t2 C', 't2 D');
48  } db2
49} {}
50do_test notify3-1.4 {
51  catchsql { ATTACH 'test.db2' AS aux }
52} {0 {}}
53
54do_test notify3-1.5 {
55  catchsql { SELECT * FROM t2 }
56} {1 {database schema is locked: aux}}
57
58do_test notify3-1.6 {
59  list [sqlite3_errcode db] [sqlite3_extended_errcode db]
60} {SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE}
61
62do_test notify3-1.7 {
63  sqlite3_extended_result_codes db 1
64  catch { set ::stmt [sqlite3_prepare_v2 db "SELECT * FROM t2" -1 tail] } msg
65  set msg
66} {(262) database schema is locked: aux}
67
68do_test notify3-1.8 {
69  set ::when 1
70  db unlock_notify { set ::res $::when }
71  set ::when 2
72  execsql { COMMIT } db2
73  set ::res
74} {2}
75do_test notify3-1.9 {
76  catchsql { SELECT * FROM t2 }
77} {0 {{t2 A} {t2 B} {t2 C} {t2 D}}}
78db close
79
80
81set err   {{1 {unable to open database: test.db2}}}
82set noerr {{0 {}}}
83
84# When a new database is attached, the connection doing the attaching
85# tries to load any unloaded schemas for both the new database and any
86# already attached databases (including the main database). If it is
87# unable to load any such schemas, then the ATTACH statement fails.
88#
89# This block tests that if the loading of schemas as a result of an
90# ATTACH fails due to locks on the schema table held by other shared-cache
91# connections the extended error code is SQLITE_LOCKED_SHAREDCACHE and
92# it is possible to use the unlock-notify mechanism to determine when
93# the ATTACH might succeed.
94#
95# This test does not work for test-permutations that specify SQL to
96# be executed as part of the [sqlite3] command that opens the database.
97# Executing such SQL causes SQLite to load the database schema into memory
98# earlier than expected, causing test cases to fail.
99#
100if {[presql] == ""} {
101  foreach {
102    tn
103    db1_loaded
104    db2_loaded
105    enable_extended_errors
106    result
107    error1 error2
108  } "
109    0   0 0 0   $err     SQLITE_LOCKED               SQLITE_LOCKED_SHAREDCACHE
110    1   0 0 1   $err     SQLITE_LOCKED_SHAREDCACHE   SQLITE_LOCKED_SHAREDCACHE
111    2   0 1 0   $err     SQLITE_LOCKED               SQLITE_LOCKED_SHAREDCACHE
112    3   0 1 1   $err     SQLITE_LOCKED_SHAREDCACHE   SQLITE_LOCKED_SHAREDCACHE
113    4   1 0 0   $err     SQLITE_LOCKED               SQLITE_LOCKED_SHAREDCACHE
114    5   1 0 1   $err     SQLITE_LOCKED_SHAREDCACHE   SQLITE_LOCKED_SHAREDCACHE
115    6   1 1 0   $noerr   SQLITE_OK                   SQLITE_OK
116    7   1 1 1   $noerr   SQLITE_OK                   SQLITE_OK
117  " {
118
119    do_test notify3-2.$tn.1 {
120      catch { db1 close }
121      catch { db2 close }
122      sqlite3 db1 test.db
123      sqlite3 db2 test.db2
124
125      sqlite3_extended_result_codes db1 $enable_extended_errors
126      sqlite3_extended_result_codes db2 $enable_extended_errors
127
128      if { $db1_loaded } { db1 eval "SELECT * FROM sqlite_master" }
129      if { $db2_loaded } { db2 eval "SELECT * FROM sqlite_master" }
130
131      db2 eval "BEGIN EXCLUSIVE"
132      catchsql "ATTACH 'test.db2' AS two" db1
133    } $result
134
135    do_test notify3-2.$tn.2 {
136      list [sqlite3_errcode db1] [sqlite3_extended_errcode db1]
137    } [list $error1 $error2]
138
139    do_test notify3-2.$tn.3 {
140      db1 unlock_notify {set invoked 1}
141      set invoked 0
142      db2 eval commit
143      set invoked
144    } [lindex $result 0]
145  }
146}
147catch { db1 close }
148catch { db2 close }
149
150
151sqlite3_enable_shared_cache $esc
152finish_test
153
154