/ Hex Artifact Content
Login

Artifact d2ae7e66f973bd6b58ce49e546b2c00f44fe0485:


0000: 23 20 32 30 31 30 20 41 70 72 69 6c 20 31 33 0a  # 2010 April 13.
0010: 23 0a 23 20 54 68 65 20 61 75 74 68 6f 72 20 64  #.# The author d
0020: 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
0030: 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
0040: 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
0050: 65 20 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e  e of.# a legal n
0060: 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
0070: 20 62 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20   blessing:.#.#  
0080: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
0090: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
00a0: 23 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  #    May you fin
00b0: 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
00c0: 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
00d0: 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23  orgive others..#
00e0: 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
00f0: 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
0100: 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
0110: 20 79 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a   you 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 2a 2a 0a 23 20 54 68 69 73 20 66 69 6c  *****.# This fil
0170: 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 72 65 67  e implements reg
0180: 72 65 73 73 69 6f 6e 20 74 65 73 74 73 20 66 6f  ression tests fo
0190: 72 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  r SQLite library
01a0: 2e 20 20 54 68 65 0a 23 20 66 6f 63 75 73 20 6f  .  The.# focus o
01b0: 66 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74  f this file is t
01c0: 65 73 74 69 6e 67 20 74 68 65 20 6f 70 65 72 61  esting the opera
01d0: 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72  tion of the libr
01e0: 61 72 79 20 69 6e 0a 23 20 22 50 52 41 47 4d 41  ary in.# "PRAGMA
01f0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 57 41   journal_mode=WA
0200: 4c 22 20 6d 6f 64 65 2e 0a 23 0a 0a 73 65 74 20  L" mode..#..set 
0210: 74 65 73 74 64 69 72 20 5b 66 69 6c 65 20 64 69  testdir [file di
0220: 72 6e 61 6d 65 20 24 61 72 67 76 30 5d 0a 73 6f  rname $argv0].so
0230: 75 72 63 65 20 24 74 65 73 74 64 69 72 2f 74 65  urce $testdir/te
0240: 73 74 65 72 2e 74 63 6c 0a 73 6f 75 72 63 65 20  ster.tcl.source 
0250: 24 74 65 73 74 64 69 72 2f 6c 6f 63 6b 5f 63 6f  $testdir/lock_co
0260: 6d 6d 6f 6e 2e 74 63 6c 0a 73 6f 75 72 63 65 20  mmon.tcl.source 
0270: 24 74 65 73 74 64 69 72 2f 77 61 6c 5f 63 6f 6d  $testdir/wal_com
0280: 6d 6f 6e 2e 74 63 6c 0a 73 6f 75 72 63 65 20 24  mon.tcl.source $
0290: 74 65 73 74 64 69 72 2f 6d 61 6c 6c 6f 63 5f 63  testdir/malloc_c
02a0: 6f 6d 6d 6f 6e 2e 74 63 6c 0a 69 66 63 61 70 61  ommon.tcl.ifcapa
02b0: 62 6c 65 20 21 77 61 6c 20 7b 66 69 6e 69 73 68  ble !wal {finish
02c0: 5f 74 65 73 74 20 3b 20 72 65 74 75 72 6e 20 7d  _test ; return }
02d0: 0a 0a 73 65 74 20 61 5f 73 74 72 69 6e 67 5f 63  ..set a_string_c
02e0: 6f 75 6e 74 65 72 20 31 0a 70 72 6f 63 20 61 5f  ounter 1.proc a_
02f0: 73 74 72 69 6e 67 20 7b 6e 7d 20 7b 0a 20 20 67  string {n} {.  g
0300: 6c 6f 62 61 6c 20 61 5f 73 74 72 69 6e 67 5f 63  lobal a_string_c
0310: 6f 75 6e 74 65 72 0a 20 20 69 6e 63 72 20 61 5f  ounter.  incr a_
0320: 73 74 72 69 6e 67 5f 63 6f 75 6e 74 65 72 0a 20  string_counter. 
0330: 20 73 74 72 69 6e 67 20 72 61 6e 67 65 20 5b 73   string range [s
0340: 74 72 69 6e 67 20 72 65 70 65 61 74 20 22 24 7b  tring repeat "${
0350: 61 5f 73 74 72 69 6e 67 5f 63 6f 75 6e 74 65 72  a_string_counter
0360: 7d 2e 22 20 24 6e 5d 20 31 20 24 6e 0a 7d 0a 64  }." $n] 1 $n.}.d
0370: 62 20 66 75 6e 63 20 61 5f 73 74 72 69 6e 67 20  b func a_string 
0380: 61 5f 73 74 72 69 6e 67 0a 0a 23 2d 2d 2d 2d 2d  a_string..#-----
0390: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
03d0: 2d 2d 2d 2d 0a 23 20 57 68 65 6e 20 61 20 72 6f  ----.# When a ro
03e0: 6c 6c 62 61 63 6b 20 6f 72 20 73 61 76 65 70 6f  llback or savepo
03f0: 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63  int rollback occ
0400: 75 72 73 2c 20 74 68 65 20 63 6c 69 65 6e 74 20  urs, the client 
0410: 6d 61 79 20 72 65 6d 6f 76 65 0a 23 20 65 6c 65  may remove.# ele
0420: 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6e 65 20 6f  ments from one o
0430: 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  f the hash table
0440: 73 20 69 6e 20 74 68 65 20 77 61 6c 2d 69 6e 64  s in the wal-ind
0450: 65 78 2e 20 54 68 69 73 20 62 6c 6f 63 6b 0a 23  ex. This block.#
0460: 20 6f 66 20 74 65 73 74 20 63 61 73 65 73 20 74   of test cases t
0470: 65 73 74 73 20 74 68 61 74 20 6e 6f 74 68 69 6e  ests that nothin
0480: 67 20 61 70 70 65 61 72 73 20 74 6f 20 67 6f 20  g appears to go 
0490: 77 72 6f 6e 67 20 77 68 65 6e 20 74 68 69 73 20  wrong when this 
04a0: 69 73 0a 23 20 64 6f 6e 65 2e 0a 23 0a 64 6f 5f  is.# done..#.do_
04b0: 74 65 73 74 20 77 61 6c 33 2d 31 2e 30 20 7b 0a  test wal3-1.0 {.
04c0: 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20    execsql {.    
04d0: 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a  PRAGMA cache_siz
04e0: 65 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 50 52  e = 2000;.    PR
04f0: 41 47 4d 41 20 70 61 67 65 5f 73 69 7a 65 20 3d  AGMA page_size =
0500: 20 31 30 32 34 3b 0a 20 20 20 20 50 52 41 47 4d   1024;.    PRAGM
0510: 41 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20  A auto_vacuum = 
0520: 6f 66 66 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  off;.    PRAGMA 
0530: 73 79 6e 63 68 72 6f 6e 6f 75 73 20 3d 20 6e 6f  synchronous = no
0540: 72 6d 61 6c 3b 0a 20 20 20 20 50 52 41 47 4d 41  rmal;.    PRAGMA
0550: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20   journal_mode = 
0560: 57 41 4c 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  WAL;.    PRAGMA 
0570: 77 61 6c 5f 61 75 74 6f 63 68 65 63 6b 70 6f 69  wal_autocheckpoi
0580: 6e 74 20 3d 20 30 3b 0a 20 20 20 20 42 45 47 49  nt = 0;.    BEGI
0590: 4e 3b 0a 20 20 20 20 20 20 43 52 45 41 54 45 20  N;.      CREATE 
05a0: 54 41 42 4c 45 20 74 31 28 78 29 3b 0a 20 20 20  TABLE t1(x);.   
05b0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
05c0: 31 20 56 41 4c 55 45 53 28 20 61 5f 73 74 72 69  1 VALUES( a_stri
05d0: 6e 67 28 38 30 30 29 20 29 3b 20 20 20 20 20 20  ng(800) );      
05e0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
05f0: 20 20 31 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53    1 */.      INS
0600: 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45  ERT INTO t1 SELE
0610: 43 54 20 61 5f 73 74 72 69 6e 67 28 38 30 30 29  CT a_string(800)
0620: 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 20 20 20   FROM t1;       
0630: 20 20 20 20 20 20 2f 2a 20 20 20 20 32 20 2a 2f        /*    2 */
0640: 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  .      INSERT IN
0650: 54 4f 20 74 31 20 53 45 4c 45 43 54 20 61 5f 73  TO t1 SELECT a_s
0660: 74 72 69 6e 67 28 38 30 30 29 20 46 52 4f 4d 20  tring(800) FROM 
0670: 74 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  t1;             
0680: 2f 2a 20 20 20 20 34 20 2a 2f 0a 20 20 20 20 20  /*    4 */.     
0690: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
06a0: 53 45 4c 45 43 54 20 61 5f 73 74 72 69 6e 67 28  SELECT a_string(
06b0: 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20  800) FROM t1;   
06c0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20            /*    
06d0: 38 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52  8 */.      INSER
06e0: 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54  T INTO t1 SELECT
06f0: 20 61 5f 73 74 72 69 6e 67 28 38 30 30 29 20 46   a_string(800) F
0700: 52 4f 4d 20 74 31 3b 20 20 20 20 20 20 20 20 20  ROM t1;         
0710: 20 20 20 20 2f 2a 20 20 20 31 36 20 2a 2f 0a 20      /*   16 */. 
0720: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
0730: 20 74 31 20 53 45 4c 45 43 54 20 61 5f 73 74 72   t1 SELECT a_str
0740: 69 6e 67 28 38 30 30 29 20 46 52 4f 4d 20 74 31  ing(800) FROM t1
0750: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
0760: 20 20 20 33 32 20 2a 2f 0a 20 20 20 20 20 20 49     32 */.      I
0770: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53 45  NSERT INTO t1 SE
0780: 4c 45 43 54 20 61 5f 73 74 72 69 6e 67 28 38 30  LECT a_string(80
0790: 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20 20  0) FROM t1;     
07a0: 20 20 20 20 20 20 20 20 2f 2a 20 20 20 36 34 20          /*   64 
07b0: 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54 20  */.      INSERT 
07c0: 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 61  INTO t1 SELECT a
07d0: 5f 73 74 72 69 6e 67 28 38 30 30 29 20 46 52 4f  _string(800) FRO
07e0: 4d 20 74 31 3b 20 20 20 20 20 20 20 20 20 20 20  M t1;           
07f0: 20 20 2f 2a 20 20 31 32 38 2a 2f 0a 20 20 20 20    /*  128*/.    
0800: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
0810: 20 53 45 4c 45 43 54 20 61 5f 73 74 72 69 6e 67   SELECT a_string
0820: 28 38 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20  (800) FROM t1;  
0830: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 32             /*  2
0840: 35 36 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45  56 */.      INSE
0850: 52 54 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43  RT INTO t1 SELEC
0860: 54 20 61 5f 73 74 72 69 6e 67 28 38 30 30 29 20  T a_string(800) 
0870: 46 52 4f 4d 20 74 31 3b 20 20 20 20 20 20 20 20  FROM t1;        
0880: 20 20 20 20 20 2f 2a 20 20 35 31 32 20 2a 2f 0a       /*  512 */.
0890: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
08a0: 4f 20 74 31 20 53 45 4c 45 43 54 20 61 5f 73 74  O t1 SELECT a_st
08b0: 72 69 6e 67 28 38 30 30 29 20 46 52 4f 4d 20 74  ring(800) FROM t
08c0: 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  1;             /
08d0: 2a 20 31 30 32 34 20 2a 2f 0a 20 20 20 20 20 20  * 1024 */.      
08e0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 53  INSERT INTO t1 S
08f0: 45 4c 45 43 54 20 61 5f 73 74 72 69 6e 67 28 38  ELECT a_string(8
0900: 30 30 29 20 46 52 4f 4d 20 74 31 3b 20 20 20 20  00) FROM t1;    
0910: 20 20 20 20 20 20 20 20 20 2f 2a 20 32 30 34 38           /* 2048
0920: 20 2a 2f 0a 20 20 20 20 20 20 49 4e 53 45 52 54   */.      INSERT
0930: 20 49 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20   INTO t1 SELECT 
0940: 61 5f 73 74 72 69 6e 67 28 38 30 30 29 20 46 52  a_string(800) FR
0950: 4f 4d 20 74 31 20 4c 49 4d 49 54 20 31 39 37 30  OM t1 LIMIT 1970
0960: 3b 20 20 2f 2a 20 34 30 31 38 20 2a 2f 0a 20 20  ;  /* 4018 */.  
0970: 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 50 52    COMMIT;.    PR
0980: 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 20  AGMA cache_size 
0990: 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 77 61 6c 5f  = 10;.  }.  wal_
09a0: 66 72 61 6d 65 5f 63 6f 75 6e 74 20 74 65 73 74  frame_count test
09b0: 2e 64 62 2d 77 61 6c 20 31 30 32 34 0a 7d 20 34  .db-wal 1024.} 4
09c0: 30 35 36 0a 0a 66 6f 72 20 7b 73 65 74 20 69 20  056..for {set i 
09d0: 31 7d 20 7b 24 69 20 3c 20 35 30 7d 20 7b 69 6e  1} {$i < 50} {in
09e0: 63 72 20 69 7d 20 7b 0a 0a 20 20 64 6f 5f 74 65  cr i} {..  do_te
09f0: 73 74 20 77 61 6c 33 2d 31 2e 24 69 2e 31 20 7b  st wal3-1.$i.1 {
0a00: 0a 20 20 20 20 73 65 74 20 73 74 72 20 5b 61 5f  .    set str [a_
0a10: 73 74 72 69 6e 67 20 38 30 30 5d 0a 20 20 20 20  string 800].    
0a20: 65 78 65 63 73 71 6c 20 7b 20 55 50 44 41 54 45  execsql { UPDATE
0a30: 20 74 31 20 53 45 54 20 78 20 3d 20 24 73 74 72   t1 SET x = $str
0a40: 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d 20 24   WHERE rowid = $
0a50: 69 20 7d 0a 20 20 20 20 6c 61 70 70 65 6e 64 20  i }.    lappend 
0a60: 4c 20 5b 77 61 6c 5f 66 72 61 6d 65 5f 63 6f 75  L [wal_frame_cou
0a70: 6e 74 20 74 65 73 74 2e 64 62 2d 77 61 6c 20 31  nt test.db-wal 1
0a80: 30 32 34 5d 0a 20 20 20 20 65 78 65 63 73 71 6c  024].    execsql
0a90: 20 7b 0a 20 20 20 20 20 20 42 45 47 49 4e 3b 0a   {.      BEGIN;.
0aa0: 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20 49          INSERT I
0ab0: 4e 54 4f 20 74 31 20 53 45 4c 45 43 54 20 61 5f  NTO t1 SELECT a_
0ac0: 73 74 72 69 6e 67 28 38 30 30 29 20 46 52 4f 4d  string(800) FROM
0ad0: 20 74 31 20 4c 49 4d 49 54 20 31 30 30 3b 0a 20   t1 LIMIT 100;. 
0ae0: 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 3b 0a 20       ROLLBACK;. 
0af0: 20 20 20 20 20 50 52 41 47 4d 41 20 69 6e 74 65       PRAGMA inte
0b00: 67 72 69 74 79 5f 63 68 65 63 6b 3b 0a 20 20 20  grity_check;.   
0b10: 20 7d 0a 20 20 7d 20 7b 6f 6b 7d 0a 0a 20 20 23   }.  } {ok}..  #
0b20: 20 43 68 65 63 6b 20 74 68 61 74 20 65 76 65 72   Check that ever
0b30: 79 74 68 69 6e 67 20 6c 6f 6f 6b 73 20 4f 4b 20  ything looks OK 
0b40: 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 20 6f  from the point o
0b50: 66 20 76 69 65 77 20 6f 66 20 61 6e 20 0a 20 20  f view of an .  
0b60: 23 20 65 78 74 65 72 6e 61 6c 20 63 6f 6e 6e 65  # external conne
0b70: 63 74 69 6f 6e 2e 0a 20 20 23 0a 20 20 73 71 6c  ction..  #.  sql
0b80: 69 74 65 33 20 64 62 32 20 74 65 73 74 2e 64 62  ite3 db2 test.db
0b90: 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .  do_test wal3-
0ba0: 31 2e 24 69 2e 32 20 7b 0a 20 20 20 20 65 78 65  1.$i.2 {.    exe
0bb0: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f  csql { SELECT co
0bc0: 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 31 20 7d  unt(*) FROM t1 }
0bd0: 20 64 62 32 0a 20 20 7d 20 34 30 31 38 0a 20 20   db2.  } 4018.  
0be0: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 31 2e 24  do_test wal3-1.$
0bf0: 69 2e 33 20 7b 0a 20 20 20 20 65 78 65 63 73 71  i.3 {.    execsq
0c00: 6c 20 7b 20 53 45 4c 45 43 54 20 78 20 46 52 4f  l { SELECT x FRO
0c10: 4d 20 74 31 20 57 48 45 52 45 20 72 6f 77 69 64  M t1 WHERE rowid
0c20: 20 3d 20 24 69 20 7d 0a 20 20 7d 20 24 73 74 72   = $i }.  } $str
0c30: 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .  do_test wal3-
0c40: 31 2e 24 69 2e 34 20 7b 0a 20 20 20 20 65 78 65  1.$i.4 {.    exe
0c50: 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 69 6e  csql { PRAGMA in
0c60: 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 7d 20  tegrity_check } 
0c70: 64 62 32 0a 20 20 7d 20 7b 6f 6b 7d 0a 20 20 64  db2.  } {ok}.  d
0c80: 62 32 20 63 6c 6f 73 65 0a 20 20 0a 20 20 23 20  b2 close.  .  # 
0c90: 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 66  Check that the f
0ca0: 69 6c 65 2d 73 79 73 74 65 6d 20 69 6e 20 69 74  ile-system in it
0cb0: 73 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  s current state 
0cc0: 63 61 6e 20 62 65 20 72 65 63 6f 76 65 72 65 64  can be recovered
0cd0: 2e 0a 20 20 23 20 0a 20 20 66 69 6c 65 20 63 6f  ..  # .  file co
0ce0: 70 79 20 2d 66 6f 72 63 65 20 74 65 73 74 2e 64  py -force test.d
0cf0: 62 20 74 65 73 74 32 2e 64 62 0a 20 20 66 69 6c  b test2.db.  fil
0d00: 65 20 63 6f 70 79 20 2d 66 6f 72 63 65 20 74 65  e copy -force te
0d10: 73 74 2e 64 62 2d 77 61 6c 20 74 65 73 74 32 2e  st.db-wal test2.
0d20: 64 62 2d 77 61 6c 0a 20 20 66 69 6c 65 20 64 65  db-wal.  file de
0d30: 6c 65 74 65 20 2d 66 6f 72 63 65 20 74 65 73 74  lete -force test
0d40: 32 2e 64 62 2d 6a 6f 75 72 6e 61 6c 0a 20 20 73  2.db-journal.  s
0d50: 71 6c 69 74 65 33 20 64 62 32 20 74 65 73 74 32  qlite3 db2 test2
0d60: 2e 64 62 0a 20 20 64 6f 5f 74 65 73 74 20 77 61  .db.  do_test wa
0d70: 6c 33 2d 31 2e 24 69 2e 35 20 7b 0a 20 20 20 20  l3-1.$i.5 {.    
0d80: 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54  execsql { SELECT
0d90: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74   count(*) FROM t
0da0: 31 20 7d 20 64 62 32 0a 20 20 7d 20 34 30 31 38  1 } db2.  } 4018
0db0: 0a 20 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .  do_test wal3-
0dc0: 31 2e 24 69 2e 36 20 7b 0a 20 20 20 20 65 78 65  1.$i.6 {.    exe
0dd0: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 78 20  csql { SELECT x 
0de0: 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 72 6f  FROM t1 WHERE ro
0df0: 77 69 64 20 3d 20 24 69 20 7d 0a 20 20 7d 20 24  wid = $i }.  } $
0e00: 73 74 72 0a 20 20 64 6f 5f 74 65 73 74 20 77 61  str.  do_test wa
0e10: 6c 33 2d 31 2e 24 69 2e 37 20 7b 0a 20 20 20 20  l3-1.$i.7 {.    
0e20: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
0e30: 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b   integrity_check
0e40: 20 7d 20 64 62 32 0a 20 20 7d 20 7b 6f 6b 7d 0a   } db2.  } {ok}.
0e50: 20 20 64 62 32 20 63 6c 6f 73 65 0a 7d 0a 0a 64    db2 close.}..d
0e60: 6f 5f 6d 75 6c 74 69 63 6c 69 65 6e 74 5f 74 65  o_multiclient_te
0e70: 73 74 20 69 20 7b 0a 0a 20 20 73 65 74 20 74 65  st i {..  set te
0e80: 73 74 6e 61 6d 65 28 31 29 20 6d 75 6c 74 69 70  stname(1) multip
0e90: 72 6f 63 0a 20 20 73 65 74 20 74 65 73 74 6e 61  roc.  set testna
0ea0: 6d 65 28 32 29 20 73 69 6e 67 6c 65 70 72 6f 63  me(2) singleproc
0eb0: 0a 20 20 73 65 74 20 74 6e 20 24 74 65 73 74 6e  .  set tn $testn
0ec0: 61 6d 65 28 24 69 29 0a 0a 20 20 64 6f 5f 74 65  ame($i)..  do_te
0ed0: 73 74 20 77 61 6c 33 2d 32 2e 24 74 6e 2e 31 20  st wal3-2.$tn.1 
0ee0: 7b 0a 20 20 20 20 73 71 6c 31 20 7b 20 0a 20 20  {.    sql1 { .  
0ef0: 20 20 20 20 50 52 41 47 4d 41 20 70 61 67 65 5f      PRAGMA page_
0f00: 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20  size = 1024;.   
0f10: 20 20 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61     PRAGMA journa
0f20: 6c 5f 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20  l_mode = WAL;.  
0f30: 20 20 7d 0a 20 20 20 20 73 71 6c 31 20 7b 0a 20    }.    sql1 {. 
0f40: 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
0f50: 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 20 20 20  E t1(a, b);.    
0f60: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
0f70: 20 56 41 4c 55 45 53 28 31 2c 20 27 6f 6e 65 27   VALUES(1, 'one'
0f80: 29 3b 0a 20 20 20 20 20 20 42 45 47 49 4e 3b 0a  );.      BEGIN;.
0f90: 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a          SELECT *
0fa0: 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 7d 0a   FROM t1;.    }.
0fb0: 20 20 7d 20 7b 31 20 6f 6e 65 7d 0a 20 20 64 6f    } {1 one}.  do
0fc0: 5f 74 65 73 74 20 77 61 6c 33 2d 32 2e 24 74 6e  _test wal3-2.$tn
0fd0: 2e 32 20 7b 0a 20 20 20 20 73 71 6c 32 20 7b 0a  .2 {.    sql2 {.
0fe0: 20 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42        CREATE TAB
0ff0: 4c 45 20 74 32 28 61 2c 20 62 29 3b 0a 20 20 20  LE t2(a, b);.   
1000: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1010: 32 20 56 41 4c 55 45 53 28 32 2c 20 27 74 77 6f  2 VALUES(2, 'two
1020: 27 29 3b 0a 20 20 20 20 20 20 42 45 47 49 4e 3b  ');.      BEGIN;
1030: 0a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20  .        SELECT 
1040: 2a 20 46 52 4f 4d 20 74 32 3b 0a 20 20 20 20 7d  * FROM t2;.    }
1050: 0a 20 20 7d 20 7b 32 20 74 77 6f 7d 0a 20 20 64  .  } {2 two}.  d
1060: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 32 2e 24 74  o_test wal3-2.$t
1070: 6e 2e 33 20 7b 0a 20 20 20 20 73 71 6c 33 20 7b  n.3 {.    sql3 {
1080: 0a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 41  .      CREATE TA
1090: 42 4c 45 20 74 33 28 61 2c 20 62 29 3b 0a 20 20  BLE t3(a, b);.  
10a0: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
10b0: 74 33 20 56 41 4c 55 45 53 28 33 2c 20 27 74 68  t3 VALUES(3, 'th
10c0: 72 65 65 27 29 3b 0a 20 20 20 20 20 20 42 45 47  ree');.      BEG
10d0: 49 4e 3b 0a 20 20 20 20 20 20 20 20 53 45 4c 45  IN;.        SELE
10e0: 43 54 20 2a 20 46 52 4f 4d 20 74 33 3b 0a 20 20  CT * FROM t3;.  
10f0: 20 20 7d 0a 20 20 7d 20 7b 33 20 74 68 72 65 65    }.  } {3 three
1100: 7d 0a 0a 20 20 23 20 54 72 79 20 74 6f 20 63 68  }..  # Try to ch
1110: 65 63 6b 70 6f 69 6e 74 20 74 68 65 20 64 61 74  eckpoint the dat
1120: 61 62 61 73 65 20 75 73 69 6e 67 20 5b 64 62 5d  abase using [db]
1130: 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70  . It should be p
1140: 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 23 20 63  ossible to.  # c
1150: 68 65 63 6b 70 6f 69 6e 74 20 65 76 65 72 79 74  heckpoint everyt
1160: 68 69 6e 67 20 65 78 63 65 70 74 20 74 68 65 20  hing except the 
1170: 74 61 62 6c 65 20 61 64 64 65 64 20 62 79 20 5b  table added by [
1180: 64 62 33 5d 20 28 63 68 65 63 6b 70 6f 69 6e 74  db3] (checkpoint
1190: 69 6e 67 0a 20 20 23 20 74 68 65 73 65 20 66 72  ing.  # these fr
11a0: 61 6d 65 73 20 77 6f 75 6c 64 20 63 6c 6f 62 62  ames would clobb
11b0: 65 72 20 74 68 65 20 73 6e 61 70 73 68 6f 74 20  er the snapshot 
11c0: 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20  currently being 
11d0: 75 73 65 64 20 62 79 20 5b 64 62 32 5d 29 2e 0a  used by [db2])..
11e0: 20 20 23 0a 20 20 23 20 41 66 74 65 72 20 5b 64    #.  # After [d
11f0: 62 32 5d 20 68 61 73 20 63 6f 6d 6d 69 74 74 65  b2] has committe
1200: 64 2c 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20  d, a checkpoint 
1210: 63 61 6e 20 63 6f 70 79 20 74 68 65 20 65 6e 74  can copy the ent
1220: 69 72 65 20 6c 6f 67 20 74 6f 20 74 68 65 0a 20  ire log to the. 
1230: 20 23 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   # database file
1240: 2e 20 43 68 65 63 6b 70 6f 69 6e 74 69 6e 67 20  . Checkpointing 
1250: 61 66 74 65 72 20 5b 64 62 33 5d 20 68 61 73 20  after [db3] has 
1260: 63 6f 6d 6d 69 74 74 65 64 20 69 73 20 74 68 65  committed is the
1270: 72 65 66 6f 72 65 20 61 0a 20 20 23 20 6e 6f 2d  refore a.  # no-
1280: 6f 70 2c 20 61 73 20 74 68 65 20 65 6e 74 69 72  op, as the entir
1290: 65 20 6c 6f 67 20 68 61 73 20 61 6c 72 65 61 64  e log has alread
12a0: 79 20 62 65 65 6e 20 62 61 63 6b 66 69 6c 6c 65  y been backfille
12b0: 64 2e 0a 20 20 23 0a 20 20 64 6f 5f 74 65 73 74  d..  #.  do_test
12c0: 20 77 61 6c 33 2d 32 2e 24 74 6e 2e 34 20 7b 0a   wal3-2.$tn.4 {.
12d0: 20 20 20 20 73 71 6c 31 20 7b 0a 20 20 20 20 20      sql1 {.     
12e0: 20 43 4f 4d 4d 49 54 3b 0a 20 20 20 20 20 20 50   COMMIT;.      P
12f0: 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70  RAGMA wal_checkp
1300: 6f 69 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oint;.    }.    
1310: 66 69 6c 65 20 73 69 7a 65 20 74 65 73 74 2e 64  file size test.d
1320: 62 0a 20 20 7d 20 5b 65 78 70 72 20 24 41 55 54  b.  } [expr $AUT
1330: 4f 56 41 43 55 55 4d 20 3f 20 34 2a 31 30 32 34  OVACUUM ? 4*1024
1340: 20 3a 20 33 2a 31 30 32 34 5d 0a 20 20 64 6f 5f   : 3*1024].  do_
1350: 74 65 73 74 20 77 61 6c 33 2d 32 2e 24 74 6e 2e  test wal3-2.$tn.
1360: 35 20 7b 0a 20 20 20 20 73 71 6c 32 20 7b 0a 20  5 {.    sql2 {. 
1370: 20 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 20       COMMIT;.   
1380: 20 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68     PRAGMA wal_ch
1390: 65 63 6b 70 6f 69 6e 74 3b 0a 20 20 20 20 7d 0a  eckpoint;.    }.
13a0: 20 20 20 20 66 69 6c 65 20 73 69 7a 65 20 74 65      file size te
13b0: 73 74 2e 64 62 0a 20 20 7d 20 5b 65 78 70 72 20  st.db.  } [expr 
13c0: 24 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 35 2a  $AUTOVACUUM ? 5*
13d0: 31 30 32 34 20 3a 20 34 2a 31 30 32 34 5d 0a 20  1024 : 4*1024]. 
13e0: 20 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 32 2e   do_test wal3-2.
13f0: 24 74 6e 2e 36 20 7b 0a 20 20 20 20 73 71 6c 33  $tn.6 {.    sql3
1400: 20 7b 0a 20 20 20 20 20 20 43 4f 4d 4d 49 54 3b   {.      COMMIT;
1410: 0a 20 20 20 20 20 20 50 52 41 47 4d 41 20 77 61  .      PRAGMA wa
1420: 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 3b 0a 20 20  l_checkpoint;.  
1430: 20 20 7d 0a 20 20 20 20 66 69 6c 65 20 73 69 7a    }.    file siz
1440: 65 20 74 65 73 74 2e 64 62 0a 20 20 7d 20 5b 65  e test.db.  } [e
1450: 78 70 72 20 24 41 55 54 4f 56 41 43 55 55 4d 20  xpr $AUTOVACUUM 
1460: 3f 20 35 2a 31 30 32 34 20 3a 20 34 2a 31 30 32  ? 5*1024 : 4*102
1470: 34 5d 0a 7d 0a 63 61 74 63 68 20 7b 64 62 20 63  4].}.catch {db c
1480: 6c 6f 73 65 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d  lose}..#--------
1490: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14d0: 2d 0a 23 20 54 65 73 74 20 74 68 61 74 20 74 68  -.# Test that th
14e0: 61 74 20 66 6f 72 20 74 68 65 20 73 69 6d 70 6c  at for the simpl
14f0: 65 20 74 65 73 74 3a 0a 23 0a 23 20 20 20 43 52  e test:.#.#   CR
1500: 45 41 54 45 20 54 41 42 4c 45 20 78 28 79 29 3b  EATE TABLE x(y);
1510: 0a 23 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .#   INSERT INTO
1520: 20 78 20 56 41 4c 55 45 53 28 27 7a 27 29 3b 0a   x VALUES('z');.
1530: 23 20 20 20 50 52 41 47 4d 41 20 77 61 6c 5f 63  #   PRAGMA wal_c
1540: 68 65 63 6b 70 6f 69 6e 74 3b 0a 23 0a 23 20 69  heckpoint;.#.# i
1550: 6e 20 57 41 4c 20 6d 6f 64 65 20 74 68 65 20 78  n WAL mode the x
1560: 53 79 6e 63 20 6d 65 74 68 6f 64 20 69 73 20 69  Sync method is i
1570: 6e 76 6f 6b 65 64 20 61 73 20 65 78 70 65 63 74  nvoked as expect
1580: 65 64 20 66 6f 72 20 65 61 63 68 20 6f 66 0a 23  ed for each of.#
1590: 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 6f 66 66   synchronous=off
15a0: 2c 20 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 6e 6f  , synchronous=no
15b0: 72 6d 61 6c 20 61 6e 64 20 73 79 6e 63 68 72 6f  rmal and synchro
15c0: 6e 6f 75 73 3d 66 75 6c 6c 2e 0a 23 0a 66 6f 72  nous=full..#.for
15d0: 65 61 63 68 20 7b 74 6e 20 73 79 6e 63 6d 6f 64  each {tn syncmod
15e0: 65 20 73 79 6e 63 63 6f 75 6e 74 7d 20 7b 0a 20  e synccount} {. 
15f0: 20 31 20 6f 66 66 20 20 20 20 20 0a 20 20 20 20   1 off     .    
1600: 7b 7d 0a 20 20 32 20 6e 6f 72 6d 61 6c 20 20 0a  {}.  2 normal  .
1610: 20 20 20 20 7b 74 65 73 74 2e 64 62 2d 77 61 6c      {test.db-wal
1620: 20 6e 6f 72 6d 61 6c 20 74 65 73 74 2e 64 62 20   normal test.db 
1630: 6e 6f 72 6d 61 6c 7d 0a 20 20 33 20 66 75 6c 6c  normal}.  3 full
1640: 20 20 20 20 0a 20 20 20 20 7b 74 65 73 74 2e 64      .    {test.d
1650: 62 2d 77 61 6c 20 6e 6f 72 6d 61 6c 20 74 65 73  b-wal normal tes
1660: 74 2e 64 62 2d 77 61 6c 20 6e 6f 72 6d 61 6c 20  t.db-wal normal 
1670: 74 65 73 74 2e 64 62 2d 77 61 6c 20 6e 6f 72 6d  test.db-wal norm
1680: 61 6c 20 74 65 73 74 2e 64 62 20 6e 6f 72 6d 61  al test.db norma
1690: 6c 7d 0a 7d 20 7b 0a 0a 20 20 70 72 6f 63 20 73  l}.} {..  proc s
16a0: 79 6e 63 5f 63 6f 75 6e 74 65 72 20 7b 61 72 67  ync_counter {arg
16b0: 73 7d 20 7b 20 0a 20 20 20 20 66 6f 72 65 61 63  s} { .    foreac
16c0: 68 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 6e 61  h {method filena
16d0: 6d 65 20 69 64 20 66 6c 61 67 73 7d 20 24 61 72  me id flags} $ar
16e0: 67 73 20 62 72 65 61 6b 0a 20 20 20 20 6c 61 70  gs break.    lap
16f0: 70 65 6e 64 20 3a 3a 73 79 6e 63 73 20 5b 66 69  pend ::syncs [fi
1700: 6c 65 20 74 61 69 6c 20 24 66 69 6c 65 6e 61 6d  le tail $filenam
1710: 65 5d 20 24 66 6c 61 67 73 0a 20 20 7d 0a 20 20  e] $flags.  }.  
1720: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 33 2e 24  do_test wal3-3.$
1730: 74 6e 20 7b 0a 20 20 20 20 66 69 6c 65 20 64 65  tn {.    file de
1740: 6c 65 74 65 20 2d 66 6f 72 63 65 20 74 65 73 74  lete -force test
1750: 2e 64 62 20 74 65 73 74 2e 64 62 2d 77 61 6c 20  .db test.db-wal 
1760: 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 0a  test.db-journal.
1770: 20 20 0a 20 20 20 20 74 65 73 74 76 66 73 20 54    .    testvfs T
1780: 0a 20 20 20 20 54 20 66 69 6c 74 65 72 20 7b 7d  .    T filter {}
1790: 20 0a 20 20 20 20 54 20 73 63 72 69 70 74 20 73   .    T script s
17a0: 79 6e 63 5f 63 6f 75 6e 74 65 72 0a 20 20 20 20  ync_counter.    
17b0: 73 71 6c 69 74 65 33 20 64 62 20 74 65 73 74 2e  sqlite3 db test.
17c0: 64 62 20 2d 76 66 73 20 54 0a 20 20 0a 20 20 20  db -vfs T.  .   
17d0: 20 65 78 65 63 73 71 6c 20 22 50 52 41 47 4d 41   execsql "PRAGMA
17e0: 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 3d 20 24   synchronous = $
17f0: 73 79 6e 63 6d 6f 64 65 22 0a 20 20 20 20 65 78  syncmode".    ex
1800: 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 6a  ecsql { PRAGMA j
1810: 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57 41  ournal_mode = WA
1820: 4c 20 7d 0a 0a 20 20 20 20 73 65 74 20 3a 3a 73  L }..    set ::s
1830: 79 6e 63 73 20 5b 6c 69 73 74 5d 0a 20 20 20 20  yncs [list].    
1840: 54 20 66 69 6c 74 65 72 20 78 53 79 6e 63 0a 20  T filter xSync. 
1850: 20 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20     execsql {.   
1860: 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
1870: 78 28 79 29 3b 0a 20 20 20 20 20 20 49 4e 53 45  x(y);.      INSE
1880: 52 54 20 49 4e 54 4f 20 78 20 56 41 4c 55 45 53  RT INTO x VALUES
1890: 28 27 7a 27 29 3b 0a 20 20 20 20 20 20 50 52 41  ('z');.      PRA
18a0: 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69  GMA wal_checkpoi
18b0: 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 20  nt;.    }.    T 
18c0: 66 69 6c 74 65 72 20 7b 7d 0a 20 20 20 20 73 65  filter {}.    se
18d0: 74 20 3a 3a 73 79 6e 63 73 0a 20 20 7d 20 24 73  t ::syncs.  } $s
18e0: 79 6e 63 63 6f 75 6e 74 0a 0a 20 20 64 62 20 63  ynccount..  db c
18f0: 6c 6f 73 65 0a 20 20 54 20 64 65 6c 65 74 65 0a  lose.  T delete.
1900: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
1910: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1920: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1930: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1940: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1950: 57 68 65 6e 20 72 65 63 6f 76 65 72 69 6e 67 20  When recovering 
1960: 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
1970: 61 20 57 41 4c 20 66 69 6c 65 2c 20 61 20 70 72  a WAL file, a pr
1980: 6f 63 65 73 73 20 6f 62 74 61 69 6e 73 20 74 68  ocess obtains th
1990: 65 20 57 52 49 54 45 52 0a 23 20 6c 6f 63 6b 2c  e WRITER.# lock,
19a0: 20 74 68 65 6e 20 6c 6f 63 6b 73 20 61 6c 6c 20   then locks all 
19b0: 6f 74 68 65 72 20 62 79 74 65 73 20 62 65 66 6f  other bytes befo
19c0: 72 65 20 63 6f 6d 6d 65 6e 63 69 6e 67 20 72 65  re commencing re
19d0: 63 6f 76 65 72 79 2e 20 49 66 20 69 74 20 66 61  covery. If it fa
19e0: 69 6c 73 0a 23 20 74 6f 20 6c 6f 63 6b 20 61 6c  ils.# to lock al
19f0: 6c 20 6f 74 68 65 72 20 62 79 74 65 73 20 28 62  l other bytes (b
1a00: 65 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65  ecause some othe
1a10: 72 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c  r process is hol
1a20: 64 69 6e 67 20 61 20 72 65 61 64 0a 23 20 6c 6f  ding a read.# lo
1a30: 63 6b 29 20 69 74 20 73 68 6f 75 6c 64 20 72 65  ck) it should re
1a40: 74 72 79 20 75 70 20 74 6f 20 31 30 30 20 74 69  try up to 100 ti
1a50: 6d 65 73 2e 20 54 68 65 6e 20 72 65 74 75 72 6e  mes. Then return
1a60: 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c   SQLITE_PROTOCOL
1a70: 20 74 6f 20 74 68 65 20 0a 23 20 63 61 6c 6c 65   to the .# calle
1a80: 72 2e 20 54 65 73 74 20 74 68 69 73 20 28 74 65  r. Test this (te
1a90: 73 74 20 63 61 73 65 20 77 61 6c 33 2d 34 2e 33  st case wal3-4.3
1aa0: 29 2e 0a 23 0a 23 20 41 6c 73 6f 20 74 65 73 74  )..#.# Also test
1ab0: 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 68   the effect of h
1ac0: 69 74 74 69 6e 67 20 61 6e 20 53 51 4c 49 54 45  itting an SQLITE
1ad0: 5f 42 55 53 59 20 77 68 69 6c 65 20 61 74 74 65  _BUSY while atte
1ae0: 6d 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e  mpting to obtain
1af0: 0a 23 20 74 68 65 20 57 52 49 54 45 52 20 6c 6f  .# the WRITER lo
1b00: 63 6b 20 28 73 68 6f 75 6c 64 20 62 65 20 74 68  ck (should be th
1b10: 65 20 73 61 6d 65 29 2e 20 54 65 73 74 20 63 61  e same). Test ca
1b20: 73 65 20 77 61 6c 33 2d 34 2e 34 2e 0a 23 20 0a  se wal3-4.4..# .
1b30: 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61  proc lock_callba
1b40: 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 6e  ck {method filen
1b50: 61 6d 65 20 68 61 6e 64 6c 65 20 6c 6f 63 6b 7d  ame handle lock}
1b60: 20 7b 0a 20 20 6c 61 70 70 65 6e 64 20 3a 3a 6c   {.  lappend ::l
1b70: 6f 63 6b 73 20 24 6c 6f 63 6b 0a 7d 0a 64 6f 5f  ocks $lock.}.do_
1b80: 74 65 73 74 20 77 61 6c 33 2d 34 2e 31 20 7b 0a  test wal3-4.1 {.
1b90: 20 20 74 65 73 74 76 66 73 20 54 0a 20 20 54 20    testvfs T.  T 
1ba0: 66 69 6c 74 65 72 20 78 53 68 6d 4c 6f 63 6b 20  filter xShmLock 
1bb0: 0a 20 20 54 20 73 63 72 69 70 74 20 6c 6f 63 6b  .  T script lock
1bc0: 5f 63 61 6c 6c 62 61 63 6b 0a 20 20 73 65 74 20  _callback.  set 
1bd0: 3a 3a 6c 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a 20  ::locks [list]. 
1be0: 20 73 71 6c 69 74 65 33 20 64 62 20 74 65 73 74   sqlite3 db test
1bf0: 2e 64 62 20 2d 76 66 73 20 54 0a 20 20 65 78 65  .db -vfs T.  exe
1c00: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  csql { SELECT * 
1c10: 46 52 4f 4d 20 78 20 7d 0a 20 20 6c 72 61 6e 67  FROM x }.  lrang
1c20: 65 20 24 3a 3a 6c 6f 63 6b 73 20 30 20 33 0a 7d  e $::locks 0 3.}
1c30: 20 5b 6c 69 73 74 20 7b 30 20 31 20 6c 6f 63 6b   [list {0 1 lock
1c40: 20 65 78 63 6c 75 73 69 76 65 7d 20 7b 31 20 37   exclusive} {1 7
1c50: 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76 65 7d   lock exclusive}
1c60: 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20        \.        
1c70: 7b 31 20 37 20 75 6e 6c 6f 63 6b 20 65 78 63 6c  {1 7 unlock excl
1c80: 75 73 69 76 65 7d 20 7b 30 20 31 20 75 6e 6c 6f  usive} {0 1 unlo
1c90: 63 6b 20 65 78 63 6c 75 73 69 76 65 7d 20 20 5c  ck exclusive}  \
1ca0: 0a 5d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  .].do_test wal3-
1cb0: 34 2e 32 20 7b 0a 20 20 64 62 20 63 6c 6f 73 65  4.2 {.  db close
1cc0: 0a 20 20 73 65 74 20 3a 3a 6c 6f 63 6b 73 20 5b  .  set ::locks [
1cd0: 6c 69 73 74 5d 0a 20 20 73 71 6c 69 74 65 33 20  list].  sqlite3 
1ce0: 64 62 20 74 65 73 74 2e 64 62 20 2d 76 66 73 20  db test.db -vfs 
1cf0: 54 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45  T.  execsql { SE
1d00: 4c 45 43 54 20 2a 20 46 52 4f 4d 20 78 20 7d 0a  LECT * FROM x }.
1d10: 20 20 6c 72 61 6e 67 65 20 24 3a 3a 6c 6f 63 6b    lrange $::lock
1d20: 73 20 30 20 33 0a 7d 20 5b 6c 69 73 74 20 7b 30  s 0 3.} [list {0
1d30: 20 31 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76   1 lock exclusiv
1d40: 65 7d 20 7b 31 20 37 20 6c 6f 63 6b 20 65 78 63  e} {1 7 lock exc
1d50: 6c 75 73 69 76 65 7d 20 20 20 20 20 20 5c 0a 20  lusive}      \. 
1d60: 20 20 20 20 20 20 20 7b 31 20 37 20 75 6e 6c 6f         {1 7 unlo
1d70: 63 6b 20 65 78 63 6c 75 73 69 76 65 7d 20 7b 30  ck exclusive} {0
1d80: 20 31 20 75 6e 6c 6f 63 6b 20 65 78 63 6c 75 73   1 unlock exclus
1d90: 69 76 65 7d 20 20 5c 0a 5d 0a 70 72 6f 63 20 6c  ive}  \.].proc l
1da0: 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65  ock_callback {me
1db0: 74 68 6f 64 20 66 69 6c 65 6e 61 6d 65 20 68 61  thod filename ha
1dc0: 6e 64 6c 65 20 6c 6f 63 6b 7d 20 7b 0a 20 20 69  ndle lock} {.  i
1dd0: 66 20 7b 24 6c 6f 63 6b 20 3d 3d 20 22 31 20 37  f {$lock == "1 7
1de0: 20 6c 6f 63 6b 20 65 78 63 6c 75 73 69 76 65 22   lock exclusive"
1df0: 7d 20 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  } { return SQLIT
1e00: 45 5f 42 55 53 59 20 7d 0a 20 20 72 65 74 75 72  E_BUSY }.  retur
1e10: 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 7d 0a 70 75  n SQLITE_OK.}.pu
1e20: 74 73 20 22 20 20 57 61 72 6e 69 6e 67 3a 20 54  ts "  Warning: T
1e30: 68 69 73 20 6e 65 78 74 20 74 65 73 74 20 63 61  his next test ca
1e40: 73 65 20 63 61 75 73 65 73 20 53 51 4c 69 74 65  se causes SQLite
1e50: 20 74 6f 20 63 61 6c 6c 20 78 53 6c 65 65 70 28   to call xSleep(
1e60: 31 29 20 31 30 30 20 74 69 6d 65 73 2e 22 0a 70  1) 100 times.".p
1e70: 75 74 73 20 22 20 20 4e 6f 72 6d 61 6c 6c 79 20  uts "  Normally 
1e80: 74 68 69 73 20 65 71 75 61 74 65 73 20 74 6f 20  this equates to 
1e90: 61 20 31 30 30 6d 73 20 64 65 6c 61 79 2c 20 62  a 100ms delay, b
1ea0: 75 74 20 69 66 20 53 51 4c 69 74 65 20 69 73 20  ut if SQLite is 
1eb0: 62 75 69 6c 74 20 6f 6e 20 75 6e 69 78 22 0a 70  built on unix".p
1ec0: 75 74 73 20 22 20 20 77 69 74 68 6f 75 74 20 48  uts "  without H
1ed0: 41 56 45 5f 55 53 4c 45 45 50 20 64 65 66 69 6e  AVE_USLEEP defin
1ee0: 65 64 2c 20 69 74 20 6d 61 79 20 62 65 20 31 30  ed, it may be 10
1ef0: 30 20 73 65 63 6f 6e 64 73 2e 22 0a 64 6f 5f 74  0 seconds.".do_t
1f00: 65 73 74 20 77 61 6c 33 2d 34 2e 33 20 7b 0a 20  est wal3-4.3 {. 
1f10: 20 64 62 20 63 6c 6f 73 65 0a 20 20 73 65 74 20   db close.  set 
1f20: 3a 3a 6c 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a 20  ::locks [list]. 
1f30: 20 73 71 6c 69 74 65 33 20 64 62 20 74 65 73 74   sqlite3 db test
1f40: 2e 64 62 20 2d 76 66 73 20 54 0a 20 20 63 61 74  .db -vfs T.  cat
1f50: 63 68 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a  chsql { SELECT *
1f60: 20 46 52 4f 4d 20 78 20 7d 0a 7d 20 7b 31 20 7b   FROM x }.} {1 {
1f70: 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c  locking protocol
1f80: 7d 7d 0a 0a 70 75 74 73 20 22 20 20 57 61 72 6e  }}..puts "  Warn
1f90: 69 6e 67 3a 20 53 61 6d 65 20 61 67 61 69 6e 21  ing: Same again!
1fa0: 22 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c  ".proc lock_call
1fb0: 62 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c  back {method fil
1fc0: 65 6e 61 6d 65 20 68 61 6e 64 6c 65 20 6c 6f 63  ename handle loc
1fd0: 6b 7d 20 7b 0a 20 20 69 66 20 7b 24 6c 6f 63 6b  k} {.  if {$lock
1fe0: 20 3d 3d 20 22 30 20 31 20 6c 6f 63 6b 20 65 78   == "0 1 lock ex
1ff0: 63 6c 75 73 69 76 65 22 7d 20 7b 20 72 65 74 75  clusive"} { retu
2000: 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 7d  rn SQLITE_BUSY }
2010: 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2020: 5f 4f 4b 0a 7d 0a 64 6f 5f 74 65 73 74 20 77 61  _OK.}.do_test wa
2030: 6c 33 2d 34 2e 34 20 7b 0a 20 20 64 62 20 63 6c  l3-4.4 {.  db cl
2040: 6f 73 65 0a 20 20 73 65 74 20 3a 3a 6c 6f 63 6b  ose.  set ::lock
2050: 73 20 5b 6c 69 73 74 5d 0a 20 20 73 71 6c 69 74  s [list].  sqlit
2060: 65 33 20 64 62 20 74 65 73 74 2e 64 62 20 2d 76  e3 db test.db -v
2070: 66 73 20 54 0a 20 20 63 61 74 63 68 73 71 6c 20  fs T.  catchsql 
2080: 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20  { SELECT * FROM 
2090: 78 20 7d 0a 7d 20 7b 31 20 7b 6c 6f 63 6b 69 6e  x }.} {1 {lockin
20a0: 67 20 70 72 6f 74 6f 63 6f 6c 7d 7d 0a 64 62 20  g protocol}}.db 
20b0: 63 6c 6f 73 65 0a 54 20 64 65 6c 65 74 65 0a 0a  close.T delete..
20c0: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
20d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2100: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4f 6e  -----------.# On
2110: 6c 79 20 6f 6e 65 20 63 6c 69 65 6e 74 20 6d 61  ly one client ma
2120: 79 20 72 75 6e 20 72 65 63 6f 76 65 72 79 20 61  y run recovery a
2130: 74 20 61 20 74 69 6d 65 2e 20 54 65 73 74 20 74  t a time. Test t
2140: 68 69 73 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 23  his mechanism..#
2150: 0a 23 20 57 68 65 6e 20 63 6c 69 65 6e 74 2d 32  .# When client-2
2160: 20 74 72 69 65 73 20 74 6f 20 6f 70 65 6e 20 61   tries to open a
2170: 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f   read transactio
2180: 6e 20 77 68 69 6c 65 20 63 6c 69 65 6e 74 2d 31  n while client-1
2190: 20 69 73 20 0a 23 20 72 75 6e 6e 69 6e 67 20 72   is .# running r
21a0: 65 63 6f 76 65 72 79 2c 20 69 74 20 66 61 69 6c  ecovery, it fail
21b0: 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6c 6f  s to obtain a lo
21c0: 63 6b 20 6f 6e 20 61 6e 20 61 52 65 61 64 4d 61  ck on an aReadMa
21d0: 72 6b 5b 5d 20 73 6c 6f 74 0a 23 20 28 62 65 63  rk[] slot.# (bec
21e0: 61 75 73 65 20 74 68 65 79 20 61 72 65 20 61 6c  ause they are al
21f0: 6c 20 6c 6f 63 6b 65 64 20 62 79 20 72 65 63 6f  l locked by reco
2200: 76 65 72 79 29 2e 20 49 74 20 74 68 65 6e 20 74  very). It then t
2210: 72 69 65 73 20 74 6f 20 6f 62 74 61 69 6e 0a 23  ries to obtain.#
2220: 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f   a shared lock o
2230: 6e 20 74 68 65 20 52 45 43 4f 56 45 52 20 6c 6f  n the RECOVER lo
2240: 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
2250: 72 65 20 72 65 61 6c 6c 79 20 69 73 20 61 0a 23  re really is a.#
2260: 20 72 65 63 6f 76 65 72 79 20 72 75 6e 6e 69 6e   recovery runnin
2270: 67 20 6f 72 20 6e 6f 74 2e 0a 23 0a 23 20 54 68  g or not..#.# Th
2280: 69 73 20 62 6c 6f 63 6b 20 6f 66 20 74 65 73 74  is block of test
2290: 73 20 63 68 65 63 6b 73 20 74 68 65 20 65 66 66  s checks the eff
22a0: 65 63 74 20 6f 66 20 61 6e 20 53 51 4c 49 54 45  ect of an SQLITE
22b0: 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 54 45 5f  _BUSY or SQLITE_
22c0: 49 4f 45 52 52 0a 23 20 62 65 69 6e 67 20 72 65  IOERR.# being re
22d0: 74 75 72 6e 65 64 20 77 68 65 6e 20 63 6c 69 65  turned when clie
22e0: 6e 74 2d 32 20 61 74 74 65 6d 70 74 73 20 61 20  nt-2 attempts a 
22f0: 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74  shared lock on t
2300: 68 65 20 52 45 43 4f 56 45 52 20 62 79 74 65 2e  he RECOVER byte.
2310: 0a 23 0a 23 20 41 6e 20 53 51 4c 49 54 45 5f 42  .#.# An SQLITE_B
2320: 55 53 59 20 73 68 6f 75 6c 64 20 62 65 20 63 6f  USY should be co
2330: 6e 76 65 72 74 65 64 20 74 6f 20 61 6e 20 53 51  nverted to an SQ
2340: 4c 49 54 45 5f 42 55 53 59 5f 52 45 43 4f 56 45  LITE_BUSY_RECOVE
2350: 52 59 2e 20 41 6e 0a 23 20 53 51 4c 49 54 45 5f  RY. An.# SQLITE_
2360: 49 4f 45 52 52 20 73 68 6f 75 6c 64 20 62 65 20  IOERR should be 
2370: 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
2380: 63 61 6c 6c 65 72 2e 0a 23 0a 64 6f 5f 74 65 73  caller..#.do_tes
2390: 74 20 77 61 6c 33 2d 35 2e 31 20 7b 0a 20 20 66  t wal3-5.1 {.  f
23a0: 61 75 6c 74 73 69 6d 5f 64 65 6c 65 74 65 5f 61  aultsim_delete_a
23b0: 6e 64 5f 72 65 6f 70 65 6e 0a 20 20 65 78 65 63  nd_reopen.  exec
23c0: 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41  sql {.    PRAGMA
23d0: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20   journal_mode = 
23e0: 57 41 4c 3b 0a 20 20 20 20 43 52 45 41 54 45 20  WAL;.    CREATE 
23f0: 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a  TABLE t1(a, b);.
2400: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2410: 74 31 20 56 41 4c 55 45 53 28 31 2c 20 32 29 3b  t1 VALUES(1, 2);
2420: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2430: 20 74 31 20 56 41 4c 55 45 53 28 33 2c 20 34 29   t1 VALUES(3, 4)
2440: 3b 0a 20 20 7d 0a 20 20 66 61 75 6c 74 73 69 6d  ;.  }.  faultsim
2450: 5f 73 61 76 65 5f 61 6e 64 5f 63 6c 6f 73 65 0a  _save_and_close.
2460: 7d 20 7b 7d 0a 0a 74 65 73 74 76 66 73 20 54 20  } {}..testvfs T 
2470: 2d 64 65 66 61 75 6c 74 20 31 0a 54 20 73 63 72  -default 1.T scr
2480: 69 70 74 20 6d 65 74 68 6f 64 5f 63 61 6c 6c 62  ipt method_callb
2490: 61 63 6b 0a 0a 70 72 6f 63 20 6d 65 74 68 6f 64  ack..proc method
24a0: 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65 74 68 6f  _callback {metho
24b0: 64 20 61 72 67 73 7d 20 7b 0a 20 20 69 66 20 7b  d args} {.  if {
24c0: 24 6d 65 74 68 6f 64 20 3d 3d 20 22 78 53 68 6d  $method == "xShm
24d0: 42 61 72 72 69 65 72 22 7d 20 7b 0a 20 20 20 20  Barrier"} {.    
24e0: 69 6e 63 72 20 3a 3a 62 61 72 72 69 65 72 5f 63  incr ::barrier_c
24f0: 6f 75 6e 74 0a 20 20 20 20 69 66 20 7b 24 3a 3a  ount.    if {$::
2500: 62 61 72 72 69 65 72 5f 63 6f 75 6e 74 20 3d 3d  barrier_count ==
2510: 20 32 7d 20 7b 0a 20 20 20 20 20 20 23 20 54 68   2} {.      # Th
2520: 69 73 20 63 6f 64 65 20 69 73 20 65 78 65 63 75  is code is execu
2530: 74 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 78  ted within the x
2540: 53 68 6d 42 61 72 72 69 65 72 28 29 20 63 61 6c  ShmBarrier() cal
2550: 6c 62 61 63 6b 20 69 6e 76 6f 6b 65 64 0a 20 20  lback invoked.  
2560: 20 20 20 20 23 20 62 79 20 74 68 65 20 63 6c 69      # by the cli
2570: 65 6e 74 20 72 75 6e 6e 69 6e 67 20 72 65 63 6f  ent running reco
2580: 76 65 72 79 20 61 73 20 70 61 72 74 20 6f 66 20  very as part of 
2590: 77 72 69 74 69 6e 67 20 74 68 65 20 72 65 63 6f  writing the reco
25a0: 76 65 72 65 64 0a 20 20 20 20 20 20 23 20 77 61  vered.      # wa
25b0: 6c 2d 69 6e 64 65 78 20 68 65 61 64 65 72 2e 20  l-index header. 
25c0: 49 66 20 61 20 73 65 63 6f 6e 64 20 63 6c 69 65  If a second clie
25d0: 6e 74 20 61 74 74 65 6d 70 74 73 20 74 6f 20 61  nt attempts to a
25e0: 63 63 65 73 73 20 74 68 65 20 0a 20 20 20 20 20  ccess the .     
25f0: 20 23 20 64 61 74 61 62 61 73 65 20 6e 6f 77 2c   # database now,
2600: 20 69 74 20 72 65 61 64 73 20 61 20 63 6f 72 72   it reads a corr
2610: 75 70 74 20 28 70 61 72 74 69 61 6c 6c 79 20 77  upt (partially w
2620: 72 69 74 74 65 6e 29 20 77 61 6c 2d 69 6e 64 65  ritten) wal-inde
2630: 78 0a 20 20 20 20 20 20 23 20 68 65 61 64 65 72  x.      # header
2640: 2e 20 42 75 74 20 69 74 20 63 61 6e 6e 6f 74 20  . But it cannot 
2650: 65 76 65 6e 20 67 65 74 20 74 68 61 74 20 66 61  even get that fa
2660: 72 2c 20 61 73 20 74 68 65 20 66 69 72 73 74 20  r, as the first 
2670: 63 6c 69 65 6e 74 0a 20 20 20 20 20 20 23 20 69  client.      # i
2680: 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20  s still holding 
2690: 61 6c 6c 20 74 68 65 20 6c 6f 63 6b 73 20 28 72  all the locks (r
26a0: 65 63 6f 76 65 72 79 20 74 61 6b 65 73 20 61 6e  ecovery takes an
26b0: 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a   exclusive lock.
26c0: 20 20 20 20 20 20 23 20 6f 6e 20 2a 61 6c 6c 2a        # on *all*
26d0: 20 64 62 20 6c 6f 63 6b 73 2c 20 70 72 65 76 65   db locks, preve
26e0: 6e 74 69 6e 67 20 61 63 63 65 73 73 20 62 79 20  nting access by 
26f0: 61 6e 79 20 6f 74 68 65 72 20 63 6c 69 65 6e 74  any other client
2700: 29 2e 0a 20 20 20 20 20 20 23 0a 20 20 20 20 20  )..      #.     
2710: 20 23 20 49 66 20 67 6c 6f 62 61 6c 20 76 61 72   # If global var
2720: 69 61 62 6c 65 20 3a 3a 77 61 6c 33 5f 64 6f 5f  iable ::wal3_do_
2730: 6c 6f 63 6b 66 61 69 6c 75 72 65 20 69 73 20 6e  lockfailure is n
2740: 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 73 65  on-zero, then se
2750: 74 0a 20 20 20 20 20 20 23 20 74 68 69 6e 67 73  t.      # things
2760: 20 75 70 20 73 6f 20 74 68 61 74 20 61 6e 20 49   up so that an I
2770: 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  O error occurs w
2780: 69 74 68 69 6e 20 61 6e 20 78 53 68 6d 4c 6f 63  ithin an xShmLoc
2790: 6b 28 29 20 63 61 6c 6c 62 61 63 6b 0a 20 20 20  k() callback.   
27a0: 20 20 20 23 20 6d 61 64 65 20 62 79 20 74 68 65     # made by the
27b0: 20 73 65 63 6f 6e 64 20 63 6c 69 65 6e 74 20 28   second client (
27c0: 61 6b 61 20 5b 64 62 32 5d 29 2e 0a 20 20 20 20  aka [db2])..    
27d0: 20 20 23 0a 20 20 20 20 20 20 73 71 6c 69 74 65    #.      sqlite
27e0: 33 20 64 62 32 20 74 65 73 74 2e 64 62 0a 20 20  3 db2 test.db.  
27f0: 20 20 20 20 69 66 20 7b 20 24 3a 3a 77 61 6c 33      if { $::wal3
2800: 5f 64 6f 5f 6c 6f 63 6b 66 61 69 6c 75 72 65 20  _do_lockfailure 
2810: 7d 20 7b 20 54 20 66 69 6c 74 65 72 20 78 53 68  } { T filter xSh
2820: 6d 4c 6f 63 6b 20 7d 0a 20 20 20 20 20 20 73 65  mLock }.      se
2830: 74 20 3a 3a 74 65 73 74 72 63 20 5b 20 63 61 74  t ::testrc [ cat
2840: 63 68 20 7b 20 64 62 32 20 65 76 61 6c 20 22 53  ch { db2 eval "S
2850: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 22  ELECT * FROM t1"
2860: 20 7d 20 3a 3a 74 65 73 74 6d 73 67 20 5d 0a 20   } ::testmsg ]. 
2870: 20 20 20 20 20 54 20 66 69 6c 74 65 72 20 7b 7d       T filter {}
2880: 0a 20 20 20 20 20 20 64 62 32 20 63 6c 6f 73 65  .      db2 close
2890: 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
28a0: 20 7b 24 6d 65 74 68 6f 64 20 3d 3d 20 22 78 53   {$method == "xS
28b0: 68 6d 4c 6f 63 6b 22 7d 20 7b 0a 20 20 20 20 66  hmLock"} {.    f
28c0: 6f 72 65 61 63 68 20 7b 66 69 6c 65 20 68 61 6e  oreach {file han
28d0: 64 6c 65 20 73 70 65 63 7d 20 24 61 72 67 73 20  dle spec} $args 
28e0: 62 72 65 61 6b 0a 20 20 20 20 69 66 20 7b 20 24  break.    if { $
28f0: 73 70 65 63 20 3d 3d 20 22 32 20 31 20 6c 6f 63  spec == "2 1 loc
2900: 6b 20 73 68 61 72 65 64 22 20 7d 20 7b 0a 20 20  k shared" } {.  
2910: 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2920: 45 5f 49 4f 45 52 52 0a 20 20 20 20 7d 0a 20 20  E_IOERR.    }.  
2930: 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
2940: 54 45 5f 4f 4b 0a 7d 0a 0a 23 20 54 65 73 74 20  TE_OK.}..# Test 
2950: 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 49 54 45 5f  a normal SQLITE_
2960: 42 55 53 59 20 72 65 74 75 72 6e 2e 0a 23 0a 54  BUSY return..#.T
2970: 20 66 69 6c 74 65 72 20 78 53 68 6d 42 61 72 72   filter xShmBarr
2980: 69 65 72 0a 73 65 74 20 74 65 73 74 72 63 20 22  ier.set testrc "
2990: 22 0a 73 65 74 20 74 65 73 74 6d 73 67 20 22 22  ".set testmsg ""
29a0: 0a 73 65 74 20 62 61 72 72 69 65 72 5f 63 6f 75  .set barrier_cou
29b0: 6e 74 20 30 0a 73 65 74 20 77 61 6c 33 5f 64 6f  nt 0.set wal3_do
29c0: 5f 6c 6f 63 6b 66 61 69 6c 75 72 65 20 30 0a 64  _lockfailure 0.d
29d0: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 35 2e 32 20  o_test wal3-5.2 
29e0: 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65 73  {.  faultsim_res
29f0: 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e 0a  tore_and_reopen.
2a00: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
2a10: 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d 0a 7d  CT * FROM t1 }.}
2a20: 20 7b 31 20 32 20 33 20 34 7d 0a 64 6f 5f 74 65   {1 2 3 4}.do_te
2a30: 73 74 20 77 61 6c 33 2d 35 2e 33 20 7b 0a 20 20  st wal3-5.3 {.  
2a40: 6c 69 73 74 20 24 3a 3a 74 65 73 74 72 63 20 24  list $::testrc $
2a50: 3a 3a 74 65 73 74 6d 73 67 0a 7d 20 7b 31 20 7b  ::testmsg.} {1 {
2a60: 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b  database is lock
2a70: 65 64 7d 7d 0a 64 62 20 63 6c 6f 73 65 0a 0a 23  ed}}.db close..#
2a80: 20 54 65 73 74 20 61 6e 20 53 51 4c 49 54 45 5f   Test an SQLITE_
2a90: 49 4f 45 52 52 20 72 65 74 75 72 6e 2e 0a 23 0a  IOERR return..#.
2aa0: 54 20 66 69 6c 74 65 72 20 78 53 68 6d 42 61 72  T filter xShmBar
2ab0: 72 69 65 72 0a 73 65 74 20 62 61 72 72 69 65 72  rier.set barrier
2ac0: 5f 63 6f 75 6e 74 20 30 0a 73 65 74 20 77 61 6c  _count 0.set wal
2ad0: 33 5f 64 6f 5f 6c 6f 63 6b 66 61 69 6c 75 72 65  3_do_lockfailure
2ae0: 20 31 0a 73 65 74 20 74 65 73 74 72 63 20 22 22   1.set testrc ""
2af0: 0a 73 65 74 20 74 65 73 74 6d 73 67 20 22 22 0a  .set testmsg "".
2b00: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 35 2e 34  do_test wal3-5.4
2b10: 20 7b 0a 20 20 66 61 75 6c 74 73 69 6d 5f 72 65   {.  faultsim_re
2b20: 73 74 6f 72 65 5f 61 6e 64 5f 72 65 6f 70 65 6e  store_and_reopen
2b30: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c  .  execsql { SEL
2b40: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d 0a  ECT * FROM t1 }.
2b50: 7d 20 7b 31 20 32 20 33 20 34 7d 0a 64 6f 5f 74  } {1 2 3 4}.do_t
2b60: 65 73 74 20 77 61 6c 33 2d 35 2e 35 20 7b 0a 20  est wal3-5.5 {. 
2b70: 20 6c 69 73 74 20 24 3a 3a 74 65 73 74 72 63 20   list $::testrc 
2b80: 24 3a 3a 74 65 73 74 6d 73 67 0a 7d 20 7b 31 20  $::testmsg.} {1 
2b90: 7b 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 7d  {disk I/O error}
2ba0: 7d 0a 0a 64 62 20 63 6c 6f 73 65 0a 54 20 64 65  }..db close.T de
2bb0: 6c 65 74 65 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  lete..#---------
2bc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2bd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2be0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2bf0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c00: 0a 23 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20  .# When opening 
2c10: 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69  a read-transacti
2c20: 6f 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  on on a database
2c30: 2c 20 69 66 20 74 68 65 20 65 6e 74 69 72 65 20  , if the entire 
2c40: 6c 6f 67 20 68 61 73 0a 23 20 61 6c 72 65 61 64  log has.# alread
2c50: 79 20 62 65 65 6e 20 63 6f 70 69 65 64 20 74 6f  y been copied to
2c60: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
2c70: 6c 65 2c 20 74 68 65 20 72 65 61 64 65 72 20 67  le, the reader g
2c80: 72 61 62 73 20 61 20 73 70 65 63 69 61 6c 0a 23  rabs a special.#
2c90: 20 6b 69 6e 64 20 6f 66 20 72 65 61 64 20 6c 6f   kind of read lo
2ca0: 63 6b 20 28 6f 6e 20 61 52 65 61 64 4d 61 72 6b  ck (on aReadMark
2cb0: 5b 30 5d 29 2e 20 54 68 69 73 20 73 65 74 20 6f  [0]). This set o
2cc0: 66 20 74 65 73 74 20 63 61 73 65 73 20 74 65 73  f test cases tes
2cd0: 74 73 20 74 68 65 20 0a 23 20 6f 75 74 63 6f 6d  ts the .# outcom
2ce0: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
2cf0: 6e 67 3a 0a 23 0a 23 20 20 20 2b 20 54 68 65 20  ng:.#.#   + The 
2d00: 72 65 61 64 65 72 20 64 69 73 63 6f 76 65 72 69  reader discoveri
2d10: 6e 67 20 74 68 61 74 20 62 65 74 77 65 65 6e 20  ng that between 
2d20: 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20 69 74  the time when it
2d30: 20 64 65 74 65 72 6d 69 6e 65 64 20 0a 23 20 20   determined .#  
2d40: 20 20 20 74 68 61 74 20 74 68 65 20 6c 6f 67 20     that the log 
2d50: 68 61 64 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74  had been complet
2d60: 65 6c 79 20 62 61 63 6b 66 69 6c 6c 65 64 20 61  ely backfilled a
2d70: 6e 64 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f  nd the lock is o
2d80: 62 74 61 69 6e 65 64 0a 23 20 20 20 20 20 74 68  btained.#     th
2d90: 61 74 20 61 20 77 72 69 74 65 72 20 68 61 73 20  at a writer has 
2da0: 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c  written to the l
2db0: 6f 67 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  og. In this case
2dc0: 20 74 68 65 20 72 65 61 64 65 72 20 73 68 6f 75   the reader shou
2dd0: 6c 64 0a 23 20 20 20 20 20 61 63 71 75 69 72 65  ld.#     acquire
2de0: 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 65 61   a different rea
2df0: 64 2d 6c 6f 63 6b 20 28 6e 6f 74 20 61 52 65 61  d-lock (not aRea
2e00: 64 4d 61 72 6b 5b 30 5d 29 20 61 6e 64 20 72 65  dMark[0]) and re
2e10: 61 64 20 74 68 65 20 6e 65 77 0a 23 20 20 20 20  ad the new.#    
2e20: 20 73 6e 61 70 73 68 6f 74 2e 0a 23 0a 23 20 20   snapshot..#.#  
2e30: 20 2b 20 54 68 65 20 61 74 74 65 6d 70 74 20 74   + The attempt t
2e40: 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63  o obtain the loc
2e50: 6b 20 6f 6e 20 61 52 65 61 64 4d 61 72 6b 5b 30  k on aReadMark[0
2e60: 5d 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c  ] fails with SQL
2e70: 49 54 45 5f 42 55 53 59 2e 0a 23 20 20 20 20 20  ITE_BUSY..#     
2e80: 54 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20  This can happen 
2e90: 69 66 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 20  if a checkpoint 
2ea0: 69 73 20 6f 6e 67 6f 69 6e 67 2e 20 49 6e 20 74  is ongoing. In t
2eb0: 68 69 73 20 63 61 73 65 20 61 6c 73 6f 20 73 69  his case also si
2ec0: 6d 70 6c 79 0a 23 20 20 20 20 20 6f 62 74 61 69  mply.#     obtai
2ed0: 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 72 65  n a different re
2ee0: 61 64 2d 6c 6f 63 6b 2e 0a 23 0a 63 61 74 63 68  ad-lock..#.catch
2ef0: 20 7b 64 62 20 63 6c 6f 73 65 7d 0a 74 65 73 74   {db close}.test
2f00: 76 66 73 20 54 20 2d 64 65 66 61 75 6c 74 20 31  vfs T -default 1
2f10: 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e  .do_test wal3-6.
2f20: 31 2e 31 20 7b 0a 20 20 66 69 6c 65 20 64 65 6c  1.1 {.  file del
2f30: 65 74 65 20 2d 66 6f 72 63 65 20 74 65 73 74 2e  ete -force test.
2f40: 64 62 20 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e  db test.db-journ
2f50: 61 6c 20 74 65 73 74 2e 64 62 20 77 61 6c 0a 20  al test.db wal. 
2f60: 20 73 71 6c 69 74 65 33 20 64 62 20 74 65 73 74   sqlite3 db test
2f70: 2e 64 62 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  .db.  execsql { 
2f80: 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d  PRAGMA journal_m
2f90: 6f 64 65 20 3d 20 57 41 4c 20 7d 0a 20 20 65 78  ode = WAL }.  ex
2fa0: 65 63 73 71 6c 20 7b 0a 20 20 20 20 43 52 45 41  ecsql {.    CREA
2fb0: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62  TE TABLE t1(a, b
2fc0: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
2fd0: 54 4f 20 74 31 20 56 41 4c 55 45 53 28 27 6f 27  TO t1 VALUES('o'
2fe0: 2c 20 27 74 27 29 3b 0a 20 20 20 20 49 4e 53 45  , 't');.    INSE
2ff0: 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
3000: 53 28 27 74 27 2c 20 27 66 27 29 3b 0a 20 20 7d  S('t', 'f');.  }
3010: 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 77 61  .} {}.do_test wa
3020: 6c 33 2d 36 2e 31 2e 32 20 7b 0a 20 20 73 71 6c  l3-6.1.2 {.  sql
3030: 69 74 65 33 20 64 62 32 20 74 65 73 74 2e 64 62  ite3 db2 test.db
3040: 0a 20 20 73 71 6c 69 74 65 33 20 64 62 33 20 74  .  sqlite3 db3 t
3050: 65 73 74 2e 64 62 0a 20 20 65 78 65 63 73 71 6c  est.db.  execsql
3060: 20 7b 20 42 45 47 49 4e 20 3b 20 53 45 4c 45 43   { BEGIN ; SELEC
3070: 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d 20 64 62  T * FROM t1 } db
3080: 33 0a 7d 20 7b 6f 20 74 20 74 20 66 7d 0a 64 6f  3.} {o t t f}.do
3090: 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 31 2e 33  _test wal3-6.1.3
30a0: 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50   {.  execsql { P
30b0: 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70  RAGMA wal_checkp
30c0: 6f 69 6e 74 20 7d 20 64 62 32 0a 7d 20 7b 7d 0a  oint } db2.} {}.
30d0: 0a 23 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  .# At this point
30e0: 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 20 68 61   the log file ha
30f0: 73 20 62 65 65 6e 20 66 75 6c 6c 79 20 63 68 65  s been fully che
3100: 63 6b 70 6f 69 6e 74 65 64 2e 20 48 6f 77 65 76  ckpointed. Howev
3110: 65 72 2c 20 0a 23 20 63 6f 6e 6e 65 63 74 69 6f  er, .# connectio
3120: 6e 20 5b 64 62 33 5d 20 68 6f 6c 64 73 20 61 20  n [db3] holds a 
3130: 6c 6f 63 6b 20 74 68 61 74 20 70 72 65 76 65 6e  lock that preven
3140: 74 73 20 74 68 65 20 6c 6f 67 20 66 72 6f 6d 20  ts the log from 
3150: 62 65 69 6e 67 20 77 72 61 70 70 65 64 2e 0a 23  being wrapped..#
3160: 20 54 65 73 74 20 63 61 73 65 20 33 2e 36 2e 31   Test case 3.6.1
3170: 2e 34 20 68 61 73 20 5b 64 62 5d 20 61 74 74 65  .4 has [db] atte
3180: 6d 70 74 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  mpt a read-lock 
3190: 6f 6e 20 61 52 65 61 64 4d 61 72 6b 5b 30 5d 2e  on aReadMark[0].
31a0: 20 42 75 74 0a 23 20 61 73 20 69 74 20 69 73 20   But.# as it is 
31b0: 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f  obtaining the lo
31c0: 63 6b 2c 20 5b 64 62 32 5d 20 61 70 70 65 6e 64  ck, [db2] append
31d0: 73 20 74 6f 20 74 68 65 20 6c 6f 67 20 66 69 6c  s to the log fil
31e0: 65 2e 0a 23 0a 54 20 66 69 6c 74 65 72 20 78 53  e..#.T filter xS
31f0: 68 6d 4c 6f 63 6b 0a 54 20 73 63 72 69 70 74 20  hmLock.T script 
3200: 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 0a 70 72  lock_callback.pr
3210: 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b  oc lock_callback
3220: 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 20 68 61   {method file ha
3230: 6e 64 6c 65 20 73 70 65 63 7d 20 7b 0a 20 20 69  ndle spec} {.  i
3240: 66 20 7b 24 73 70 65 63 20 3d 3d 20 22 33 20 31  f {$spec == "3 1
3250: 20 6c 6f 63 6b 20 73 68 61 72 65 64 22 7d 20 7b   lock shared"} {
3260: 0a 20 20 20 20 23 20 54 68 69 73 20 69 73 20 74  .    # This is t
3270: 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  he callback for 
3280: 5b 64 62 5d 20 74 6f 20 6f 62 74 61 69 6e 20 74  [db] to obtain t
3290: 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20  he read lock on 
32a0: 61 52 65 61 64 4d 61 72 6b 5b 30 5d 2e 0a 20 20  aReadMark[0]..  
32b0: 20 20 23 20 44 69 73 61 62 6c 65 20 66 75 74 75    # Disable futu
32c0: 72 65 20 63 61 6c 6c 62 61 63 6b 73 20 75 73 69  re callbacks usi
32d0: 6e 67 20 5b 54 20 66 69 6c 74 65 72 20 7b 7d 5d  ng [T filter {}]
32e0: 20 61 6e 64 20 77 72 69 74 65 20 74 6f 20 74 68   and write to th
32f0: 65 20 6c 6f 67 0a 20 20 20 20 23 20 66 69 6c 65  e log.    # file
3300: 20 75 73 69 6e 67 20 5b 64 62 32 5d 2e 20 5b 64   using [db2]. [d
3310: 62 33 5d 20 69 73 20 70 72 65 76 65 6e 74 69 6e  b3] is preventin
3320: 67 20 5b 64 62 32 5d 20 66 72 6f 6d 20 77 72 61  g [db2] from wra
3330: 70 70 69 6e 67 20 74 68 65 20 6c 6f 67 0a 20 20  pping the log.  
3340: 20 20 23 20 68 65 72 65 2c 20 73 6f 20 74 68 69    # here, so thi
3350: 73 20 69 73 20 61 6e 20 61 70 70 65 6e 64 2e 0a  s is an append..
3360: 20 20 20 20 54 20 66 69 6c 74 65 72 20 7b 7d 0a      T filter {}.
3370: 20 20 20 20 64 62 32 20 65 76 61 6c 20 7b 20 49      db2 eval { I
3380: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
3390: 4c 55 45 53 28 27 66 27 2c 20 27 73 27 29 20 7d  LUES('f', 's') }
33a0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
33b0: 4c 49 54 45 5f 4f 4b 0a 7d 0a 64 6f 5f 74 65 73  LITE_OK.}.do_tes
33c0: 74 20 77 61 6c 33 2d 36 2e 31 2e 34 20 7b 0a 20  t wal3-6.1.4 {. 
33d0: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 42   execsql {.    B
33e0: 45 47 49 4e 3b 0a 20 20 20 20 53 45 4c 45 43 54  EGIN;.    SELECT
33f0: 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20 20 7d 0a   * FROM t1;.  }.
3400: 7d 20 7b 6f 20 74 20 74 20 66 20 66 20 73 7d 0a  } {o t t f f s}.
3410: 0a 23 20 5b 64 62 5d 20 73 68 6f 75 6c 64 20 62  .# [db] should b
3420: 65 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61  e left holding a
3430: 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 73 6f   read-lock on so
3440: 6d 65 20 73 6c 6f 74 20 6f 74 68 65 72 20 74 68  me slot other th
3450: 61 6e 20 0a 23 20 61 52 65 61 64 4d 61 72 6b 5b  an .# aReadMark[
3460: 30 5d 2e 20 54 65 73 74 20 74 68 69 73 20 62 79  0]. Test this by
3470: 20 64 65 6d 6f 6e 73 74 72 61 74 69 6e 67 20 74   demonstrating t
3480: 68 61 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63  hat the read-loc
3490: 6b 20 69 73 20 70 72 65 76 65 6e 74 69 6e 67 0a  k is preventing.
34a0: 23 20 74 68 65 20 6c 6f 67 20 66 72 6f 6d 20 62  # the log from b
34b0: 65 69 6e 67 20 77 72 61 70 70 65 64 2e 0a 23 0a  eing wrapped..#.
34c0: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 31  do_test wal3-6.1
34d0: 2e 35 20 7b 0a 20 20 64 62 33 20 65 76 61 6c 20  .5 {.  db3 eval 
34e0: 43 4f 4d 4d 49 54 0a 20 20 64 62 32 20 65 76 61  COMMIT.  db2 eva
34f0: 6c 20 7b 20 50 52 41 47 4d 41 20 77 61 6c 5f 63  l { PRAGMA wal_c
3500: 68 65 63 6b 70 6f 69 6e 74 20 7d 0a 20 20 73 65  heckpoint }.  se
3510: 74 20 73 7a 31 20 5b 66 69 6c 65 20 73 69 7a 65  t sz1 [file size
3520: 20 74 65 73 74 2e 64 62 2d 77 61 6c 5d 0a 20 20   test.db-wal].  
3530: 64 62 32 20 65 76 61 6c 20 7b 20 49 4e 53 45 52  db2 eval { INSER
3540: 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
3550: 28 27 73 27 2c 20 27 65 27 29 20 7d 0a 20 20 73  ('s', 'e') }.  s
3560: 65 74 20 73 7a 32 20 5b 66 69 6c 65 20 73 69 7a  et sz2 [file siz
3570: 65 20 74 65 73 74 2e 64 62 2d 77 61 6c 5d 0a 20  e test.db-wal]. 
3580: 20 65 78 70 72 20 7b 24 73 7a 32 3e 24 73 7a 31   expr {$sz2>$sz1
3590: 7d 0a 7d 20 7b 31 7d 0a 0a 23 20 54 65 73 74 20  }.} {1}..# Test 
35a0: 74 68 61 74 20 69 66 20 5b 64 62 32 5d 20 68 61  that if [db2] ha
35b0: 64 20 6e 6f 74 20 69 6e 74 65 72 66 65 72 65 64  d not interfered
35c0: 20 77 68 65 6e 20 5b 64 62 5d 20 77 61 73 20 74   when [db] was t
35d0: 72 79 69 6e 67 20 74 6f 20 67 72 61 62 0a 23 20  rying to grab.# 
35e0: 61 52 65 61 64 4d 61 72 6b 5b 30 5d 2c 20 69 74  aReadMark[0], it
35f0: 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e   would have been
3600: 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 77 72 61   possible to wra
3610: 70 20 74 68 65 20 6c 6f 67 20 69 6e 20 33 2e 36  p the log in 3.6
3620: 2e 31 2e 35 2e 0a 23 0a 64 6f 5f 74 65 73 74 20  .1.5..#.do_test 
3630: 77 61 6c 33 2d 36 2e 31 2e 36 20 7b 0a 20 20 65  wal3-6.1.6 {.  e
3640: 78 65 63 73 71 6c 20 7b 20 43 4f 4d 4d 49 54 20  xecsql { COMMIT 
3650: 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52  }.  execsql { PR
3660: 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f  AGMA wal_checkpo
3670: 69 6e 74 20 7d 20 64 62 32 0a 20 20 65 78 65 63  int } db2.  exec
3680: 73 71 6c 20 7b 0a 20 20 20 20 42 45 47 49 4e 3b  sql {.    BEGIN;
3690: 0a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52  .    SELECT * FR
36a0: 4f 4d 20 74 31 3b 0a 20 20 7d 0a 7d 20 7b 6f 20  OM t1;.  }.} {o 
36b0: 74 20 74 20 66 20 66 20 73 20 73 20 65 7d 0a 64  t t f f s s e}.d
36c0: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 36 2e 31 2e  o_test wal3-6.1.
36d0: 37 20 7b 0a 20 20 64 62 32 20 65 76 61 6c 20 7b  7 {.  db2 eval {
36e0: 20 50 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63   PRAGMA wal_chec
36f0: 6b 70 6f 69 6e 74 20 7d 0a 20 20 73 65 74 20 73  kpoint }.  set s
3700: 7a 31 20 5b 66 69 6c 65 20 73 69 7a 65 20 74 65  z1 [file size te
3710: 73 74 2e 64 62 2d 77 61 6c 5d 0a 20 20 64 62 32  st.db-wal].  db2
3720: 20 65 76 61 6c 20 7b 20 49 4e 53 45 52 54 20 49   eval { INSERT I
3730: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 27 6e  NTO t1 VALUES('n
3740: 27 2c 20 27 74 27 29 20 7d 0a 20 20 73 65 74 20  ', 't') }.  set 
3750: 73 7a 32 20 5b 66 69 6c 65 20 73 69 7a 65 20 74  sz2 [file size t
3760: 65 73 74 2e 64 62 2d 77 61 6c 5d 0a 20 20 65 78  est.db-wal].  ex
3770: 70 72 20 7b 24 73 7a 32 3d 3d 24 73 7a 31 7d 0a  pr {$sz2==$sz1}.
3780: 7d 20 7b 31 7d 0a 0a 64 62 33 20 63 6c 6f 73 65  } {1}..db3 close
3790: 0a 64 62 32 20 63 6c 6f 73 65 0a 64 62 20 63 6c  .db2 close.db cl
37a0: 6f 73 65 0a 0a 64 6f 5f 74 65 73 74 20 77 61 6c  ose..do_test wal
37b0: 33 2d 36 2e 32 2e 31 20 7b 0a 20 20 66 69 6c 65  3-6.2.1 {.  file
37c0: 20 64 65 6c 65 74 65 20 2d 66 6f 72 63 65 20 74   delete -force t
37d0: 65 73 74 2e 64 62 20 74 65 73 74 2e 64 62 2d 6a  est.db test.db-j
37e0: 6f 75 72 6e 61 6c 20 74 65 73 74 2e 64 62 20 77  ournal test.db w
37f0: 61 6c 0a 20 20 73 71 6c 69 74 65 33 20 64 62 20  al.  sqlite3 db 
3800: 74 65 73 74 2e 64 62 0a 20 20 73 71 6c 69 74 65  test.db.  sqlite
3810: 33 20 64 62 32 20 74 65 73 74 2e 64 62 0a 20 20  3 db2 test.db.  
3820: 65 78 65 63 73 71 6c 20 7b 20 50 52 41 47 4d 41  execsql { PRAGMA
3830: 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20   journal_mode = 
3840: 57 41 4c 20 7d 0a 20 20 65 78 65 63 73 71 6c 20  WAL }.  execsql 
3850: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41 42  {.    CREATE TAB
3860: 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 20 20  LE t1(a, b);.   
3870: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
3880: 56 41 4c 55 45 53 28 27 68 27 2c 20 27 68 27 29  VALUES('h', 'h')
3890: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
38a0: 4f 20 74 31 20 56 41 4c 55 45 53 28 27 6c 27 2c  O t1 VALUES('l',
38b0: 20 27 62 27 29 3b 0a 20 20 7d 0a 7d 20 7b 7d 0a   'b');.  }.} {}.
38c0: 0a 54 20 66 69 6c 74 65 72 20 78 53 68 6d 4c 6f  .T filter xShmLo
38d0: 63 6b 0a 54 20 73 63 72 69 70 74 20 6c 6f 63 6b  ck.T script lock
38e0: 5f 63 61 6c 6c 62 61 63 6b 0a 70 72 6f 63 20 6c  _callback.proc l
38f0: 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 20 7b 6d 65  ock_callback {me
3900: 74 68 6f 64 20 66 69 6c 65 20 68 61 6e 64 6c 65  thod file handle
3910: 20 73 70 65 63 7d 20 7b 0a 20 20 69 66 20 7b 24   spec} {.  if {$
3920: 73 70 65 63 20 3d 3d 20 22 33 20 31 20 75 6e 6c  spec == "3 1 unl
3930: 6f 63 6b 20 65 78 63 6c 75 73 69 76 65 22 7d 20  ock exclusive"} 
3940: 7b 0a 20 20 20 20 54 20 66 69 6c 74 65 72 20 7b  {.    T filter {
3950: 7d 0a 20 20 20 20 73 65 74 20 3a 3a 52 20 5b 64  }.    set ::R [d
3960: 62 32 20 65 76 61 6c 20 7b 0a 20 20 20 20 20 20  b2 eval {.      
3970: 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 53 45 4c  BEGIN;.      SEL
3980: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 3b 0a 20  ECT * FROM t1;. 
3990: 20 20 20 7d 5d 0a 20 20 7d 0a 7d 0a 64 6f 5f 74     }].  }.}.do_t
39a0: 65 73 74 20 77 61 6c 33 2d 36 2e 32 2e 32 20 7b  est wal3-6.2.2 {
39b0: 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50 52 41  .  execsql { PRA
39c0: 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70 6f 69  GMA wal_checkpoi
39d0: 6e 74 20 7d 0a 7d 20 7b 7d 0a 64 6f 5f 74 65 73  nt }.} {}.do_tes
39e0: 74 20 77 61 6c 33 2d 36 2e 32 2e 33 20 7b 0a 20  t wal3-6.2.3 {. 
39f0: 20 73 65 74 20 3a 3a 52 0a 7d 20 7b 68 20 68 20   set ::R.} {h h 
3a00: 6c 20 62 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c  l b}.do_test wal
3a10: 33 2d 36 2e 32 2e 34 20 7b 0a 20 20 73 65 74 20  3-6.2.4 {.  set 
3a20: 73 7a 31 20 5b 66 69 6c 65 20 73 69 7a 65 20 74  sz1 [file size t
3a30: 65 73 74 2e 64 62 2d 77 61 6c 5d 0a 20 20 65 78  est.db-wal].  ex
3a40: 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49  ecsql { INSERT I
3a50: 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 27 62  NTO t1 VALUES('b
3a60: 27 2c 20 27 63 27 29 3b 20 7d 0a 20 20 73 65 74  ', 'c'); }.  set
3a70: 20 73 7a 32 20 5b 66 69 6c 65 20 73 69 7a 65 20   sz2 [file size 
3a80: 74 65 73 74 2e 64 62 2d 77 61 6c 5d 0a 20 20 65  test.db-wal].  e
3a90: 78 70 72 20 7b 24 73 7a 32 20 3e 20 24 73 7a 31  xpr {$sz2 > $sz1
3aa0: 7d 0a 7d 20 7b 31 7d 0a 64 6f 5f 74 65 73 74 20  }.} {1}.do_test 
3ab0: 77 61 6c 33 2d 36 2e 32 2e 35 20 7b 0a 20 20 64  wal3-6.2.5 {.  d
3ac0: 62 32 20 65 76 61 6c 20 7b 20 43 4f 4d 4d 49 54  b2 eval { COMMIT
3ad0: 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 50   }.  execsql { P
3ae0: 52 41 47 4d 41 20 77 61 6c 5f 63 68 65 63 6b 70  RAGMA wal_checkp
3af0: 6f 69 6e 74 20 7d 0a 20 20 73 65 74 20 73 7a 31  oint }.  set sz1
3b00: 20 5b 66 69 6c 65 20 73 69 7a 65 20 74 65 73 74   [file size test
3b10: 2e 64 62 2d 77 61 6c 5d 0a 20 20 65 78 65 63 73  .db-wal].  execs
3b20: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
3b30: 20 74 31 20 56 41 4c 55 45 53 28 27 6e 27 2c 20   t1 VALUES('n', 
3b40: 27 6f 27 29 3b 20 7d 0a 20 20 73 65 74 20 73 7a  'o'); }.  set sz
3b50: 32 20 5b 66 69 6c 65 20 73 69 7a 65 20 74 65 73  2 [file size tes
3b60: 74 2e 64 62 2d 77 61 6c 5d 0a 20 20 65 78 70 72  t.db-wal].  expr
3b70: 20 7b 24 73 7a 32 20 3d 3d 20 24 73 7a 31 7d 0a   {$sz2 == $sz1}.
3b80: 7d 20 7b 31 7d 0a 20 0a 64 62 32 20 63 6c 6f 73  } {1}. .db2 clos
3b90: 65 0a 64 62 20 63 6c 6f 73 65 0a 54 20 64 65 6c  e.db close.T del
3ba0: 65 74 65 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ete..#----------
3bb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3bc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3bd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3be0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
3bf0: 23 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61  # When opening a
3c00: 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f   read-transactio
3c10: 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 2c  n on a database,
3c20: 20 69 66 20 74 68 65 20 65 6e 74 69 72 65 20 6c   if the entire l
3c30: 6f 67 20 68 61 73 0a 23 20 6e 6f 74 20 79 65 74  og has.# not yet
3c40: 20 62 65 65 6e 20 63 6f 70 69 65 64 20 74 6f 20   been copied to 
3c50: 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
3c60: 65 2c 20 74 68 65 20 72 65 61 64 65 72 20 67 72  e, the reader gr
3c70: 61 62 73 20 61 20 72 65 61 64 0a 23 20 6c 6f 63  abs a read.# loc
3c80: 6b 20 6f 6e 20 61 52 65 61 64 4d 61 72 6b 5b 78  k on aReadMark[x
3c90: 5d 2c 20 77 68 65 72 65 20 78 3e 30 2e 20 54 68  ], where x>0. Th
3ca0: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74  e following test
3cb0: 20 63 61 73 65 73 20 65 78 70 65 72 69 6d 65 6e   cases experimen
3cc0: 74 0a 23 20 77 69 74 68 20 74 68 65 20 6f 75 74  t.# with the out
3cd0: 63 6f 6d 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  come of the foll
3ce0: 6f 77 69 6e 67 3a 0a 23 0a 23 20 20 20 2b 20 54  owing:.#.#   + T
3cf0: 68 65 20 72 65 61 64 65 72 20 64 69 73 63 6f 76  he reader discov
3d00: 65 72 69 6e 67 20 74 68 61 74 20 62 65 74 77 65  ering that betwe
3d10: 65 6e 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e  en the time when
3d20: 20 69 74 20 72 65 61 64 20 74 68 65 0a 23 20 20   it read the.#  
3d30: 20 20 20 77 61 6c 2d 69 6e 64 65 78 20 68 65 61     wal-index hea
3d40: 64 65 72 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b  der and the lock
3d50: 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20 74 68   was obtained th
3d60: 61 74 20 61 20 77 72 69 74 65 72 20 68 61 73 20  at a writer has 
3d70: 0a 23 20 20 20 20 20 77 72 69 74 74 65 6e 20 74  .#     written t
3d80: 6f 20 74 68 65 20 6c 6f 67 2e 20 49 6e 20 74 68  o the log. In th
3d90: 69 73 20 63 61 73 65 20 74 68 65 20 72 65 61 64  is case the read
3da0: 65 72 20 73 68 6f 75 6c 64 20 72 65 2d 72 65 61  er should re-rea
3db0: 64 20 74 68 65 20 0a 23 20 20 20 20 20 77 61 6c  d the .#     wal
3dc0: 2d 69 6e 64 65 78 20 68 65 61 64 65 72 20 61 6e  -index header an
3dd0: 64 20 6c 6f 63 6b 20 61 20 73 6e 61 70 73 68 6f  d lock a snapsho
3de0: 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
3df0: 74 6f 20 74 68 65 20 6e 65 77 20 0a 23 20 20 20  to the new .#   
3e00: 20 20 68 65 61 64 65 72 2e 0a 23 0a 23 20 20 20    header..#.#   
3e10: 2b 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 74  + The value in t
3e20: 68 65 20 61 52 65 61 64 4d 61 72 6b 5b 78 5d 20  he aReadMark[x] 
3e30: 73 6c 6f 74 20 68 61 73 20 62 65 65 6e 20 6d 6f  slot has been mo
3e40: 64 69 66 69 65 64 20 73 69 6e 63 65 20 69 74 20  dified since it 
3e50: 77 61 73 0a 23 20 20 20 20 20 72 65 61 64 2e 0a  was.#     read..
3e60: 23 0a 63 61 74 63 68 20 7b 64 62 20 63 6c 6f 73  #.catch {db clos
3e70: 65 7d 0a 74 65 73 74 76 66 73 20 54 20 2d 64 65  e}.testvfs T -de
3e80: 66 61 75 6c 74 20 31 0a 64 6f 5f 74 65 73 74 20  fault 1.do_test 
3e90: 77 61 6c 33 2d 37 2e 31 2e 31 20 7b 0a 20 20 66  wal3-7.1.1 {.  f
3ea0: 69 6c 65 20 64 65 6c 65 74 65 20 2d 66 6f 72 63  ile delete -forc
3eb0: 65 20 74 65 73 74 2e 64 62 20 74 65 73 74 2e 64  e test.db test.d
3ec0: 62 2d 6a 6f 75 72 6e 61 6c 20 74 65 73 74 2e 64  b-journal test.d
3ed0: 62 20 77 61 6c 0a 20 20 73 71 6c 69 74 65 33 20  b wal.  sqlite3 
3ee0: 64 62 20 74 65 73 74 2e 64 62 0a 20 20 65 78 65  db test.db.  exe
3ef0: 63 73 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d  csql {.    PRAGM
3f00: 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d  A journal_mode =
3f10: 20 57 41 4c 3b 0a 20 20 20 20 43 52 45 41 54 45   WAL;.    CREATE
3f20: 20 54 41 42 4c 45 20 62 6c 75 65 28 72 65 64 20   TABLE blue(red 
3f30: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 67 72 65  PRIMARY KEY, gre
3f40: 65 6e 29 3b 0a 20 20 7d 0a 7d 20 7b 77 61 6c 7d  en);.  }.} {wal}
3f50: 0a 0a 54 20 73 63 72 69 70 74 20 6d 65 74 68 6f  ..T script metho
3f60: 64 5f 63 61 6c 6c 62 61 63 6b 0a 54 20 66 69 6c  d_callback.T fil
3f70: 74 65 72 20 78 4f 70 65 6e 0a 70 72 6f 63 20 6d  ter xOpen.proc m
3f80: 65 74 68 6f 64 5f 63 61 6c 6c 62 61 63 6b 20 7b  ethod_callback {
3f90: 6d 65 74 68 6f 64 20 61 72 67 73 7d 20 7b 0a 20  method args} {. 
3fa0: 20 69 66 20 7b 24 6d 65 74 68 6f 64 20 3d 3d 20   if {$method == 
3fb0: 22 78 4f 70 65 6e 22 7d 20 7b 20 72 65 74 75 72  "xOpen"} { retur
3fc0: 6e 20 22 72 65 61 64 65 72 22 20 7d 0a 7d 0a 64  n "reader" }.}.d
3fd0: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 37 2e 31 2e  o_test wal3-7.1.
3fe0: 32 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 64 62  2 {.  sqlite3 db
3ff0: 32 20 74 65 73 74 2e 64 62 0a 20 20 65 78 65 63  2 test.db.  exec
4000: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46  sql { SELECT * F
4010: 52 4f 4d 20 62 6c 75 65 20 7d 20 64 62 32 0a 7d  ROM blue } db2.}
4020: 20 7b 7d 0a 0a 54 20 66 69 6c 74 65 72 20 78 53   {}..T filter xS
4030: 68 6d 4c 6f 63 6b 0a 73 65 74 20 3a 3a 6c 6f 63  hmLock.set ::loc
4040: 6b 73 20 5b 6c 69 73 74 5d 0a 70 72 6f 63 20 6d  ks [list].proc m
4050: 65 74 68 6f 64 5f 63 61 6c 6c 62 61 63 6b 20 7b  ethod_callback {
4060: 6d 65 74 68 6f 64 20 66 69 6c 65 20 68 61 6e 64  method file hand
4070: 6c 65 20 73 70 65 63 7d 20 7b 0a 20 20 69 66 20  le spec} {.  if 
4080: 7b 24 68 61 6e 64 6c 65 20 21 3d 20 22 72 65 61  {$handle != "rea
4090: 64 65 72 22 20 7d 20 7b 20 72 65 74 75 72 6e 20  der" } { return 
40a0: 7d 0a 20 20 69 66 20 7b 24 6d 65 74 68 6f 64 20  }.  if {$method 
40b0: 3d 3d 20 22 78 53 68 6d 4c 6f 63 6b 22 7d 20 7b  == "xShmLock"} {
40c0: 0a 20 20 20 20 63 61 74 63 68 20 7b 20 65 78 65  .    catch { exe
40d0: 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e  csql { INSERT IN
40e0: 54 4f 20 62 6c 75 65 20 56 41 4c 55 45 53 28 31  TO blue VALUES(1
40f0: 2c 20 32 29 20 7d 20 7d 0a 20 20 20 20 63 61 74  , 2) } }.    cat
4100: 63 68 20 7b 20 65 78 65 63 73 71 6c 20 7b 20 49  ch { execsql { I
4110: 4e 53 45 52 54 20 49 4e 54 4f 20 62 6c 75 65 20  NSERT INTO blue 
4120: 56 41 4c 55 45 53 28 33 2c 20 34 29 20 7d 20 7d  VALUES(3, 4) } }
4130: 0a 20 20 7d 0a 20 20 6c 61 70 70 65 6e 64 20 3a  .  }.  lappend :
4140: 3a 6c 6f 63 6b 73 20 24 73 70 65 63 0a 7d 0a 64  :locks $spec.}.d
4150: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 37 2e 31 2e  o_test wal3-7.1.
4160: 33 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20  3 {.  execsql { 
4170: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 62 6c  SELECT * FROM bl
4180: 75 65 20 7d 20 64 62 32 0a 7d 20 7b 31 20 32 20  ue } db2.} {1 2 
4190: 33 20 34 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c  3 4}.do_test wal
41a0: 33 2d 37 2e 31 2e 34 20 7b 0a 20 20 73 65 74 20  3-7.1.4 {.  set 
41b0: 3a 3a 6c 6f 63 6b 73 0a 7d 20 7b 7b 34 20 31 20  ::locks.} {{4 1 
41c0: 6c 6f 63 6b 20 73 68 61 72 65 64 7d 20 7b 34 20  lock shared} {4 
41d0: 31 20 75 6e 6c 6f 63 6b 20 73 68 61 72 65 64 7d  1 unlock shared}
41e0: 20 7b 35 20 31 20 6c 6f 63 6b 20 73 68 61 72 65   {5 1 lock share
41f0: 64 7d 20 7b 35 20 31 20 75 6e 6c 6f 63 6b 20 73  d} {5 1 unlock s
4200: 68 61 72 65 64 7d 7d 0a 0a 73 65 74 20 3a 3a 6c  hared}}..set ::l
4210: 6f 63 6b 73 20 5b 6c 69 73 74 5d 0a 70 72 6f 63  ocks [list].proc
4220: 20 6d 65 74 68 6f 64 5f 63 61 6c 6c 62 61 63 6b   method_callback
4230: 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65 20 68 61   {method file ha
4240: 6e 64 6c 65 20 73 70 65 63 7d 20 7b 0a 20 20 69  ndle spec} {.  i
4250: 66 20 7b 24 68 61 6e 64 6c 65 20 21 3d 20 22 72  f {$handle != "r
4260: 65 61 64 65 72 22 20 7d 20 7b 20 72 65 74 75 72  eader" } { retur
4270: 6e 20 7d 0a 20 20 69 66 20 7b 24 6d 65 74 68 6f  n }.  if {$metho
4280: 64 20 3d 3d 20 22 78 53 68 6d 4c 6f 63 6b 22 7d  d == "xShmLock"}
4290: 20 7b 0a 20 20 20 20 63 61 74 63 68 20 7b 20 65   {.    catch { e
42a0: 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  xecsql { INSERT 
42b0: 49 4e 54 4f 20 62 6c 75 65 20 56 41 4c 55 45 53  INTO blue VALUES
42c0: 28 35 2c 20 36 29 20 7d 20 7d 0a 20 20 7d 0a 20  (5, 6) } }.  }. 
42d0: 20 6c 61 70 70 65 6e 64 20 3a 3a 6c 6f 63 6b 73   lappend ::locks
42e0: 20 24 73 70 65 63 0a 7d 0a 64 6f 5f 74 65 73 74   $spec.}.do_test
42f0: 20 77 61 6c 33 2d 37 2e 32 2e 31 20 7b 0a 20 20   wal3-7.2.1 {.  
4300: 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54  execsql { SELECT
4310: 20 2a 20 46 52 4f 4d 20 62 6c 75 65 20 7d 20 64   * FROM blue } d
4320: 62 32 0a 7d 20 7b 31 20 32 20 33 20 34 20 35 20  b2.} {1 2 3 4 5 
4330: 36 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  6}.do_test wal3-
4340: 37 2e 32 2e 32 20 7b 0a 20 20 73 65 74 20 3a 3a  7.2.2 {.  set ::
4350: 6c 6f 63 6b 73 0a 7d 20 7b 7b 35 20 31 20 6c 6f  locks.} {{5 1 lo
4360: 63 6b 20 73 68 61 72 65 64 7d 20 7b 35 20 31 20  ck shared} {5 1 
4370: 75 6e 6c 6f 63 6b 20 73 68 61 72 65 64 7d 20 7b  unlock shared} {
4380: 34 20 31 20 6c 6f 63 6b 20 73 68 61 72 65 64 7d  4 1 lock shared}
4390: 20 7b 34 20 31 20 75 6e 6c 6f 63 6b 20 73 68 61   {4 1 unlock sha
43a0: 72 65 64 7d 7d 0a 0a 64 62 20 63 6c 6f 73 65 0a  red}}..db close.
43b0: 64 62 32 20 63 6c 6f 73 65 0a 54 20 64 65 6c 65  db2 close.T dele
43c0: 74 65 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  te..#-----------
43d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
43e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
43f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4400: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
4410: 20 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 38   .do_test wal3-8
4420: 2e 31 20 7b 0a 20 20 66 69 6c 65 20 64 65 6c 65  .1 {.  file dele
4430: 74 65 20 2d 66 6f 72 63 65 20 74 65 73 74 2e 64  te -force test.d
4440: 62 20 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61  b test.db-journa
4450: 6c 20 74 65 73 74 2e 64 62 20 77 61 6c 0a 20 20  l test.db wal.  
4460: 73 71 6c 69 74 65 33 20 64 62 20 74 65 73 74 2e  sqlite3 db test.
4470: 64 62 0a 20 20 73 71 6c 69 74 65 33 20 64 62 32  db.  sqlite3 db2
4480: 20 74 65 73 74 2e 64 62 0a 20 20 65 78 65 63 73   test.db.  execs
4490: 71 6c 20 7b 0a 20 20 20 20 50 52 41 47 4d 41 20  ql {.    PRAGMA 
44a0: 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 57  journal_mode = W
44b0: 41 4c 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54  AL;.    CREATE T
44c0: 41 42 4c 45 20 62 28 63 29 3b 0a 20 20 20 20 49  ABLE b(c);.    I
44d0: 4e 53 45 52 54 20 49 4e 54 4f 20 62 20 56 41 4c  NSERT INTO b VAL
44e0: 55 45 53 28 27 54 65 68 72 61 6e 27 29 3b 0a 20  UES('Tehran');. 
44f0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 62     INSERT INTO b
4500: 20 56 41 4c 55 45 53 28 27 51 6f 6d 27 29 3b 0a   VALUES('Qom');.
4510: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
4520: 62 20 56 41 4c 55 45 53 28 27 4d 61 72 6b 61 7a  b VALUES('Markaz
4530: 69 27 29 3b 0a 20 20 20 20 50 52 41 47 4d 41 20  i');.    PRAGMA 
4540: 77 61 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 3b 0a  wal_checkpoint;.
4550: 20 20 7d 0a 7d 20 7b 77 61 6c 7d 0a 64 6f 5f 74    }.} {wal}.do_t
4560: 65 73 74 20 77 61 6c 33 2d 38 2e 32 20 7b 0a 20  est wal3-8.2 {. 
4570: 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45 43   execsql { SELEC
4580: 54 20 2a 20 46 52 4f 4d 20 62 20 7d 0a 7d 20 7b  T * FROM b }.} {
4590: 54 65 68 72 61 6e 20 51 6f 6d 20 4d 61 72 6b 61  Tehran Qom Marka
45a0: 7a 69 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33  zi}.do_test wal3
45b0: 2d 38 2e 33 20 7b 0a 20 20 64 62 20 65 76 61 6c  -8.3 {.  db eval
45c0: 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   { SELECT * FROM
45d0: 20 62 20 7d 20 7b 0a 20 20 20 20 64 62 20 65 76   b } {.    db ev
45e0: 61 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  al { INSERT INTO
45f0: 20 62 20 56 41 4c 55 45 53 28 27 51 61 7a 76 69   b VALUES('Qazvi
4600: 6e 27 29 20 7d 0a 20 20 20 20 73 65 74 20 72 20  n') }.    set r 
4610: 5b 64 62 32 20 65 76 61 6c 20 7b 20 53 45 4c 45  [db2 eval { SELE
4620: 43 54 20 2a 20 46 52 4f 4d 20 62 20 7d 5d 0a 20  CT * FROM b }]. 
4630: 20 20 20 62 72 65 61 6b 0a 20 20 7d 0a 20 20 73     break.  }.  s
4640: 65 74 20 72 0a 7d 20 7b 54 65 68 72 61 6e 20 51  et r.} {Tehran Q
4650: 6f 6d 20 4d 61 72 6b 61 7a 69 20 51 61 7a 76 69  om Markazi Qazvi
4660: 6e 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d  n}.do_test wal3-
4670: 38 2e 34 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  8.4 {.  execsql 
4680: 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  {.    INSERT INT
4690: 4f 20 62 20 56 41 4c 55 45 53 28 27 47 69 6c 61  O b VALUES('Gila
46a0: 6e 27 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20  n');.    INSERT 
46b0: 49 4e 54 4f 20 62 20 56 41 4c 55 45 53 28 27 41  INTO b VALUES('A
46c0: 72 64 61 62 69 6c 27 29 3b 0a 20 20 7d 0a 7d 20  rdabil');.  }.} 
46d0: 7b 7d 0a 64 62 32 20 63 6c 6f 73 65 0a 0a 66 61  {}.db2 close..fa
46e0: 75 6c 74 73 69 6d 5f 73 61 76 65 5f 61 6e 64 5f  ultsim_save_and_
46f0: 63 6c 6f 73 65 0a 74 65 73 74 76 66 73 20 54 20  close.testvfs T 
4700: 2d 64 65 66 61 75 6c 74 20 31 0a 66 61 75 6c 74  -default 1.fault
4710: 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61 6e 64 5f  sim_restore_and_
4720: 72 65 6f 70 65 6e 0a 54 20 66 69 6c 74 65 72 20  reopen.T filter 
4730: 78 53 68 6d 4c 6f 63 6b 0a 54 20 73 63 72 69 70  xShmLock.T scrip
4740: 74 20 6c 6f 63 6b 5f 63 61 6c 6c 62 61 63 6b 0a  t lock_callback.
4750: 0a 70 72 6f 63 20 6c 6f 63 6b 5f 63 61 6c 6c 62  .proc lock_callb
4760: 61 63 6b 20 7b 6d 65 74 68 6f 64 20 66 69 6c 65  ack {method file
4770: 20 68 61 6e 64 6c 65 20 73 70 65 63 7d 20 7b 0a   handle spec} {.
4780: 20 20 69 66 20 7b 24 73 70 65 63 20 3d 3d 20 22    if {$spec == "
4790: 34 20 31 20 75 6e 6c 6f 63 6b 20 65 78 63 6c 75  4 1 unlock exclu
47a0: 73 69 76 65 22 7d 20 7b 0a 20 20 20 20 54 20 66  sive"} {.    T f
47b0: 69 6c 74 65 72 20 7b 7d 0a 20 20 20 20 73 65 74  ilter {}.    set
47c0: 20 3a 3a 72 20 5b 63 61 74 63 68 73 71 6c 20 7b   ::r [catchsql {
47d0: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 62   SELECT * FROM b
47e0: 20 7d 20 64 62 32 5d 0a 20 20 7d 0a 7d 0a 73 71   } db2].  }.}.sq
47f0: 6c 69 74 65 33 20 64 62 20 74 65 73 74 2e 64 62  lite3 db test.db
4800: 0a 73 71 6c 69 74 65 33 20 64 62 32 20 74 65 73  .sqlite3 db2 tes
4810: 74 2e 64 62 0a 64 6f 5f 74 65 73 74 20 77 61 6c  t.db.do_test wal
4820: 33 2d 38 2e 35 20 7b 0a 20 20 65 78 65 63 73 71  3-8.5 {.  execsq
4830: 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  l { SELECT * FRO
4840: 4d 20 62 20 7d 0a 7d 20 7b 54 65 68 72 61 6e 20  M b }.} {Tehran 
4850: 51 6f 6d 20 4d 61 72 6b 61 7a 69 20 51 61 7a 76  Qom Markazi Qazv
4860: 69 6e 20 47 69 6c 61 6e 20 41 72 64 61 62 69 6c  in Gilan Ardabil
4870: 7d 0a 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 38  }.do_test wal3-8
4880: 2e 36 20 7b 0a 20 20 73 65 74 20 3a 3a 72 0a 7d  .6 {.  set ::r.}
4890: 20 7b 31 20 7b 6c 6f 63 6b 69 6e 67 20 70 72 6f   {1 {locking pro
48a0: 74 6f 63 6f 6c 7d 7d 0a 0a 64 62 20 63 6c 6f 73  tocol}}..db clos
48b0: 65 0a 64 62 32 20 63 6c 6f 73 65 0a 0a 66 61 75  e.db2 close..fau
48c0: 6c 74 73 69 6d 5f 72 65 73 74 6f 72 65 5f 61 6e  ltsim_restore_an
48d0: 64 5f 72 65 6f 70 65 6e 0a 73 71 6c 69 74 65 33  d_reopen.sqlite3
48e0: 20 64 62 32 20 74 65 73 74 2e 64 62 0a 54 20 66   db2 test.db.T f
48f0: 69 6c 74 65 72 20 78 53 68 6d 4c 6f 63 6b 0a 54  ilter xShmLock.T
4900: 20 73 63 72 69 70 74 20 6c 6f 63 6b 5f 63 61 6c   script lock_cal
4910: 6c 62 61 63 6b 0a 70 72 6f 63 20 6c 6f 63 6b 5f  lback.proc lock_
4920: 63 61 6c 6c 62 61 63 6b 20 7b 6d 65 74 68 6f 64  callback {method
4930: 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 73 70 65   file handle spe
4940: 63 7d 20 7b 0a 20 20 69 66 20 7b 24 73 70 65 63  c} {.  if {$spec
4950: 20 3d 3d 20 22 31 20 37 20 75 6e 6c 6f 63 6b 20   == "1 7 unlock 
4960: 65 78 63 6c 75 73 69 76 65 22 7d 20 7b 0a 20 20  exclusive"} {.  
4970: 20 20 54 20 66 69 6c 74 65 72 20 7b 7d 0a 20 20    T filter {}.  
4980: 20 20 73 65 74 20 3a 3a 72 20 5b 63 61 74 63 68    set ::r [catch
4990: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46  sql { SELECT * F
49a0: 52 4f 4d 20 62 20 7d 20 64 62 32 5d 0a 20 20 7d  ROM b } db2].  }
49b0: 0a 7d 0a 75 6e 73 65 74 20 3a 3a 72 0a 64 6f 5f  .}.unset ::r.do_
49c0: 74 65 73 74 20 77 61 6c 33 2d 38 2e 35 20 7b 0a  test wal3-8.5 {.
49d0: 20 20 65 78 65 63 73 71 6c 20 7b 20 53 45 4c 45    execsql { SELE
49e0: 43 54 20 2a 20 46 52 4f 4d 20 62 20 7d 0a 7d 20  CT * FROM b }.} 
49f0: 7b 54 65 68 72 61 6e 20 51 6f 6d 20 4d 61 72 6b  {Tehran Qom Mark
4a00: 61 7a 69 20 51 61 7a 76 69 6e 20 47 69 6c 61 6e  azi Qazvin Gilan
4a10: 20 41 72 64 61 62 69 6c 7d 0a 64 6f 5f 74 65 73   Ardabil}.do_tes
4a20: 74 20 77 61 6c 33 2d 38 2e 36 20 7b 0a 20 20 73  t wal3-8.6 {.  s
4a30: 65 74 20 3a 3a 72 0a 7d 20 7b 31 20 7b 6c 6f 63  et ::r.} {1 {loc
4a40: 6b 69 6e 67 20 70 72 6f 74 6f 63 6f 6c 7d 7d 0a  king protocol}}.
4a50: 0a 64 62 20 63 6c 6f 73 65 0a 64 62 32 20 63 6c  .db close.db2 cl
4a60: 6f 73 65 0a 54 20 64 65 6c 65 74 65 0a 0a 23 2d  ose.T delete..#-
4a70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4a80: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4a90: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4aa0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
4ab0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 57 68 65 6e 20  --------.# When 
4ac0: 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65  a connection ope
4ad0: 6e 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  ns a read-lock o
4ae0: 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
4af0: 69 74 20 73 65 61 72 63 68 65 73 20 66 6f 72 0a  it searches for.
4b00: 23 20 61 6e 20 61 52 65 61 64 4d 61 72 6b 5b 5d  # an aReadMark[]
4b10: 20 73 6c 6f 74 20 74 68 61 74 20 69 73 20 61 6c   slot that is al
4b20: 72 65 61 64 79 20 73 65 74 20 74 6f 20 74 68 65  ready set to the
4b30: 20 6d 78 46 72 61 6d 65 20 76 61 6c 75 65 20 66   mxFrame value f
4b40: 6f 72 20 74 68 65 0a 23 20 6e 65 77 20 74 72 61  or the.# new tra
4b50: 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74 20  nsaction. If it 
4b60: 63 61 6e 6e 6f 74 20 66 69 6e 64 20 6f 6e 65 2c  cannot find one,
4b70: 20 69 74 20 61 74 74 65 6d 70 74 73 20 74 6f 20   it attempts to 
4b80: 6f 62 74 61 69 6e 20 61 6e 20 0a 23 20 65 78 63  obtain an .# exc
4b90: 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 61  lusive lock on a
4ba0: 6e 20 61 52 65 61 64 4d 61 72 6b 5b 5d 20 73 6c  n aReadMark[] sl
4bb0: 6f 74 20 66 6f 72 20 74 68 65 20 70 75 72 70 6f  ot for the purpo
4bc0: 73 65 73 20 6f 66 20 6d 6f 64 69 66 79 69 6e 67  ses of modifying
4bd0: 0a 23 20 74 68 65 20 76 61 6c 75 65 2c 20 74 68  .# the value, th
4be0: 65 6e 20 64 72 6f 70 73 20 62 61 63 6b 20 74 6f  en drops back to
4bf0: 20 61 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 66   a shared-lock f
4c00: 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
4c10: 6f 66 20 74 68 65 0a 23 20 74 72 61 6e 73 61 63  of the.# transac
4c20: 74 69 6f 6e 2e 0a 23 0a 23 20 54 68 69 73 20 74  tion..#.# This t
4c30: 65 73 74 20 63 61 73 65 20 76 65 72 69 66 69 65  est case verifie
4c40: 73 20 74 68 61 74 20 69 66 20 61 6e 20 65 78 63  s that if an exc
4c50: 6c 75 73 69 76 65 20 6c 6f 63 6b 20 63 61 6e 6e  lusive lock cann
4c60: 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 23  ot be obtained.#
4c70: 20 6f 6e 20 61 6e 79 20 61 52 65 61 64 4d 61 72   on any aReadMar
4c80: 6b 5b 5d 20 73 6c 6f 74 20 28 62 65 63 61 75 73  k[] slot (becaus
4c90: 65 20 74 68 65 72 65 20 61 72 65 20 61 6c 72 65  e there are alre
4ca0: 61 64 79 20 73 65 76 65 72 61 6c 20 72 65 61 64  ady several read
4cb0: 65 72 73 29 2c 0a 23 20 74 68 65 20 63 6c 69 65  ers),.# the clie
4cc0: 6e 74 20 74 61 6b 65 73 20 61 20 73 68 61 72 65  nt takes a share
4cd0: 64 2d 6c 6f 63 6b 20 6f 6e 20 61 20 73 6c 6f 74  d-lock on a slot
4ce0: 20 77 69 74 68 6f 75 74 20 6d 6f 64 69 66 79 69   without modifyi
4cf0: 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 23 20 61  ng the value.# a
4d00: 6e 64 20 63 6f 6e 74 69 6e 75 65 73 2e 0a 23 0a  nd continues..#.
4d10: 64 6f 5f 74 65 73 74 20 77 61 6c 33 2d 39 2e 30  do_test wal3-9.0
4d20: 20 7b 0a 20 20 66 69 6c 65 20 64 65 6c 65 74 65   {.  file delete
4d30: 20 2d 66 6f 72 63 65 20 74 65 73 74 2e 64 62 20   -force test.db 
4d40: 74 65 73 74 2e 64 62 2d 6a 6f 75 72 6e 61 6c 20  test.db-journal 
4d50: 74 65 73 74 2e 64 62 20 77 61 6c 0a 20 20 73 71  test.db wal.  sq
4d60: 6c 69 74 65 33 20 64 62 20 74 65 73 74 2e 64 62  lite3 db test.db
4d70: 0a 20 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20  .  execsql {.   
4d80: 20 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f   PRAGMA journal_
4d90: 6d 6f 64 65 20 3d 20 57 41 4c 3b 0a 20 20 20 20  mode = WAL;.    
4da0: 43 52 45 41 54 45 20 54 41 42 4c 45 20 77 68 6f  CREATE TABLE who
4db0: 61 6d 69 28 78 29 3b 0a 20 20 20 20 49 4e 53 45  ami(x);.    INSE
4dc0: 52 54 20 49 4e 54 4f 20 77 68 6f 61 6d 69 20 56  RT INTO whoami V
4dd0: 41 4c 55 45 53 28 27 6e 6f 62 6f 64 79 27 29 3b  ALUES('nobody');
4de0: 0a 20 20 7d 0a 7d 20 7b 77 61 6c 7d 0a 66 6f 72  .  }.} {wal}.for
4df0: 20 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c   {set i 0} {$i <
4e00: 20 35 30 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a   50} {incr i} {.
4e10: 20 20 73 65 74 20 63 20 64 62 24 69 0a 20 20 64    set c db$i.  d
4e20: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 39 2e 31 2e  o_test wal3-9.1.
4e30: 24 69 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  $i {.    sqlite3
4e40: 20 24 63 20 74 65 73 74 2e 64 62 0a 20 20 20 20   $c test.db.    
4e50: 65 78 65 63 73 71 6c 20 7b 20 55 50 44 41 54 45  execsql { UPDATE
4e60: 20 77 68 6f 61 6d 69 20 53 45 54 20 78 20 3d 20   whoami SET x = 
4e70: 24 63 20 7d 0a 20 20 20 20 65 78 65 63 73 71 6c  $c }.    execsql
4e80: 20 7b 0a 20 20 20 20 20 20 42 45 47 49 4e 3b 0a   {.      BEGIN;.
4e90: 20 20 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46        SELECT * F
4ea0: 52 4f 4d 20 77 68 6f 61 6d 69 0a 20 20 20 20 7d  ROM whoami.    }
4eb0: 20 24 63 0a 20 20 7d 20 24 63 0a 7d 0a 66 6f 72   $c.  } $c.}.for
4ec0: 20 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c   {set i 0} {$i <
4ed0: 20 35 30 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a   50} {incr i} {.
4ee0: 20 20 73 65 74 20 63 20 64 62 24 69 0a 20 20 64    set c db$i.  d
4ef0: 6f 5f 74 65 73 74 20 77 61 6c 33 2d 39 2e 32 2e  o_test wal3-9.2.
4f00: 24 69 20 7b 0a 20 20 20 20 65 78 65 63 73 71 6c  $i {.    execsql
4f10: 20 7b 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d   { SELECT * FROM
4f20: 20 77 68 6f 61 6d 69 20 7d 20 24 63 0a 20 20 7d   whoami } $c.  }
4f30: 20 24 63 0a 7d 0a 64 6f 5f 74 65 73 74 20 77 61   $c.}.do_test wa
4f40: 6c 33 2d 39 2e 33 20 7b 0a 20 20 66 6f 72 20 7b  l3-9.3 {.  for {
4f50: 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c 20 34  set i 0} {$i < 4
4f60: 39 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 20 64 62  9} {incr i} { db
4f70: 24 69 20 63 6c 6f 73 65 20 7d 0a 20 20 65 78 65  $i close }.  exe
4f80: 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 77 61  csql { PRAGMA wa
4f90: 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20 7d 20 0a  l_checkpoint } .
4fa0: 20 20 73 65 74 20 73 7a 31 20 5b 66 69 6c 65 20    set sz1 [file 
4fb0: 73 69 7a 65 20 74 65 73 74 2e 64 62 5d 0a 20 20  size test.db].  
4fc0: 64 62 34 39 20 63 6c 6f 73 65 0a 20 20 65 78 65  db49 close.  exe
4fd0: 63 73 71 6c 20 7b 20 50 52 41 47 4d 41 20 77 61  csql { PRAGMA wa
4fe0: 6c 5f 63 68 65 63 6b 70 6f 69 6e 74 20 7d 20 0a  l_checkpoint } .
4ff0: 20 20 73 65 74 20 73 7a 32 20 5b 66 69 6c 65 20    set sz2 [file 
5000: 73 69 7a 65 20 74 65 73 74 2e 64 62 5d 0a 20 20  size test.db].  
5010: 65 78 70 72 20 7b 24 73 7a 32 20 3e 20 24 73 7a  expr {$sz2 > $sz
5020: 31 7d 0a 7d 20 7b 31 7d 0a 0a 64 62 20 63 6c 6f  1}.} {1}..db clo
5030: 73 65 0a 0a 66 69 6e 69 73 68 5f 74 65 73 74 0a  se..finish_test.
5040: 0a                                               .