/ Hex Artifact Content
Login

Artifact 7d3b6b4bf0b12f8007121b1e6ef714bc99101fb3b48e46371df1db868eebc131:


0000: 23 20 32 30 31 38 20 4a 75 6c 79 20 34 0a 23 0a  # 2018 July 4.#.
0010: 23 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  # The author dis
0020: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
0030: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
0040: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
0050: 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e 6f 74  of.# a legal not
0060: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
0070: 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20 20 20  lessing:.#.#    
0080: 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
0090: 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 23 20  and not evil..# 
00a0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
00b0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
00c0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
00d0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23 20 20  give others..#  
00e0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
00f0: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
0100: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
0110: 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a 2a 2a  ou give..#.#****
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 0a 23 0a 0a 73 65 74 20 74 65 73 74 64  ***.#..set testd
0170: 69 72 20 5b 66 69 6c 65 20 64 69 72 6e 61 6d 65  ir [file dirname
0180: 20 24 61 72 67 76 30 5d 0a 73 6f 75 72 63 65 20   $argv0].source 
0190: 24 74 65 73 74 64 69 72 2f 74 65 73 74 65 72 2e  $testdir/tester.
01a0: 74 63 6c 0a 73 6f 75 72 63 65 20 24 74 65 73 74  tcl.source $test
01b0: 64 69 72 2f 6c 6f 63 6b 5f 63 6f 6d 6d 6f 6e 2e  dir/lock_common.
01c0: 74 63 6c 0a 73 6f 75 72 63 65 20 24 74 65 73 74  tcl.source $test
01d0: 64 69 72 2f 77 61 6c 5f 63 6f 6d 6d 6f 6e 2e 74  dir/wal_common.t
01e0: 63 6c 0a 69 66 63 61 70 61 62 6c 65 20 21 77 61  cl.ifcapable !wa
01f0: 6c 20 7b 66 69 6e 69 73 68 5f 74 65 73 74 20 3b  l {finish_test ;
0200: 20 72 65 74 75 72 6e 20 7d 0a 0a 73 65 74 20 74   return }..set t
0210: 65 73 74 70 72 65 66 69 78 20 77 61 6c 70 72 6f  estprefix walpro
0220: 74 6f 63 6f 6c 32 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  tocol2..#-------
0230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0260: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0270: 2d 2d 0a 23 20 57 68 65 6e 20 72 65 63 6f 76 65  --.# When recove
0280: 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ring the content
0290: 73 20 6f 66 20 61 20 57 41 4c 20 66 69 6c 65 2c  s of a WAL file,
02a0: 20 61 20 70 72 6f 63 65 73 73 20 6f 62 74 61 69   a process obtai
02b0: 6e 73 20 74 68 65 20 57 52 49 54 45 52 0a 23 20  ns the WRITER.# 
02c0: 6c 6f 63 6b 2c 20 74 68 65 6e 20 6c 6f 63 6b 73  lock, then locks
02d0: 20 61 6c 6c 20 6f 74 68 65 72 20 62 79 74 65 73   all other bytes
02e0: 20 62 65 66 6f 72 65 20 63 6f 6d 6d 65 6e 63 69   before commenci
02f0: 6e 67 20 72 65 63 6f 76 65 72 79 2e 20 49 66 20  ng recovery. If 
0300: 69 74 20 66 61 69 6c 73 0a 23 20 74 6f 20 6c 6f  it fails.# to lo
0310: 63 6b 20 61 6c 6c 20 6f 74 68 65 72 20 62 79 74  ck all other byt
0320: 65 73 20 28 62 65 63 61 75 73 65 20 73 6f 6d 65  es (because some
0330: 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 69   other process i
0340: 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64  s holding a read
0350: 0a 23 20 6c 6f 63 6b 29 20 69 74 20 73 68 6f 75  .# lock) it shou
0360: 6c 64 20 72 65 74 72 79 20 75 70 20 74 6f 20 31  ld retry up to 1
0370: 30 30 20 74 69 6d 65 73 2e 20 54 68 65 6e 20 72  00 times. Then r
0380: 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 52 4f  eturn SQLITE_PRO
0390: 54 4f 43 4f 4c 20 74 6f 20 74 68 65 20 0a 23 20  TOCOL to the .# 
03a0: 63 61 6c 6c 65 72 2e 20 54 65 73 74 20 74 68 69  caller. Test thi
03b0: 73 20 28 74 65 73 74 20 63 61 73 65 20 31 2e 33  s (test case 1.3
03c0: 29 2e 0a 23 0a 23 20 41 6c 73 6f 20 74 65 73 74  )..#.# Also test
03d0: 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 68   the effect of h
03e0: 69 74 74 69 6e 67 20 61 6e 20 53 51 4c 49 54 45  itting an SQLITE
03f0: 5f 42 55 53 59 20 77 68 69 6c 65 20 61 74 74 65  _BUSY while atte
0400: 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e  mpting to obtain
0410: 0a 23 20 74 68 65 20 57 52 49 54 45 52 20 6c 6f  .# the WRITER lo
0420: 63 6b 20 28 73 68 6f 75 6c 64 20 62 65 20 74 68  ck (should be th
0430: 65 20 73 61 6d 65 29 2e 20 54 65 73 74 20 63 61  e same). Test ca
0440: 73 65 20 31 2e 34 2e 0a 23 20 0a 64 6f 5f 65 78  se 1.4..# .do_ex
0450: 65 63 73 71 6c 5f 74 65 73 74 20 31 2e 30 20 7b  ecsql_test 1.0 {
0460: 0a 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61  .  PRAGMA journa
0470: 6c 5f 6d 6f 64 65 20 3d 20 77 61 6c 3b 0a 20 20  l_mode = wal;.  
0480: 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28 79  CREATE TABLE x(y
0490: 29 3b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  );.  INSERT INTO
04a0: 20 78 20 56 41 4c 55 45 53 28 27 7a 27 29 3b 0a   x VALUES('z');.
04b0: 7d 20 7b 77 61 6c 7d 0a 0a 64 62 20 63 6c 6f 73  } {wal}..db clos
04c0: 65 0a 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c  e..proc lock_cal
04d0: 6c 62 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69  lback {method fi
04e0: 6c 65 6e 61 6d 65 20 68 61 6e 64 6c 65 20 6c 6f  lename handle lo
04f0: 63 6b 7d 20 7b 0a 20 20 23 20 70 75 74 73 20 22  ck} {.  # puts "
0500: 24 6d 65 74 68 6f 64 20 24 66 69 6c 65 6e 61 6d  $method $filenam
0510: 65 20 24 68 61 6e 64 6c 65 20 24 6c 6f 63 6b 22  e $handle $lock"
0520: 0a 7d 0a 74 65 73 74 76 66 73 20 54 0a 54 20 66  .}.testvfs T.T f
0530: 69 6c 74 65 72 20 78 53 68 6d 4c 6f 63 6b 20 0a  ilter xShmLock .
0540: 54 20 73 63 72 69 70 74 20 6c 6f 63 6b 5f 63 61  T script lock_ca
0550: 6c 6c 62 61 63 6b 0a 0a 73 71 6c 69 74 65 33 20  llback..sqlite3 
0560: 64 62 20 20 74 65 73 74 2e 64 62 20 2d 76 66 73  db  test.db -vfs
0570: 20 54 0a 73 71 6c 69 74 65 33 20 64 62 32 20 74   T.sqlite3 db2 t
0580: 65 73 74 2e 64 62 20 2d 76 66 73 20 54 0a 0a 64  est.db -vfs T..d
0590: 6f 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20 32  o_execsql_test 2
05a0: 2e 30 20 7b 0a 20 20 53 45 4c 45 43 54 20 2a 20  .0 {.  SELECT * 
05b0: 46 52 4f 4d 20 78 3b 0a 7d 20 7b 7a 7d 0a 64 6f  FROM x;.} {z}.do
05c0: 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20 2d 64  _execsql_test -d
05d0: 62 20 64 62 32 20 32 2e 31 20 7b 0a 20 20 53 45  b db2 2.1 {.  SE
05e0: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 78 3b 0a 7d  LECT * FROM x;.}
05f0: 20 7b 7a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d   {z}..#---------
0600: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0610: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0620: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0630: 2d 2d 2d 2d 2d 2d 0a 23 20 41 74 74 65 6d 70 74  ------.# Attempt
0640: 20 61 20 22 42 45 47 49 4e 20 45 58 43 4c 55 53   a "BEGIN EXCLUS
0650: 49 56 45 22 20 75 73 69 6e 67 20 63 6f 6e 6e 65  IVE" using conne
0660: 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 5b 64 62  ction handle [db
0670: 5d 2e 20 54 68 69 73 0a 23 20 63 61 75 73 65 73  ]. This.# causes
0680: 20 53 51 4c 69 74 65 20 74 6f 20 6f 70 65 6e 20   SQLite to open 
0690: 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  a read transacti
06a0: 6f 6e 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65  on, then a write
06b0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 23 20   transaction..# 
06c0: 52 69 67 20 74 68 65 20 78 53 68 6d 4c 6f 63 6b  Rig the xShmLock
06d0: 28 29 20 63 61 6c 6c 62 61 63 6b 20 73 6f 20 74  () callback so t
06e0: 68 61 74 20 6a 75 73 74 20 62 65 66 6f 72 65 20  hat just before 
06f0: 74 68 65 20 45 58 43 4c 55 53 49 56 45 20 6c 6f  the EXCLUSIVE lo
0700: 63 6b 0a 23 20 66 6f 72 20 74 68 65 20 77 72 69  ck.# for the wri
0710: 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te transaction i
0720: 73 20 74 61 6b 65 6e 2c 20 63 6f 6e 6e 65 63 74  s taken, connect
0730: 69 6f 6e 20 5b 64 62 32 5d 20 6a 75 6d 70 73 20  ion [db2] jumps 
0740: 69 6e 20 61 6e 64 0a 23 20 6d 6f 64 69 66 69 65  in and.# modifie
0750: 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  s the database. 
0760: 54 68 69 73 20 63 61 75 73 65 73 20 74 68 65 20  This causes the 
0770: 22 42 45 47 49 4e 20 45 58 43 4c 55 53 49 56 45  "BEGIN EXCLUSIVE
0780: 22 20 74 6f 20 74 68 72 6f 77 0a 23 20 61 6e 20  " to throw.# an 
0790: 53 51 4c 49 54 45 5f 42 55 53 59 5f 53 4e 41 50  SQLITE_BUSY_SNAP
07a0: 53 48 4f 54 20 65 72 72 6f 72 2e 0a 23 0a 70 72  SHOT error..#.pr
07b0: 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b  oc lock_callback
07c0: 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 6e 61 6d   {method filenam
07d0: 65 20 68 61 6e 64 6c 65 20 6c 6f 63 6b 7d 20 7b  e handle lock} {
07e0: 0a 20 20 69 66 20 7b 24 6c 6f 63 6b 3d 3d 22 30  .  if {$lock=="0
07f0: 20 31 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76   1 lock exclusiv
0800: 65 22 7d 20 7b 0a 20 20 20 20 70 72 6f 63 20 6c  e"} {.    proc l
0810: 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65  ock_callback {me
0820: 74 68 6f 64 20 66 69 6c 65 6e 61 6d 65 20 68 61  thod filename ha
0830: 6e 64 6c 65 20 6c 6f 63 6b 7d 20 7b 7d 0a 20 20  ndle lock} {}.  
0840: 20 20 64 62 32 20 65 76 61 6c 20 7b 20 49 4e 53    db2 eval { INS
0850: 45 52 54 20 49 4e 54 4f 20 78 20 56 41 4c 55 45  ERT INTO x VALUE
0860: 53 28 27 79 27 29 20 7d 0a 20 20 7d 0a 7d 0a 64  S('y') }.  }.}.d
0870: 6f 5f 63 61 74 63 68 73 71 6c 5f 74 65 73 74 20  o_catchsql_test 
0880: 32 2e 32 20 7b 0a 20 20 42 45 47 49 4e 20 45 58  2.2 {.  BEGIN EX
0890: 43 4c 55 53 49 56 45 3b 0a 7d 20 7b 31 20 7b 64  CLUSIVE;.} {1 {d
08a0: 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65  atabase is locke
08b0: 64 7d 7d 0a 64 6f 5f 74 65 73 74 20 32 2e 33 20  d}}.do_test 2.3 
08c0: 7b 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74 65  {.  sqlite3_exte
08d0: 6e 64 65 64 5f 65 72 72 63 6f 64 65 20 64 62 0a  nded_errcode db.
08e0: 7d 20 7b 53 51 4c 49 54 45 5f 42 55 53 59 7d 0a  } {SQLITE_BUSY}.
08f0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
0900: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0910: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0920: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0930: 2d 0a 23 20 53 61 6d 65 20 61 67 61 69 6e 2c 20  -.# Same again, 
0940: 62 75 74 20 77 69 74 68 20 61 20 62 75 73 79 2d  but with a busy-
0950: 68 61 6e 64 6c 65 72 2e 20 54 68 69 73 20 74 69  handler. This ti
0960: 6d 65 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  me, following th
0970: 65 0a 23 20 53 51 4c 49 54 45 5f 42 55 53 59 5f  e.# SQLITE_BUSY_
0980: 53 4e 41 50 53 48 4f 54 20 65 72 72 6f 72 20 74  SNAPSHOT error t
0990: 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20  he busy-handler 
09a0: 69 73 20 69 6e 76 6f 6b 65 64 20 61 6e 64 20 74  is invoked and t
09b0: 68 65 6e 20 74 68 65 20 0a 23 20 77 68 6f 6c 65  hen the .# whole
09c0: 20 74 68 69 6e 67 20 72 65 74 72 69 65 64 20 66   thing retried f
09d0: 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
09e0: 67 2e 20 54 68 69 73 20 74 69 6d 65 20 69 74 20  g. This time it 
09f0: 73 75 63 63 65 65 64 73 2e 0a 23 0a 70 72 6f 63  succeeds..#.proc
0a00: 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 20 7b   lock_callback {
0a10: 6d 65 74 68 6f 64 20 66 69 6c 65 6e 61 6d 65 20  method filename 
0a20: 68 61 6e 64 6c 65 20 6c 6f 63 6b 7d 20 7b 0a 20  handle lock} {. 
0a30: 20 69 66 20 7b 24 6c 6f 63 6b 3d 3d 22 30 20 31   if {$lock=="0 1
0a40: 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76 65 22   lock exclusive"
0a50: 7d 20 7b 0a 20 20 20 20 70 72 6f 63 20 6c 6f 63  } {.    proc loc
0a60: 6b 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65 74 68  k_callback {meth
0a70: 6f 64 20 66 69 6c 65 6e 61 6d 65 20 68 61 6e 64  od filename hand
0a80: 6c 65 20 6c 6f 63 6b 7d 20 7b 7d 0a 20 20 20 20  le lock} {}.    
0a90: 64 62 32 20 65 76 61 6c 20 7b 20 49 4e 53 45 52  db2 eval { INSER
0aa0: 54 20 49 4e 54 4f 20 78 20 56 41 4c 55 45 53 28  T INTO x VALUES(
0ab0: 27 78 27 29 20 7d 0a 20 20 7d 0a 7d 0a 64 62 20  'x') }.  }.}.db 
0ac0: 74 69 6d 65 6f 75 74 20 31 30 0a 64 6f 5f 63 61  timeout 10.do_ca
0ad0: 74 63 68 73 71 6c 5f 74 65 73 74 20 32 2e 34 20  tchsql_test 2.4 
0ae0: 7b 0a 20 20 42 45 47 49 4e 20 45 58 43 4c 55 53  {.  BEGIN EXCLUS
0af0: 49 56 45 3b 0a 7d 20 7b 30 20 7b 7d 7d 0a 64 6f  IVE;.} {0 {}}.do
0b00: 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20 32 2e  _execsql_test 2.
0b10: 35 20 7b 0a 20 20 53 45 4c 45 43 54 20 2a 20 46  5 {.  SELECT * F
0b20: 52 4f 4d 20 78 3b 0a 20 20 43 4f 4d 4d 49 54 3b  ROM x;.  COMMIT;
0b30: 0a 7d 20 7b 7a 20 79 20 78 7d 0a 0a 66 69 6e 69  .} {z y x}..fini
0b40: 73 68 5f 74 65 73 74 0a                          sh_test.