summaryrefslogtreecommitdiffstats
path: root/test/notify1.test
blob: be6a122585da99e1ccecdad95b242f8651109d44 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# 2009 March 04
#
# 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 implements regression tests for SQLite library.  The
# focus of this file is testing the sqlite3_unlock_notify() API.
#
# $Id: notify1.test,v 1.4 2009/06/05 17:09:12 drh Exp $

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

ifcapable !unlock_notify||!shared_cache {
  finish_test
  return
}
db close
set ::enable_shared_cache [sqlite3_enable_shared_cache 1]

#-------------------------------------------------------------------------
# Warm body test. Test that an unlock-notify callback can be registered 
# and that it is invoked.
#
do_test notify1-1.1 {
  sqlite3 db test.db
  sqlite3 db2 test.db
  execsql { CREATE TABLE t1(a, b) }
} {}
do_test notify1-1.2 {
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(1, 2);
  }
  catchsql { INSERT INTO t1 VALUES(3, 4) } db2
} {1 {database table is locked}}
do_test notify1-1.3 {
  set zScript ""
  db2 unlock_notify {
    set zScript "db2 eval { INSERT INTO t1 VALUES(3, 4) }"
  }
  execsql { SELECT * FROM t1 }
} {1 2}
do_test notify1-1.4 {
  set zScript
} {}
do_test notify1-1.5 {
  execsql { COMMIT }
  eval $zScript
  execsql { SELECT * FROM t1 }
} {1 2 3 4}

#-------------------------------------------------------------------------
# Verify that invoking the "unlock_notify" method with no arguments
# (which is the equivalent of invoking sqlite3_unlock_notify() with
# a NULL xNotify argument) cancels a pending notify callback.
#
do_test notify1-1.11 {
  execsql { DROP TABLE t1; CREATE TABLE t1(a, b) }
} {}
do_test notify1-1.12 {
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(1, 2);
  }
  catchsql { INSERT INTO t1 VALUES(3, 4) } db2
} {1 {database table is locked}}
do_test notify1-1.13 {
  set zScript ""
  db2 unlock_notify {
    set zScript "db2 eval { INSERT INTO t1 VALUES(3, 4) }"
  }
  execsql { SELECT * FROM t1 }
} {1 2}
do_test notify1-1.14 {
  set zScript
} {}
do_test notify1-1.15 {
  db2 unlock_notify
  execsql { COMMIT }
  eval $zScript
  execsql { SELECT * FROM t1 }
} {1 2}

#-------------------------------------------------------------------------
# The following tests, notify1-2.*, test that deadlock is detected 
# correctly.
# 
do_test notify1-2.1 {
  execsql { 
    CREATE TABLE t2(a, b);
    INSERT INTO t2 VALUES('I', 'II');
  }
} {}

#
# Test for simple deadlock involving two database connections.
#
# 1. Grab a write-lock on t1 with [db]. Then grab a read-lock on t2 with [db2].
# 2. Try to grab a read-lock on t1 with [db2] (fails).
# 3. Have [db2] wait on the read-lock it failed to obtain in step 2.
# 4. Try to grab a write-lock on t2 with [db] (fails).
# 5. Try to have [db] wait on the lock from step 4. Fails, as the system
#    would be deadlocked (since [db2] is already waiting on [db], and this
#    operation would have [db] wait on [db2]).
#
do_test notify1-2.2.1 {
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(5, 6);
  }
  execsql {
    BEGIN;
    SELECT * FROM t2;
  } db2
} {I II}
do_test notify1-2.2.2 {
  catchsql { SELECT * FROM t1 } db2
} {1 {database table is locked: t1}}
do_test notify1-2.2.3 {
  db2 unlock_notify {lappend unlock_notify db2}
} {}
do_test notify1-2.2.4 {
  catchsql { INSERT INTO t2 VALUES('III', 'IV') }
} {1 {database table is locked: t2}}
do_test notify1-2.2.5 {
  set rc [catch { db unlock_notify {lappend unlock_notify db} } msg]
  list $rc $msg
} {1 {database is deadlocked}}

