xref: /sqlite-3.40.0/test/sharedA.test (revision d5dd3305)
1# 2013 May 14
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#
12# Test some specific circumstances to do with shared cache mode.
13#
14
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18if {[run_thread_tests]==0} { finish_test ; return }
19db close
20set ::testprefix sharedA
21
22set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
23
24#-------------------------------------------------------------------------
25#
26do_test 0.1 {
27  sqlite3 db1 test.db
28  sqlite3 db2 test.db
29
30  db1 eval {
31    CREATE TABLE t1(x);
32    INSERT INTO t1 VALUES(randomblob(100));
33    INSERT INTO t1 SELECT randomblob(100) FROM t1;
34    INSERT INTO t1 SELECT randomblob(100) FROM t1;
35    INSERT INTO t1 SELECT randomblob(100) FROM t1;
36    INSERT INTO t1 SELECT randomblob(100) FROM t1;
37    INSERT INTO t1 SELECT randomblob(100) FROM t1;
38    INSERT INTO t1 SELECT randomblob(100) FROM t1;
39    CREATE INDEX i1 ON t1(x);
40  }
41
42  db1 eval {
43    BEGIN;
44    DROP INDEX i1;
45  }
46
47  db2 close
48
49  db1 eval {
50    INSERT INTO t1 SELECT randomblob(100) FROM t1;
51    ROLLBACK;
52    PRAGMA integrity_check;
53  }
54} {ok}
55
56db1 close
57forcedelete test.db
58
59
60#-------------------------------------------------------------------------
61#
62do_test 1.1 {
63  sqlite3 db1 test.db
64  sqlite3 db2 test.db
65  db2 eval {
66    CREATE TABLE t1(x);
67    INSERT INTO t1 VALUES(123);
68  }
69  db1 eval {
70    SELECT * FROM t1;
71    CREATE INDEX i1 ON t1(x);
72  }
73} {123}
74
75do_test 1.2 {
76  db2 eval { SELECT * FROM t1 ORDER BY x; }
77
78  db1 eval {
79    BEGIN; DROP INDEX i1;
80  }
81  db1 close
82
83  db2 eval { SELECT * FROM t1 ORDER BY x; }
84} {123}
85
86do_test 1.3 {
87  db2 close
88} {}
89
90#-------------------------------------------------------------------------
91#
92testvfs tvfs
93tvfs filter xRead
94tvfs script read_callback
95proc read_callback {args} { }
96
97do_test 2.1 {
98  forcedelete test.db test.db2
99  sqlite3 db1 test.db -vfs tvfs
100  db1 eval { ATTACH 'test.db2' AS two }
101
102  db1 eval {
103    CREATE TABLE t1(x);
104    INSERT INTO t1 VALUES(1);
105    INSERT INTO t1 VALUES(2);
106    INSERT INTO t1 VALUES(3);
107    CREATE TABLE two.t2(x);
108    INSERT INTO t2 SELECT * FROM t1;
109  }
110
111  sqlite3 db2 test.db -vfs tvfs
112  db2 eval { SELECT * FROM t1 }
113} {1 2 3}
114
115do_test 2.2 {
116  set ::STMT [sqlite3_prepare db2 "CREATE INDEX i1 ON t1(x)" -1 tail]
117  set {} {}
118} {}
119
120do_test 2.3 {
121  db1 eval {
122    BEGIN;
123      CREATE INDEX i1 ON t1(x);
124      INSERT INTO t2 VALUES('value!');
125  }
126} {}
127
128set ::bFired 0
129proc read_callback {call file args} {
130  if { $::bFired==0 && [string match *test.db2-journal $file] } {
131    sqlthread spawn ::thread_result [subst -nocommands {
132      sqlite3_step $::STMT
133      set rc [sqlite3_finalize $::STMT]
134    }]
135    after 1000 { set ::bFired 1 }
136    vwait ::bFired
137  }
138}
139do_test 2.4 { db1 eval ROLLBACK } {}
140
141if {[info exists ::thread_result]==0} { vwait ::thread_result }
142do_test 2.5 {
143  list $::thread_result [sqlite3_errmsg db2]
144} {SQLITE_SCHEMA {database schema has changed}}
145
146db1 close
147db2 close
148tvfs delete
149
150sqlite3_enable_shared_cache $::enable_shared_cache
151finish_test
152
153