xref: /sqlite-3.40.0/test/shared2.test (revision 0f198a74)
1ed429311Sdanielk1977# 2005 January 19
2ed429311Sdanielk1977#
3ed429311Sdanielk1977# The author disclaims copyright to this source code.  In place of
4ed429311Sdanielk1977# a legal notice, here is a blessing:
5ed429311Sdanielk1977#
6ed429311Sdanielk1977#    May you do good and not evil.
7ed429311Sdanielk1977#    May you find forgiveness for yourself and forgive others.
8ed429311Sdanielk1977#    May you share freely, never taking more than you give.
9ed429311Sdanielk1977#
10ed429311Sdanielk1977#***********************************************************************
11ed429311Sdanielk1977#
12dda70fe3Sdrh# $Id: shared2.test,v 1.8 2009/06/05 17:09:12 drh Exp $
13ed429311Sdanielk1977
14ed429311Sdanielk1977set testdir [file dirname $argv0]
15ed429311Sdanielk1977source $testdir/tester.tcl
162969a587Sdansource $testdir/lock_common.tcl
172969a587Sdansource $testdir/malloc_common.tcl
18ed429311Sdanielk1977db close
19ed429311Sdanielk1977
20ed429311Sdanielk1977ifcapable !shared_cache {
21ed429311Sdanielk1977  finish_test
22ed429311Sdanielk1977  return
23ed429311Sdanielk1977}
24ed429311Sdanielk1977set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
25ed429311Sdanielk1977
26ed429311Sdanielk1977# Test that if we delete all rows from a table any read-uncommitted
27ed429311Sdanielk1977# cursors are correctly invalidated. Test on both table and index btrees.
28ed429311Sdanielk1977do_test shared2-1.1 {
29ed429311Sdanielk1977  sqlite3 db1 test.db
30ed429311Sdanielk1977  sqlite3 db2 test.db
31ed429311Sdanielk1977
32ed429311Sdanielk1977  # Set up some data. Table "numbers" has 64 rows after this block
33ed429311Sdanielk1977  # is executed.
34ed429311Sdanielk1977  execsql {
35ed429311Sdanielk1977    BEGIN;
36ed429311Sdanielk1977    CREATE TABLE numbers(a PRIMARY KEY, b);
37ed429311Sdanielk1977    INSERT INTO numbers(oid) VALUES(NULL);
38ed429311Sdanielk1977    INSERT INTO numbers(oid) SELECT NULL FROM numbers;
39ed429311Sdanielk1977    INSERT INTO numbers(oid) SELECT NULL FROM numbers;
40ed429311Sdanielk1977    INSERT INTO numbers(oid) SELECT NULL FROM numbers;
41ed429311Sdanielk1977    INSERT INTO numbers(oid) SELECT NULL FROM numbers;
42ed429311Sdanielk1977    INSERT INTO numbers(oid) SELECT NULL FROM numbers;
43ed429311Sdanielk1977    INSERT INTO numbers(oid) SELECT NULL FROM numbers;
44ed429311Sdanielk1977    UPDATE numbers set a = oid, b = 'abcdefghijklmnopqrstuvwxyz0123456789';
45ed429311Sdanielk1977    COMMIT;
46ed429311Sdanielk1977  } db1
47ed429311Sdanielk1977} {}
48ed429311Sdanielk1977do_test shared2-1.2 {
49ed429311Sdanielk1977  # Put connection 2 in read-uncommitted mode and start a SELECT on table
50ed429311Sdanielk1977  # 'numbers'. Half way through the SELECT, use connection 1 to delete the
51ed429311Sdanielk1977  # contents of this table.
52ed429311Sdanielk1977  execsql {
53ed429311Sdanielk1977    pragma read_uncommitted = 1;
54ed429311Sdanielk1977  } db2
55ed429311Sdanielk1977  set count [execsql {SELECT count(*) FROM numbers} db2]
56ed429311Sdanielk1977  db2 eval {SELECT a FROM numbers ORDER BY oid} {
57ed429311Sdanielk1977    if {$a==32} {
58ed429311Sdanielk1977      execsql {
59ed429311Sdanielk1977        BEGIN;
60ed429311Sdanielk1977        DELETE FROM numbers;
61ed429311Sdanielk1977      } db1
62ed429311Sdanielk1977    }
63ed429311Sdanielk1977  }
64ed429311Sdanielk1977  list $a $count
65ed429311Sdanielk1977} {32 64}
66ed429311Sdanielk1977do_test shared2-1.3 {
67ed429311Sdanielk1977  # Same test as 1.2, except scan using the index this time.
68ed429311Sdanielk1977  execsql {
69ed429311Sdanielk1977    ROLLBACK;
70ed429311Sdanielk1977  } db1
71ed429311Sdanielk1977  set count [execsql {SELECT count(*) FROM numbers} db2]
72ed429311Sdanielk1977  db2 eval {SELECT a, b FROM numbers ORDER BY a} {
73ed429311Sdanielk1977    if {$a==32} {
74ed429311Sdanielk1977      execsql {
75ed429311Sdanielk1977        DELETE FROM numbers;
76ed429311Sdanielk1977      } db1
77ed429311Sdanielk1977    }
78ed429311Sdanielk1977  }
79ed429311Sdanielk1977  list $a $count
80ed429311Sdanielk1977} {32 64}
81ed429311Sdanielk1977
822b8c13e7Sdanielk1977
83ed429311Sdanielk1977db1 close
84ed429311Sdanielk1977db2 close
85ed429311Sdanielk1977
862b8c13e7Sdanielk1977do_test shared2-3.2 {
877246f5b9Sdanielk1977  sqlite3_enable_shared_cache 1
884a50aac5Sdrh} {1}
897246f5b9Sdanielk1977
90*fda06befSmistachkinforcedelete test.db
9121822c58Sdanielk1977
9221822c58Sdanielk1977sqlite3 db test.db
9321822c58Sdanielk1977do_test shared2-4.1 {
9421822c58Sdanielk1977  execsql {
9521822c58Sdanielk1977    CREATE TABLE t0(a, b);
9621822c58Sdanielk1977    CREATE TABLE t1(a, b DEFAULT 'hello world');
9721822c58Sdanielk1977  }
9821822c58Sdanielk1977} {}
9921822c58Sdanielk1977db close
10021822c58Sdanielk1977
10121822c58Sdanielk1977sqlite3 db test.db
10221822c58Sdanielk1977sqlite3 db2 test.db
10321822c58Sdanielk1977
10421822c58Sdanielk1977do_test shared2-4.2 {
10521822c58Sdanielk1977  execsql { SELECT a, b FROM t0 } db
10621822c58Sdanielk1977  execsql { INSERT INTO t1(a) VALUES(1) } db2
10721822c58Sdanielk1977} {}
10821822c58Sdanielk1977
10921822c58Sdanielk1977do_test shared2-4.3 {
11021822c58Sdanielk1977  db2 close
11121822c58Sdanielk1977  db close
11221822c58Sdanielk1977} {}
11321822c58Sdanielk1977
114cb9d8d88Sdanielk1977# At one point, this was causing a crash.
115cb9d8d88Sdanielk1977#
116cb9d8d88Sdanielk1977do_test shared2-5.1 {
117cb9d8d88Sdanielk1977  sqlite3 db test.db
118cb9d8d88Sdanielk1977  sqlite3 db2 test.db
119cb9d8d88Sdanielk1977  execsql { CREATE TABLE t2(a, b, c) }
120cb9d8d88Sdanielk1977
121cb9d8d88Sdanielk1977  # The following statement would crash when attempting to sqlite3_free()
122cb9d8d88Sdanielk1977  # a pointer allocated from a lookaside buffer.
123cb9d8d88Sdanielk1977  execsql { CREATE INDEX i1 ON t2(a) } db2
124cb9d8d88Sdanielk1977} {}
125cb9d8d88Sdanielk1977
126cb9d8d88Sdanielk1977db close
127cb9d8d88Sdanielk1977db2 close
128cb9d8d88Sdanielk1977
1292969a587Sdan# The following test verifies that shared-cache mode does not automatically
1302969a587Sdan# turn on exclusive-locking mode for some reason.
1312969a587Sdando_multiclient_test {tn} {
1322969a587Sdan  sql1 { CREATE TABLE t1(a, b) }
1332969a587Sdan  sql2 { CREATE TABLE t2(a, b) }
1342969a587Sdan  do_test shared2-6.$tn.1 { sql1 { SELECT * FROM t2 } } {}
1352969a587Sdan  do_test shared2-6.$tn.2 { sql2 { SELECT * FROM t1 } } {}
1362969a587Sdan}
1372969a587Sdan
138ed429311Sdanielk1977sqlite3_enable_shared_cache $::enable_shared_cache
139ed429311Sdanielk1977finish_test
140