#
# Test for slightly more complex deadlock involving three database
# connections: db, db2 and db3.
#
do_test notify1-2.3.1 {
  db close
  db2 close
  forcedelete test.db test2.db test3.db
  foreach con {db db2 db3} {
    sqlite3 $con test.db
    $con eval { ATTACH 'test2.db' AS aux2 }
    $con eval { ATTACH 'test3.db' AS aux3 }
  }
  execsql {
    CREATE TABLE main.t1(a, b);
    CREATE TABLE aux2.t2(a, b);
    CREATE TABLE aux3.t3(a, b);
  }
} {}
do_test notify1-2.3.2 {
  execsql { BEGIN ; INSERT INTO t1 VALUES(1, 2) } db
  execsql { BEGIN ; INSERT INTO t2 VALUES(1, 2) } db2
  execsql { BEGIN ; INSERT INTO t3 VALUES(1, 2) } db3
} {}
do_test notify1-2.3.3 {
  catchsql { SELECT * FROM t2 } db
} {1 {database table is locked: t2}}
do_test notify1-2.3.4 {
  catchsql { SELECT * FROM t3 } db2
} {1 {database table is locked: t3}}
do_test notify1-2.3.5 {
  catchsql { SELECT * FROM t1 } db3
} {1 {database table is locked: t1}}
do_test notify1-2.3.6 {
  set lUnlock [list]
  db  unlock_notify {lappend lUnlock db}
  db2 unlock_notify {lappend lUnlock db2}
} {}
do_test notify1-2.3.7 {
  set rc [catch { db3 unlock_notify {lappend lUnlock db3} } msg]
  list $rc $msg
} {1 {database is deadlocked}}
do_test notify1-2.3.8 {
  execsql { COMMIT }
  set lUnlock
} {}
do_test notify1-2.3.9 {
  db3 unlock_notify {lappend lUnlock db3} 
  set lUnlock
} {db3}
do_test notify1-2.3.10 {
  execsql { COMMIT } db2
  set lUnlock
} {db3 db}
do_test notify1-2.3.11 {
  execsql { COMMIT } db3
  set lUnlock
} {db3 db db2}
catch { db3 close }
catch { db2 close }
catch { db close }

#-------------------------------------------------------------------------
# The following tests, notify1-3.* and notify1-4.*, test that callbacks 
# can be issued when there are many (>16) connections waiting on a single 
# unlock event.
# 
foreach {tn nConn} {3 20 4 76} {
  do_test notify1-$tn.1 {
    sqlite3 db test.db
    execsql {
      BEGIN;
      INSERT INTO t1 VALUES('a', 'b');
    }
  } {}
  set lUnlock [list]
  set lUnlockFinal [list]
  for {set ii 1} {$ii <= $nConn} {incr ii} {
    do_test notify1-$tn.2.$ii.1 {
      set cmd "db$ii"
      sqlite3 $cmd test.db
      catchsql { SELECT * FROM t1 } $cmd
    } {1 {database table is locked: t1}}
    do_test notify1-$tn.2.$ii.2 {
      $cmd unlock_notify "lappend lUnlock $ii"
    } {}
    lappend lUnlockFinal $ii
  }
  do_test notify1-$tn.3 {
    set lUnlock
  } {}
  do_test notify1-$tn.4 {
    execsql {COMMIT}
    lsort -integer $lUnlock
  } $lUnlockFinal
  do_test notify1-$tn.5 {
    for {set ii 1} {$ii <= $nConn} {incr ii} {
      "db$ii" close
    }
  } {}
}
db close

