interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / walsetlk3.test

135 lines · 2898 bytes Raw Blame History
1# 2020 May 06
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# TESTRUNNER: slow
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17source $testdir/lock_common.tcl
18set testprefix walsetlk3
19
20ifcapable !wal {finish_test ; return }
21ifcapable !setlk_timeout {finish_test ; return }
22
23do_execsql_test 1.0 {
24 CREATE TABLE t1(x, y);
25 PRAGMA journal_mode = wal;
26 INSERT INTO t1 VALUES(1, 2);
27 INSERT INTO t1 VALUES(3, 4);
28} {wal}
29
30db close
31
32proc sql_block_on_close {sql} {
33 testfixture_nb done [string map [list %SQL% $sql] {
34 testvfs tvfs
35 tvfs script xWrite
36 tvfs filter xWrite
37
38 set ::delay_done 0
39 proc xWrite {method fname args} {
40 if {[file tail $fname]=="test.db" && $::delay_done==0} {
41 after 3000
42 set ::delay_done 1
43 }
44 return 0
45 }
46
47 sqlite3 db test.db -vfs tvfs
48 db eval {%SQL%}
49 db close
50 }]
51}
52
53# Start a second process that writes to the db, then blocks within the
54# [db close] holding an EXCLUSIVE on the db in order to checkpoint and
55# delete the wal file. Then try to read the db.
56#
57# Without the SQLITE_SETLK_BLOCK_ON_CONNECT flag, this should fail with
58# SQLITE_BUSY.
59#
60sql_block_on_close {
61 INSERT INTO t1 VALUES(5, 6);
62 INSERT INTO t1 VALUES(7, 8);
63}
64after 500 {set ok 1}
65vwait ok
66sqlite3 db test.db
67sqlite3_setlk_timeout db 2000
68do_catchsql_test 1.1 {
69 SELECT * FROM t1
70} {1 {database is locked}}
71
72vwait ::done
73
74# But with SQLITE_SETLK_BLOCK_ON_CONNECT flag, it should succeed.
75#
76sql_block_on_close {
77 INSERT INTO t1 VALUES(9, 10);
78 INSERT INTO t1 VALUES(11, 12);
79}
80after 500 {set ok 1}
81vwait ok
82sqlite3 db test.db
83sqlite3_setlk_timeout -block db 2000
84do_catchsql_test 1.2 {
85 SELECT * FROM t1
86} {0 {1 2 3 4 5 6 7 8 9 10 11 12}}
87
88vwait ::done
89
90#-------------------------------------------------------------------------
91# Check that the SQLITE_SETLK_BLOCK_ON_CONNECT does not cause connections
92# to block when taking a SHARED lock on a rollback mode database.
93#
94reset_db
95do_execsql_test 2.1 {
96 CREATE TABLE x1(a);
97 INSERT INTO x1 VALUES(1), (2), (3);
98}
99
100proc sql_block_on_write {sql} {
101 testfixture_nb done [string map [list %SQL% $sql] {
102 sqlite3 db test.db
103 db eval "BEGIN EXCLUSIVE"
104 db eval {%SQL%}
105 after 3000
106 db eval COMMIT
107 db close
108 }]
109}
110
111db close
112sql_block_on_write {
113 INSERT INTO x1 VALUES(4);
114}
115
116after 500 {set ok 1}
117vwait ok
118
119sqlite3 db test.db
120sqlite3_setlk_timeout -block db 2000
121
122do_catchsql_test 2.2 {
123 SELECT * FROM x1
124} {1 {database is locked}}
125
126vwait ::done
127after 500 {set ok 1}
128vwait ok
129
130do_catchsql_test 2.3 {
131 SELECT * FROM x1
132} {0 {1 2 3 4}}
133
134finish_test
135