summaryrefslogtreecommitdiffstats
path: root/test/busy2.test
blob: ec420c8b2095aab038fa3416e5a2ee9e2fdaaf4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 2020 June 30
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file test the busy handler
#
# TESTRUNNER: slow


set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
set testprefix busy2

do_multiclient_test tn {
  do_test 1.$tn.0 {
    sql2 {
      CREATE TABLE t1(a, b);
      PRAGMA journal_mode = wal;
      INSERT INTO t1 VALUES('A', 'B');
    }
  } {wal}

  do_test 1.$tn.1 {
    code1 { db timeout 1000 }
    sql1 { SELECT * FROM t1 }
  } {A B}

  do_test 1.$tn.2 {
    sql2 {
      BEGIN;
        INSERT INTO t1 VALUES('C', 'D');
    }
  } {}

  do_test 1.$tn.3 {
    set us [lindex [time { catch { sql1 { BEGIN EXCLUSIVE } } }] 0]
    expr {$us>950000 && $us<1500000}
  } {1}

  do_test 1.$tn.4 {
    sql2 {
      COMMIT
    }
  } {}
}

#-------------------------------------------------------------------------

do_multiclient_test tn {
  # Make the db a WAL mode db. And add a table and a row to it. Then open
  # a second connection within process 1. Process 1 now has connections
  # [db] and [db1.2], process 2 has connection [db2] only.
  #
  # Configure all connections to use a 1000 ms timeout.
  #
  do_test 2.$tn.0 {
    code1 {
      sqlite3 db1.2 test.db
    }
    sql1 {
      PRAGMA auto_vacuum = off;
      PRAGMA journal_mode = wal;
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES(1, 2);
    }
    code2 {
      db2 timeout 1000
    }
    code1 {
      db1.2 timeout 1000
      db    timeout 1000
      db1.2 eval {SELECT * FROM t1}
    }
  } {1 2}

  # Take a read lock with [db] in process 1.
  #
  do_test 2.$tn.1 {
    sql1 {
      BEGIN;
        SELECT * FROM t1;
    }
  } {1 2}

  # Insert a row using [db2] in process 2. Then try a passive checkpoint.
  # It fails to checkpoint the final frame (due to the readlock taken by
  # [db]), and returns in less than 250ms.
  do_test 2.$tn.2 {
    sql2 { INSERT INTO t1 VALUES(3, 4) }
    set us [lindex [time {
      set res [code2 { db2 eval { PRAGMA wal_checkpoint } }]
    }] 0]
    list [expr $us < 250000] $res
  } {1 {0 4 3}}

  # Now try a FULL checkpoint with [db2]. It returns SQLITE_BUSY. And takes
  # over 950ms to do so.
  do_test 2.$tn.3 {
    set us [lindex [time {
      set res [code2 { db2 eval { PRAGMA wal_checkpoint = FULL } }]
    }] 0]
    list [expr $us > 950000] $res
  } {1 {1 4 3}}

  # Passive checkpoint with [db1.2] (process 1). No SQLITE_BUSY, returns
  # in under 250ms.
  do_test 2.$tn.4 {
    set us [lindex [time {
      set res [code1 { db1.2 eval { PRAGMA wal_checkpoint } }]
    }] 0]
    list [expr $us < 250000] $res
  } {1 {0 4 3}}

  # Full checkpoint with [db1.2] (process 1). SQLITE_BUSY returned in
  # a bit over 950ms.
  do_test 2.$tn.5 {
    set us [lindex [time {
      set res [code1 { db1.2 eval { PRAGMA wal_checkpoint = FULL } }]
    }] 0]
    list [expr $us > 950000] $res
  } {1 {1 4 3}}

  code1 {
    db1.2 close
  }
}

#-------------------------------------------------------------------------
# Check that even if the busy-handler fails (returns zero) within a
# call to sqlite3_prepare() (or _v2(), or _v3()), it is still invoked
# the next time an SQLITE_BUSY is encountered.
#
do_multiclient_test tn {
  code1 {
    set ::busy_called 0
    proc busy {args} {
      if {$::busy_called} { return 1 }
      set ::busy_called 1
      return 0
    }
    db busy busy
  }

  do_test 3.$tn.1 {
    sql2 {
      CREATE TABLE t1(x);
      BEGIN EXCLUSIVE;
        INSERT INTO t1 VALUES('x');
    }
  } {}

  do_test 3.$tn.2 {
    set ::busy_called 0
    list [catch { sql1 { SELECT * FROM t1 } } msg] $::busy_called
  } {1 1}

  do_test 3.$tn.3 {
    set ::busy_called 0
    list [catch { sql1 { SELECT * FROM t1 } } msg] $::busy_called
  } {1 1}

}

finish_test