#-------------------------------------------------------------------------
# These tests, notify1-5.*, test that a malloc() failure that occurs while
# allocating an array to use as an argument to an unlock-notify callback
# is handled correctly.
# 
source $testdir/malloc_common.tcl
do_malloc_test notify1-5 -tclprep {
  set ::lUnlock [list]
  execsql {
    CREATE TABLE t1(a, b);
    BEGIN;
    INSERT INTO t1 VALUES('a', 'b');
  }
  for {set ii 1} {$ii <= 60} {incr ii} {
    set cmd "db$ii"
    sqlite3 $cmd test.db
    catchsql { SELECT * FROM t1 } $cmd
    $cmd unlock_notify "lappend ::lUnlock $ii"
  }
} -sqlbody {
  COMMIT;
} -cleanup {
  # One of two things should have happened:
  #
  #   1) The transaction opened by [db] was not committed. No unlock-notify
  #      callbacks were invoked, OR
  #   2) The transaction opened by [db] was committed and 60 unlock-notify
  #      callbacks were invoked.
  #
  do_test notify1-5.systemstate {
    expr { ([llength $::lUnlock]==0 && [sqlite3_get_autocommit db]==0)
        || ([llength $::lUnlock]==60 && [sqlite3_get_autocommit db]==1)
    }
  } {1}
  for {set ii 1} {$ii <= 60} {incr ii} { "db$ii" close }
}

#-------------------------------------------------------------------------
# Test cases notify1-6.* test cases where the following occur:
# 
#   notify1-6.1.*: Test encountering an SQLITE_LOCKED error when the
#                  "blocking connection" has already been set by a previous
#                  SQLITE_LOCKED.
#
#   notify1-6.2.*: Test encountering an SQLITE_LOCKED error when already
#                  waiting on an unlock-notify callback.
#
#   notify1-6.3.*: Test that if an SQLITE_LOCKED error is encountered while
#                  already waiting on an unlock-notify callback, and then
#                  the blocker that caused the SQLITE_LOCKED commits its
#                  transaction, the unlock-notify callback is not invoked.
#
#   notify1-6.4.*: Like 6.3.*, except that instead of the second blocker
#                  committing its transaction, the first does. The 
#                  unlock-notify callback is therefore invoked.
#
db close
do_test notify1-6.1.1 {
  forcedelete test.db test2.db
  foreach conn {db db2 db3} {
    sqlite3 $conn test.db
    execsql { ATTACH 'test2.db' AS two } $conn
  }
  execsql {
    CREATE TABLE t1(a, b);
    CREATE TABLE two.t2(a, b);
  }
  execsql { 
    BEGIN;
    INSERT INTO t1 VALUES(1, 2);
  } db2
  execsql { 
    BEGIN;
    INSERT INTO t2 VALUES(1, 2);
  } db3
} {}
do_test notify1-6.1.2 {
  catchsql { SELECT * FROM t2 }
} {1 {database table is locked: t2}}
do_test notify1-6.1.3 {
  catchsql { SELECT * FROM t1 }
} {1 {database table is locked: t1}}

do_test notify1-6.2.1 {
  set unlocked 0
  db unlock_notify {set unlocked 1}
  set unlocked
} {0}
do_test notify1-6.2.2 {
  catchsql { SELECT * FROM t2 }
} {1 {database table is locked: t2}}
do_test notify1-6.2.3 {
  execsql { COMMIT } db2
  set unlocked
} {1}

do_test notify1-6.3.1 {
  execsql { 
    BEGIN;
    INSERT INTO t1 VALUES(3, 4);
  } db2
} {}
do_test notify1-6.3.2 {
  catchsql { SELECT * FROM t1 }
} {1 {database table is locked: t1}}
do_test notify1-6.3.3 {
  set unlocked 0
  db unlock_notify {set unlocked 1}
  set unlocked
} {0}
do_test notify1-6.3.4 {
  catchsql { SELECT * FROM t2 }
} {1 {database table is locked: t2}}
do_test notify1-6.3.5 {
  execsql { COMMIT } db3
  set unlocked
} {0}

do_test notify1-6.4.1 {
  execsql { 
    BEGIN;
    INSERT INTO t2 VALUES(3, 4);
  } db3
  catchsql { SELECT * FROM t2 }
} {1 {database table is locked: t2}}
do_test notify1-6.4.2 {
  execsql { COMMIT } db2
  set unlocked
} {1}
do_test notify1-6.4.3 {
  execsql { COMMIT } db3
} {}
db close
db2 close
db3 close

#-------------------------------------------------------------------------
# Test cases notify1-7.* tests that when more than one distinct 
# unlock-notify function is registered, all are invoked correctly.
#
proc unlock_notify {} {
  incr ::unlock_notify
}
do_test notify1-7.1 {
  foreach conn {db db2 db3} {
    sqlite3 $conn test.db
  }
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(5, 6);
  }
} {}
do_test notify1-7.2 {
  catchsql { SELECT * FROM t1 } db2
} {1 {database table is locked: t1}}
do_test notify1-7.3 {
  catchsql { SELECT * FROM t1 } db3
} {1 {database table is locked: t1}}
do_test notify1-7.4 {
  set unlock_notify 0
  db2 unlock_notify unlock_notify
  sqlite3_unlock_notify db3
} {SQLITE_OK}
do_test notify1-7.5 {
  set unlock_notify
} {0}
do_test notify1-7.6 {
  execsql { COMMIT }
  set unlock_notify
} {2}

#-------------------------------------------------------------------------
# Test cases notify1-8.* tests that the correct SQLITE_LOCKED extended 
# error code is returned in various scenarios.
#
do_test notify1-8.1 {
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(7, 8);
  }
  catchsql { SELECT * FROM t1 } db2
} {1 {database table is locked: t1}}
do_test notify1-8.2 {
  sqlite3_extended_errcode db2
} {SQLITE_LOCKED_SHAREDCACHE}

do_test notify1-8.3 {
  execsql {
    COMMIT;
    BEGIN EXCLUSIVE;
  }
  catchsql { SELECT * FROM t1 } db2
} {1 {database schema is locked: main}}
do_test notify1-8.4 {
  sqlite3_extended_errcode db2
} {SQLITE_LOCKED_SHAREDCACHE}

do_test notify1-8.X {
  execsql { COMMIT } 
} {}

#-------------------------------------------------------------------------
# Test cases notify1-9.* test the shared-cache 'pending-lock' feature.
#
do_test notify1-9.1 {
  execsql {
    CREATE TABLE t2(a, b);
    BEGIN;
    SELECT * FROM t1;
  } db2
} {1 2 3 4 5 6 7 8}
do_test notify1-9.2 {
  execsql { SELECT * FROM t1 } db3
} {1 2 3 4 5 6 7 8}
do_test notify1-9.3 {
  catchsql { 
    BEGIN;
    INSERT INTO t1 VALUES(9, 10);
  }
} {1 {database table is locked: t1}}
do_test notify1-9.4 {
  catchsql { SELECT * FROM t2 } db3
} {1 {database table is locked}}
do_test notify1-9.5 {
  execsql  { COMMIT } db2
  execsql { SELECT * FROM t2 } db3
} {}
do_test notify1-9.6 {
  execsql  { COMMIT }
} {}

do_test notify1-9.7 {
  execsql {
    BEGIN;
    SELECT * FROM t1;
  } db2
} {1 2 3 4 5 6 7 8}
do_test notify1-9.8 {
  execsql { SELECT * FROM t1 } db3
} {1 2 3 4 5 6 7 8}
do_test notify1-9.9 {
  catchsql { 
    BEGIN;
    INSERT INTO t1 VALUES(9, 10);
  }
} {1 {database table is locked: t1}}
do_test notify1-9.10 {
  catchsql { SELECT * FROM t2 } db3
} {1 {database table is locked}}
do_test notify1-9.11 {
  execsql  { COMMIT }
  execsql { SELECT * FROM t2 } db3
} {}
do_test notify1-9.12 {
  execsql  { COMMIT } db2
} {}

db close
db2 close
db3 close
sqlite3_enable_shared_cache $::enable_shared_cache
finish_test