wal2.test 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. # 2010 May 5
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the operation of the library in
  13. # "PRAGMA journal_mode=WAL" mode.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/lock_common.tcl
  18. source $testdir/malloc_common.tcl
  19. source $testdir/wal_common.tcl
  20. set testprefix wal2
  21. ifcapable !wal {finish_test ; return }
  22. set sqlite_sync_count 0
  23. proc cond_incr_sync_count {adj} {
  24. global sqlite_sync_count
  25. if {$::tcl_platform(platform) == "windows"} {
  26. incr sqlite_sync_count $adj
  27. } {
  28. ifcapable !dirsync {
  29. incr sqlite_sync_count $adj
  30. }
  31. }
  32. }
  33. proc set_tvfs_hdr {file args} {
  34. # Set $nHdr to the number of bytes in the wal-index header:
  35. set nHdr 48
  36. set nInt [expr {$nHdr/4}]
  37. if {[llength $args]>2} {
  38. error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"}
  39. }
  40. set blob [tvfs shm $file]
  41. if {$::tcl_platform(byteOrder)=="bigEndian"} {set fmt I} {set fmt i}
  42. if {[llength $args]} {
  43. set ia [lindex $args 0]
  44. set ib $ia
  45. if {[llength $args]==2} {
  46. set ib [lindex $args 1]
  47. }
  48. binary scan $blob a[expr $nHdr*2]a* dummy tail
  49. set blob [binary format ${fmt}${nInt}${fmt}${nInt}a* $ia $ib $tail]
  50. tvfs shm $file $blob
  51. }
  52. binary scan $blob ${fmt}${nInt} ints
  53. return $ints
  54. }
  55. proc incr_tvfs_hdr {file idx incrval} {
  56. set ints [set_tvfs_hdr $file]
  57. set v [lindex $ints $idx]
  58. incr v $incrval
  59. lset ints $idx $v
  60. set_tvfs_hdr $file $ints
  61. }
  62. #-------------------------------------------------------------------------
  63. # Test case wal2-1.*:
  64. #
  65. # Set up a small database containing a single table. The database is not
  66. # checkpointed during the test - all content resides in the log file.
  67. #
  68. # Two connections are established to the database file - a writer ([db])
  69. # and a reader ([db2]). For each of the 8 integer fields in the wal-index
  70. # header (6 fields and 2 checksum values), do the following:
  71. #
  72. # 1. Modify the database using the writer.
  73. #
  74. # 2. Attempt to read the database using the reader. Before the reader
  75. # has a chance to snapshot the wal-index header, increment one
  76. # of the integer fields (so that the reader ends up with a corrupted
  77. # header).
  78. #
  79. # 3. Check that the reader recovers the wal-index and reads the correct
  80. # database content.
  81. #
  82. do_test wal2-1.0 {
  83. proc tvfs_cb {method filename args} {
  84. set ::filename $filename
  85. return SQLITE_OK
  86. }
  87. testvfs tvfs
  88. tvfs script tvfs_cb
  89. tvfs filter xShmOpen
  90. sqlite3 db test.db -vfs tvfs
  91. sqlite3 db2 test.db -vfs tvfs
  92. execsql {
  93. PRAGMA journal_mode = WAL;
  94. CREATE TABLE t1(a);
  95. } db2
  96. execsql {
  97. INSERT INTO t1 VALUES(1);
  98. INSERT INTO t1 VALUES(2);
  99. INSERT INTO t1 VALUES(3);
  100. INSERT INTO t1 VALUES(4);
  101. SELECT count(a), sum(a) FROM t1;
  102. }
  103. } {4 10}
  104. do_test wal2-1.1 {
  105. execsql { SELECT count(a), sum(a) FROM t1 } db2
  106. } {4 10}
  107. set RECOVER [list \
  108. {0 1 lock exclusive} {1 7 lock exclusive} \
  109. {1 7 unlock exclusive} {0 1 unlock exclusive} \
  110. ]
  111. set READ [list \
  112. {4 1 lock shared} {4 1 unlock shared} \
  113. ]
  114. set INITSLOT [list \
  115. {4 1 lock exclusive} {4 1 unlock exclusive} \
  116. ]
  117. foreach {tn iInsert res wal_index_hdr_mod wal_locks} "
  118. 2 5 {5 15} 0 {$RECOVER $READ}
  119. 3 6 {6 21} 1 {$RECOVER $READ}
  120. 4 7 {7 28} 2 {$RECOVER $READ}
  121. 5 8 {8 36} 3 {$RECOVER $READ}
  122. 6 9 {9 45} 4 {$RECOVER $READ}
  123. 7 10 {10 55} 5 {$RECOVER $READ}
  124. 8 11 {11 66} 6 {$RECOVER $READ}
  125. 9 12 {12 78} 7 {$RECOVER $READ}
  126. 10 13 {13 91} 8 {$RECOVER $READ}
  127. 11 14 {14 105} 9 {$RECOVER $READ}
  128. 12 15 {15 120} -1 {$INITSLOT $READ}
  129. " {
  130. do_test wal2-1.$tn.1 {
  131. execsql { INSERT INTO t1 VALUES($iInsert) }
  132. set ::locks [list]
  133. proc tvfs_cb {method args} {
  134. lappend ::locks [lindex $args 2]
  135. return SQLITE_OK
  136. }
  137. tvfs filter xShmLock
  138. if {$::wal_index_hdr_mod >= 0} {
  139. incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
  140. }
  141. execsql { SELECT count(a), sum(a) FROM t1 } db2
  142. } $res
  143. do_test wal2-1.$tn.2 {
  144. set ::locks
  145. } $wal_locks
  146. }
  147. db close
  148. db2 close
  149. tvfs delete
  150. forcedelete test.db test.db-wal test.db-journal
  151. #-------------------------------------------------------------------------
  152. # This test case is very similar to the previous one, except, after
  153. # the reader reads the corrupt wal-index header, but before it has
  154. # a chance to re-read it under the cover of the RECOVER lock, the
  155. # wal-index header is replaced with a valid, but out-of-date, header.
  156. #
  157. # Because the header checksum looks Ok, the reader does not run recovery,
  158. # it simply drops back to a READ lock and proceeds. But because the
  159. # header is out-of-date, the reader reads the out-of-date snapshot.
  160. #
  161. # After this, the header is corrupted again and the reader is allowed
  162. # to run recovery. This time, it sees an up-to-date snapshot of the
  163. # database file.
  164. #
  165. set WRITER [list 0 1 lock exclusive]
  166. set LOCKS [list \
  167. {0 1 lock exclusive} {0 1 unlock exclusive} \
  168. {4 1 lock exclusive} {4 1 unlock exclusive} \
  169. {4 1 lock shared} {4 1 unlock shared} \
  170. ]
  171. do_test wal2-2.0 {
  172. testvfs tvfs
  173. tvfs script tvfs_cb
  174. tvfs filter xShmOpen
  175. proc tvfs_cb {method args} {
  176. set ::filename [lindex $args 0]
  177. return SQLITE_OK
  178. }
  179. sqlite3 db test.db -vfs tvfs
  180. sqlite3 db2 test.db -vfs tvfs
  181. execsql {
  182. PRAGMA journal_mode = WAL;
  183. CREATE TABLE t1(a);
  184. } db2
  185. execsql {
  186. INSERT INTO t1 VALUES(1);
  187. INSERT INTO t1 VALUES(2);
  188. INSERT INTO t1 VALUES(3);
  189. INSERT INTO t1 VALUES(4);
  190. SELECT count(a), sum(a) FROM t1;
  191. }
  192. } {4 10}
  193. do_test wal2-2.1 {
  194. execsql { SELECT count(a), sum(a) FROM t1 } db2
  195. } {4 10}
  196. foreach {tn iInsert res0 res1 wal_index_hdr_mod} {
  197. 2 5 {4 10} {5 15} 0
  198. 3 6 {5 15} {6 21} 1
  199. 4 7 {6 21} {7 28} 2
  200. 5 8 {7 28} {8 36} 3
  201. 6 9 {8 36} {9 45} 4
  202. 7 10 {9 45} {10 55} 5
  203. 8 11 {10 55} {11 66} 6
  204. 9 12 {11 66} {12 78} 7
  205. } {
  206. tvfs filter xShmLock
  207. do_test wal2-2.$tn.1 {
  208. set oldhdr [set_tvfs_hdr $::filename]
  209. execsql { INSERT INTO t1 VALUES($iInsert) }
  210. execsql { SELECT count(a), sum(a) FROM t1 }
  211. } $res1
  212. do_test wal2-2.$tn.2 {
  213. set ::locks [list]
  214. proc tvfs_cb {method args} {
  215. set lock [lindex $args 2]
  216. lappend ::locks $lock
  217. if {$lock == $::WRITER} {
  218. set_tvfs_hdr $::filename $::oldhdr
  219. }
  220. return SQLITE_OK
  221. }
  222. if {$::wal_index_hdr_mod >= 0} {
  223. incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
  224. }
  225. execsql { SELECT count(a), sum(a) FROM t1 } db2
  226. } $res0
  227. do_test wal2-2.$tn.3 {
  228. set ::locks
  229. } $LOCKS
  230. do_test wal2-2.$tn.4 {
  231. set ::locks [list]
  232. proc tvfs_cb {method args} {
  233. set lock [lindex $args 2]
  234. lappend ::locks $lock
  235. return SQLITE_OK
  236. }
  237. if {$::wal_index_hdr_mod >= 0} {
  238. incr_tvfs_hdr $::filename $::wal_index_hdr_mod 1
  239. }
  240. execsql { SELECT count(a), sum(a) FROM t1 } db2
  241. } $res1
  242. }
  243. db close
  244. db2 close
  245. tvfs delete
  246. forcedelete test.db test.db-wal test.db-journal
  247. if 0 {
  248. #-------------------------------------------------------------------------
  249. # This test case - wal2-3.* - tests the response of the library to an
  250. # SQLITE_BUSY when attempting to obtain a READ or RECOVER lock.
  251. #
  252. # wal2-3.0 - 2: SQLITE_BUSY when obtaining a READ lock
  253. # wal2-3.3 - 6: SQLITE_BUSY when obtaining a RECOVER lock
  254. #
  255. do_test wal2-3.0 {
  256. proc tvfs_cb {method args} {
  257. if {$method == "xShmLock"} {
  258. if {[info exists ::locked]} { return SQLITE_BUSY }
  259. }
  260. return SQLITE_OK
  261. }
  262. proc busyhandler x {
  263. if {$x>3} { unset -nocomplain ::locked }
  264. return 0
  265. }
  266. testvfs tvfs
  267. tvfs script tvfs_cb
  268. sqlite3 db test.db -vfs tvfs
  269. db busy busyhandler
  270. execsql {
  271. PRAGMA journal_mode = WAL;
  272. CREATE TABLE t1(a);
  273. INSERT INTO t1 VALUES(1);
  274. INSERT INTO t1 VALUES(2);
  275. INSERT INTO t1 VALUES(3);
  276. INSERT INTO t1 VALUES(4);
  277. }
  278. set ::locked 1
  279. info exists ::locked
  280. } {1}
  281. do_test wal2-3.1 {
  282. execsql { SELECT count(a), sum(a) FROM t1 }
  283. } {4 10}
  284. do_test wal2-3.2 {
  285. info exists ::locked
  286. } {0}
  287. do_test wal2-3.3 {
  288. proc tvfs_cb {method args} {
  289. if {$method == "xShmLock"} {
  290. if {[info exists ::sabotage]} {
  291. unset -nocomplain ::sabotage
  292. incr_tvfs_hdr [lindex $args 0] 1 1
  293. }
  294. if {[info exists ::locked] && [lindex $args 2] == "RECOVER"} {
  295. return SQLITE_BUSY
  296. }
  297. }
  298. return SQLITE_OK
  299. }
  300. set ::sabotage 1
  301. set ::locked 1
  302. list [info exists ::sabotage] [info exists ::locked]
  303. } {1 1}
  304. do_test wal2-3.4 {
  305. execsql { SELECT count(a), sum(a) FROM t1 }
  306. } {4 10}
  307. do_test wal2-3.5 {
  308. list [info exists ::sabotage] [info exists ::locked]
  309. } {0 0}
  310. db close
  311. tvfs delete
  312. forcedelete test.db test.db-wal test.db-journal
  313. }
  314. #-------------------------------------------------------------------------
  315. # Test that a database connection using a VFS that does not support the
  316. # xShmXXX interfaces cannot open a WAL database.
  317. #
  318. do_test wal2-4.1 {
  319. sqlite3 db test.db
  320. execsql {
  321. PRAGMA auto_vacuum = 0;
  322. PRAGMA journal_mode = WAL;
  323. CREATE TABLE data(x);
  324. INSERT INTO data VALUES('need xShmOpen to see this');
  325. PRAGMA wal_checkpoint;
  326. }
  327. # Three pages in the WAL file at this point: One copy of page 1 and two
  328. # of the root page for table "data".
  329. } {wal 0 3 3}
  330. do_test wal2-4.2 {
  331. db close
  332. testvfs tvfs -noshm 1
  333. sqlite3 db test.db -vfs tvfs
  334. catchsql { SELECT * FROM data }
  335. } {1 {unable to open database file}}
  336. do_test wal2-4.3 {
  337. db close
  338. testvfs tvfs
  339. sqlite3 db test.db -vfs tvfs
  340. catchsql { SELECT * FROM data }
  341. } {0 {{need xShmOpen to see this}}}
  342. db close
  343. tvfs delete
  344. #-------------------------------------------------------------------------
  345. # Test that if a database connection is forced to run recovery before it
  346. # can perform a checkpoint, it does not transition into RECOVER state.
  347. #
  348. # UPDATE: This has now changed. When running a checkpoint, if recovery is
  349. # required the client grabs all exclusive locks (just as it would for a
  350. # recovery performed as a pre-cursor to a normal database transaction).
  351. #
  352. set expected_locks [list]
  353. lappend expected_locks {1 1 lock exclusive} ;# Lock checkpoint
  354. lappend expected_locks {0 1 lock exclusive} ;# Lock writer
  355. lappend expected_locks {2 6 lock exclusive} ;# Lock recovery & all aReadMark[]
  356. lappend expected_locks {2 6 unlock exclusive} ;# Unlock recovery & aReadMark[]
  357. lappend expected_locks {0 1 unlock exclusive} ;# Unlock writer
  358. lappend expected_locks {3 1 lock exclusive} ;# Lock aReadMark[0]
  359. lappend expected_locks {3 1 unlock exclusive} ;# Unlock aReadMark[0]
  360. lappend expected_locks {1 1 unlock exclusive} ;# Unlock checkpoint
  361. do_test wal2-5.1 {
  362. proc tvfs_cb {method args} {
  363. set ::shm_file [lindex $args 0]
  364. if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] }
  365. return $::tvfs_cb_return
  366. }
  367. set tvfs_cb_return SQLITE_OK
  368. testvfs tvfs
  369. tvfs script tvfs_cb
  370. sqlite3 db test.db -vfs tvfs
  371. execsql {
  372. PRAGMA journal_mode = WAL;
  373. CREATE TABLE x(y);
  374. INSERT INTO x VALUES(1);
  375. }
  376. incr_tvfs_hdr $::shm_file 1 1
  377. set ::locks [list]
  378. execsql { PRAGMA wal_checkpoint }
  379. set ::locks
  380. } $expected_locks
  381. db close
  382. tvfs delete
  383. #-------------------------------------------------------------------------
  384. # This block, test cases wal2-6.*, tests the operation of WAL with
  385. # "PRAGMA locking_mode=EXCLUSIVE" set.
  386. #
  387. # wal2-6.1.*: Changing to WAL mode before setting locking_mode=exclusive.
  388. #
  389. # wal2-6.2.*: Changing to WAL mode after setting locking_mode=exclusive.
  390. #
  391. # wal2-6.3.*: Changing back to rollback mode from WAL mode after setting
  392. # locking_mode=exclusive.
  393. #
  394. # wal2-6.4.*: Check that xShmLock calls are omitted in exclusive locking
  395. # mode.
  396. #
  397. # wal2-6.5.*:
  398. #
  399. # wal2-6.6.*: Check that if the xShmLock() to reaquire a WAL read-lock when
  400. # exiting exclusive mode fails (i.e. SQLITE_IOERR), then the
  401. # connection silently remains in exclusive mode.
  402. #
  403. do_test wal2-6.1.1 {
  404. forcedelete test.db test.db-wal test.db-journal
  405. sqlite3 db test.db
  406. execsql {
  407. Pragma Journal_Mode = Wal;
  408. }
  409. } {wal}
  410. do_test wal2-6.1.2 {
  411. execsql { PRAGMA lock_status }
  412. } {main unlocked temp closed}
  413. do_test wal2-6.1.3 {
  414. execsql {
  415. SELECT * FROM sqlite_master;
  416. Pragma Locking_Mode = Exclusive;
  417. }
  418. execsql {
  419. BEGIN;
  420. CREATE TABLE t1(a, b);
  421. INSERT INTO t1 VALUES(1, 2);
  422. COMMIT;
  423. PRAGMA lock_status;
  424. }
  425. } {main exclusive temp closed}
  426. do_test wal2-6.1.4 {
  427. execsql {
  428. PRAGMA locking_mode = normal;
  429. PRAGMA lock_status;
  430. }
  431. } {normal main exclusive temp closed}
  432. do_test wal2-6.1.5 {
  433. execsql {
  434. SELECT * FROM t1;
  435. PRAGMA lock_status;
  436. }
  437. } {1 2 main shared temp closed}
  438. do_test wal2-6.1.6 {
  439. execsql {
  440. INSERT INTO t1 VALUES(3, 4);
  441. PRAGMA lock_status;
  442. }
  443. } {main shared temp closed}
  444. db close
  445. do_test wal2-6.2.1 {
  446. forcedelete test.db test.db-wal test.db-journal
  447. sqlite3 db test.db
  448. execsql {
  449. Pragma Locking_Mode = Exclusive;
  450. Pragma Journal_Mode = Wal;
  451. Pragma Lock_Status;
  452. }
  453. } {exclusive wal main exclusive temp closed}
  454. do_test wal2-6.2.2 {
  455. execsql {
  456. BEGIN;
  457. CREATE TABLE t1(a, b);
  458. INSERT INTO t1 VALUES(1, 2);
  459. COMMIT;
  460. Pragma loCK_STATus;
  461. }
  462. } {main exclusive temp closed}
  463. do_test wal2-6.2.3 {
  464. db close
  465. sqlite3 db test.db
  466. execsql { SELECT * FROM sqlite_master }
  467. execsql { PRAGMA LOCKING_MODE = EXCLUSIVE }
  468. } {exclusive}
  469. do_test wal2-6.2.4 {
  470. execsql {
  471. SELECT * FROM t1;
  472. pragma lock_status;
  473. }
  474. } {1 2 main shared temp closed}
  475. do_test wal2-6.2.5 {
  476. execsql {
  477. INSERT INTO t1 VALUES(3, 4);
  478. pragma lock_status;
  479. }
  480. } {main exclusive temp closed}
  481. do_test wal2-6.2.6 {
  482. execsql {
  483. PRAGMA locking_mode = NORMAL;
  484. pragma lock_status;
  485. }
  486. } {normal main exclusive temp closed}
  487. do_test wal2-6.2.7 {
  488. execsql {
  489. BEGIN IMMEDIATE; COMMIT;
  490. pragma lock_status;
  491. }
  492. } {main shared temp closed}
  493. do_test wal2-6.2.8 {
  494. execsql {
  495. PRAGMA locking_mode = EXCLUSIVE;
  496. BEGIN IMMEDIATE; COMMIT;
  497. PRAGMA locking_mode = NORMAL;
  498. }
  499. execsql {
  500. SELECT * FROM t1;
  501. pragma lock_status;
  502. }
  503. } {1 2 3 4 main shared temp closed}
  504. do_test wal2-6.2.9 {
  505. execsql {
  506. INSERT INTO t1 VALUES(5, 6);
  507. SELECT * FROM t1;
  508. pragma lock_status;
  509. }
  510. } {1 2 3 4 5 6 main shared temp closed}
  511. db close
  512. do_test wal2-6.3.1 {
  513. forcedelete test.db test.db-wal test.db-journal
  514. sqlite3 db test.db
  515. execsql {
  516. PRAGMA journal_mode = WAL;
  517. PRAGMA locking_mode = exclusive;
  518. BEGIN;
  519. CREATE TABLE t1(x);
  520. INSERT INTO t1 VALUES('Chico');
  521. INSERT INTO t1 VALUES('Harpo');
  522. COMMIT;
  523. }
  524. list [file exists test.db-wal] [file exists test.db-journal]
  525. } {1 0}
  526. do_test wal2-6.3.2 {
  527. execsql { PRAGMA journal_mode = DELETE }
  528. file exists test.db-wal
  529. } {0}
  530. do_test wal2-6.3.3 {
  531. execsql { PRAGMA lock_status }
  532. } {main exclusive temp closed}
  533. do_test wal2-6.3.4 {
  534. execsql {
  535. BEGIN;
  536. INSERT INTO t1 VALUES('Groucho');
  537. }
  538. list [file exists test.db-wal] [file exists test.db-journal]
  539. } {0 1}
  540. do_test wal2-6.3.5 {
  541. execsql { PRAGMA lock_status }
  542. } {main exclusive temp closed}
  543. do_test wal2-6.3.6 {
  544. execsql { COMMIT }
  545. list [file exists test.db-wal] [file exists test.db-journal]
  546. } {0 1}
  547. do_test wal2-6.3.7 {
  548. execsql { PRAGMA lock_status }
  549. } {main exclusive temp closed}
  550. db close
  551. # This test - wal2-6.4.* - uses a single database connection and the
  552. # [testvfs] instrumentation to test that xShmLock() is being called
  553. # as expected when a WAL database is used with locking_mode=exclusive.
  554. #
  555. do_test wal2-6.4.1 {
  556. forcedelete test.db test.db-wal test.db-journal
  557. proc tvfs_cb {method args} {
  558. set ::shm_file [lindex $args 0]
  559. if {$method == "xShmLock"} { lappend ::locks [lindex $args 2] }
  560. return "SQLITE_OK"
  561. }
  562. testvfs tvfs
  563. tvfs script tvfs_cb
  564. sqlite3 db test.db -vfs tvfs
  565. set {} {}
  566. } {}
  567. set RECOVERY {
  568. {0 1 lock exclusive} {1 7 lock exclusive}
  569. {1 7 unlock exclusive} {0 1 unlock exclusive}
  570. }
  571. set READMARK0_READ {
  572. {3 1 lock shared} {3 1 unlock shared}
  573. }
  574. set READMARK0_WRITE {
  575. {3 1 lock shared}
  576. {0 1 lock exclusive} {3 1 unlock shared}
  577. {4 1 lock exclusive} {4 1 unlock exclusive} {4 1 lock shared}
  578. {0 1 unlock exclusive} {4 1 unlock shared}
  579. }
  580. set READMARK1_SET {
  581. {4 1 lock exclusive} {4 1 unlock exclusive}
  582. }
  583. set READMARK1_READ {
  584. {4 1 lock shared} {4 1 unlock shared}
  585. }
  586. set READMARK1_WRITE {
  587. {4 1 lock shared}
  588. {0 1 lock exclusive} {0 1 unlock exclusive}
  589. {4 1 unlock shared}
  590. }
  591. foreach {tn sql res expected_locks} {
  592. 2 {
  593. PRAGMA auto_vacuum = 0;
  594. PRAGMA journal_mode = WAL;
  595. BEGIN;
  596. CREATE TABLE t1(x);
  597. INSERT INTO t1 VALUES('Leonard');
  598. INSERT INTO t1 VALUES('Arthur');
  599. COMMIT;
  600. } {wal} {
  601. $RECOVERY
  602. $READMARK0_WRITE
  603. }
  604. 3 {
  605. # This test should do the READMARK1_SET locking to populate the
  606. # aReadMark[1] slot with the current mxFrame value. Followed by
  607. # READMARK1_READ to read the database.
  608. #
  609. SELECT * FROM t1
  610. } {Leonard Arthur} {
  611. $READMARK1_SET
  612. $READMARK1_READ
  613. }
  614. 4 {
  615. # aReadMark[1] is already set to mxFrame. So just READMARK1_READ
  616. # this time, not READMARK1_SET.
  617. #
  618. SELECT * FROM t1 ORDER BY x
  619. } {Arthur Leonard} {
  620. $READMARK1_READ
  621. }
  622. 5 {
  623. PRAGMA locking_mode = exclusive
  624. } {exclusive} { }
  625. 6 {
  626. INSERT INTO t1 VALUES('Julius Henry');
  627. SELECT * FROM t1;
  628. } {Leonard Arthur {Julius Henry}} {
  629. $READMARK1_READ
  630. }
  631. 7 {
  632. INSERT INTO t1 VALUES('Karl');
  633. SELECT * FROM t1;
  634. } {Leonard Arthur {Julius Henry} Karl} { }
  635. 8 {
  636. PRAGMA locking_mode = normal
  637. } {normal} { }
  638. 9 {
  639. SELECT * FROM t1 ORDER BY x
  640. } {Arthur {Julius Henry} Karl Leonard} $READMARK1_READ
  641. 10 { DELETE FROM t1 } {} $READMARK1_WRITE
  642. 11 {
  643. SELECT * FROM t1
  644. } {} {
  645. $READMARK1_SET
  646. $READMARK1_READ
  647. }
  648. } {
  649. set L [list]
  650. foreach el [subst $expected_locks] { lappend L $el }
  651. set S ""
  652. foreach sq [split $sql "\n"] {
  653. set sq [string trim $sq]
  654. if {[string match {#*} $sq]==0} {append S "$sq\n"}
  655. }
  656. set ::locks [list]
  657. do_test wal2-6.4.$tn.1 { execsql $S } $res
  658. do_test wal2-6.4.$tn.2 { set ::locks } $L
  659. }
  660. db close
  661. tvfs delete
  662. do_test wal2-6.5.1 {
  663. sqlite3 db test.db
  664. execsql {
  665. PRAGMA auto_vacuum = 0;
  666. PRAGMA journal_mode = wal;
  667. PRAGMA locking_mode = exclusive;
  668. CREATE TABLE t2(a, b);
  669. PRAGMA wal_checkpoint;
  670. INSERT INTO t2 VALUES('I', 'II');
  671. PRAGMA journal_mode;
  672. }
  673. } {wal exclusive 0 2 2 wal}
  674. do_test wal2-6.5.2 {
  675. execsql {
  676. PRAGMA locking_mode = normal;
  677. INSERT INTO t2 VALUES('III', 'IV');
  678. PRAGMA locking_mode = exclusive;
  679. SELECT * FROM t2;
  680. }
  681. } {normal exclusive I II III IV}
  682. do_test wal2-6.5.3 {
  683. execsql { PRAGMA wal_checkpoint }
  684. } {0 2 2}
  685. db close
  686. proc lock_control {method filename handle spec} {
  687. foreach {start n op type} $spec break
  688. if {$op == "lock"} { return SQLITE_IOERR }
  689. return SQLITE_OK
  690. }
  691. do_test wal2-6.6.1 {
  692. testvfs T
  693. T script lock_control
  694. T filter {}
  695. sqlite3 db test.db -vfs T
  696. execsql { SELECT * FROM sqlite_master }
  697. execsql { PRAGMA locking_mode = exclusive }
  698. execsql { INSERT INTO t2 VALUES('V', 'VI') }
  699. } {}
  700. do_test wal2-6.6.2 {
  701. execsql { PRAGMA locking_mode = normal }
  702. T filter xShmLock
  703. execsql { INSERT INTO t2 VALUES('VII', 'VIII') }
  704. } {}
  705. do_test wal2-6.6.3 {
  706. # At this point the connection should still be in exclusive-mode, even
  707. # though it tried to exit exclusive-mode when committing the INSERT
  708. # statement above. To exit exclusive mode, SQLite has to take a read-lock
  709. # on the WAL file using xShmLock(). Since that call failed, it remains
  710. # in exclusive mode.
  711. #
  712. sqlite3 db2 test.db -vfs T
  713. catchsql { SELECT * FROM t2 } db2
  714. } {1 {database is locked}}
  715. do_test wal2-6.6.2 {
  716. db2 close
  717. T filter {}
  718. execsql { INSERT INTO t2 VALUES('IX', 'X') }
  719. } {}
  720. do_test wal2-6.6.4 {
  721. # This time, we have successfully exited exclusive mode. So the second
  722. # connection can read the database.
  723. sqlite3 db2 test.db -vfs T
  724. catchsql { SELECT * FROM t2 } db2
  725. } {0 {I II III IV V VI VII VIII IX X}}
  726. db close
  727. db2 close
  728. T delete
  729. #-------------------------------------------------------------------------
  730. # Test a theory about the checksum algorithm. Theory was false and this
  731. # test did not provoke a bug.
  732. #
  733. forcedelete test.db test.db-wal test.db-journal
  734. do_test wal2-7.1.1 {
  735. sqlite3 db test.db
  736. execsql {
  737. PRAGMA page_size = 4096;
  738. PRAGMA journal_mode = WAL;
  739. CREATE TABLE t1(a, b);
  740. }
  741. file size test.db
  742. } {4096}
  743. do_test wal2-7.1.2 {
  744. forcecopy test.db test2.db
  745. forcecopy test.db-wal test2.db-wal
  746. hexio_write test2.db-wal 48 FF
  747. } {1}
  748. do_test wal2-7.1.3 {
  749. sqlite3 db2 test2.db
  750. execsql { PRAGMA wal_checkpoint } db2
  751. execsql { SELECT * FROM sqlite_master } db2
  752. } {}
  753. db close
  754. db2 close
  755. forcedelete test.db test.db-wal test.db-journal
  756. do_test wal2-8.1.2 {
  757. sqlite3 db test.db
  758. execsql {
  759. PRAGMA auto_vacuum=OFF;
  760. PRAGMA page_size = 1024;
  761. PRAGMA journal_mode = WAL;
  762. CREATE TABLE t1(x);
  763. INSERT INTO t1 VALUES(zeroblob(8188*1020));
  764. CREATE TABLE t2(y);
  765. PRAGMA wal_checkpoint;
  766. }
  767. execsql {
  768. SELECT rootpage>=8192 FROM sqlite_master WHERE tbl_name = 't2';
  769. }
  770. } {1}
  771. do_test wal2-8.1.3 {
  772. execsql {
  773. PRAGMA cache_size = 10;
  774. CREATE TABLE t3(z);
  775. BEGIN;
  776. INSERT INTO t3 VALUES(randomblob(900));
  777. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  778. INSERT INTO t2 VALUES('hello');
  779. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  780. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  781. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  782. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  783. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  784. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  785. ROLLBACK;
  786. }
  787. execsql {
  788. INSERT INTO t2 VALUES('goodbye');
  789. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  790. INSERT INTO t3 SELECT randomblob(900) FROM t3;
  791. }
  792. } {}
  793. do_test wal2-8.1.4 {
  794. sqlite3 db2 test.db
  795. execsql { SELECT * FROM t2 }
  796. } {goodbye}
  797. db2 close
  798. db close
  799. #-------------------------------------------------------------------------
  800. # Test that even if the checksums for both are valid, if the two copies
  801. # of the wal-index header in the wal-index do not match, the client
  802. # runs (or at least tries to run) database recovery.
  803. #
  804. #
  805. proc get_name {method args} { set ::filename [lindex $args 0] ; tvfs filter {} }
  806. testvfs tvfs
  807. tvfs script get_name
  808. tvfs filter xShmOpen
  809. forcedelete test.db test.db-wal test.db-journal
  810. do_test wal2-9.1 {
  811. sqlite3 db test.db -vfs tvfs
  812. execsql {
  813. PRAGMA journal_mode = WAL;
  814. CREATE TABLE x(y);
  815. INSERT INTO x VALUES('Barton');
  816. INSERT INTO x VALUES('Deakin');
  817. }
  818. # Set $wih(1) to the contents of the wal-index header after
  819. # the frames associated with the first two rows in table 'x' have
  820. # been inserted. Then insert one more row and set $wih(2)
  821. # to the new value of the wal-index header.
  822. #
  823. # If the $wih(1) is written into the wal-index before running
  824. # a read operation, the client will see only the first two rows. If
  825. # $wih(2) is written into the wal-index, the client will see
  826. # three rows. If an invalid header is written into the wal-index, then
  827. # the client will run recovery and see three rows.
  828. #
  829. set wih(1) [set_tvfs_hdr $::filename]
  830. execsql { INSERT INTO x VALUES('Watson') }
  831. set wih(2) [set_tvfs_hdr $::filename]
  832. sqlite3 db2 test.db -vfs tvfs
  833. execsql { SELECT * FROM x } db2
  834. } {Barton Deakin Watson}
  835. foreach {tn hdr1 hdr2 res} [list \
  836. 3 $wih(1) $wih(1) {Barton Deakin} \
  837. 4 $wih(1) $wih(2) {Barton Deakin Watson} \
  838. 5 $wih(2) $wih(1) {Barton Deakin Watson} \
  839. 6 $wih(2) $wih(2) {Barton Deakin Watson} \
  840. 7 $wih(1) $wih(1) {Barton Deakin} \
  841. 8 {0 0 0 0 0 0 0 0 0 0 0 0} {0 0 0 0 0 0 0 0 0 0 0 0} {Barton Deakin Watson}
  842. ] {
  843. do_test wal2-9.$tn {
  844. set_tvfs_hdr $::filename $hdr1 $hdr2
  845. execsql { SELECT * FROM x } db2
  846. } $res
  847. }
  848. db2 close
  849. db close
  850. #-------------------------------------------------------------------------
  851. # This block of tests - wal2-10.* - focus on the libraries response to
  852. # new versions of the wal or wal-index formats.
  853. #
  854. # wal2-10.1.*: Test that the library refuses to "recover" a new WAL
  855. # format.
  856. #
  857. # wal2-10.2.*: Test that the library refuses to read or write a database
  858. # if the wal-index version is newer than it understands.
  859. #
  860. # At time of writing, the only versions of the wal and wal-index formats
  861. # that exist are versions 3007000 (corresponding to SQLite version 3.7.0,
  862. # the first version of SQLite to feature wal mode).
  863. #
  864. do_test wal2-10.1.1 {
  865. faultsim_delete_and_reopen
  866. execsql {
  867. PRAGMA journal_mode = WAL;
  868. CREATE TABLE t1(a, b);
  869. PRAGMA wal_checkpoint;
  870. INSERT INTO t1 VALUES(1, 2);
  871. INSERT INTO t1 VALUES(3, 4);
  872. }
  873. faultsim_save_and_close
  874. } {}
  875. do_test wal2-10.1.2 {
  876. faultsim_restore_and_reopen
  877. execsql { SELECT * FROM t1 }
  878. } {1 2 3 4}
  879. do_test wal2-10.1.3 {
  880. faultsim_restore_and_reopen
  881. set hdr [wal_set_walhdr test.db-wal]
  882. lindex $hdr 1
  883. } {3007000}
  884. do_test wal2-10.1.4 {
  885. lset hdr 1 3007001
  886. wal_set_walhdr test.db-wal $hdr
  887. catchsql { SELECT * FROM t1 }
  888. } {1 {unable to open database file}}
  889. testvfs tvfs -default 1
  890. do_test wal2-10.2.1 {
  891. faultsim_restore_and_reopen
  892. execsql { SELECT * FROM t1 }
  893. } {1 2 3 4}
  894. do_test wal2-10.2.2 {
  895. set hdr [set_tvfs_hdr $::filename]
  896. lindex $hdr 0
  897. } {3007000}
  898. do_test wal2-10.2.3 {
  899. lset hdr 0 3007001
  900. wal_fix_walindex_cksum hdr
  901. set_tvfs_hdr $::filename $hdr
  902. catchsql { SELECT * FROM t1 }
  903. } {1 {unable to open database file}}
  904. db close
  905. tvfs delete
  906. #-------------------------------------------------------------------------
  907. # This block of tests - wal2-11.* - tests that it is not possible to put
  908. # the library into an infinite loop by presenting it with a corrupt
  909. # hash table (one that appears to contain a single chain of infinite
  910. # length).
  911. #
  912. # wal2-11.1.*: While reading the hash-table.
  913. #
  914. # wal2-11.2.*: While writing the hash-table.
  915. #
  916. testvfs tvfs -default 1
  917. do_test wal2-11.0 {
  918. faultsim_delete_and_reopen
  919. execsql {
  920. PRAGMA journal_mode = WAL;
  921. CREATE TABLE t1(a, b, c);
  922. INSERT INTO t1 VALUES(1, 2, 3);
  923. INSERT INTO t1 VALUES(4, 5, 6);
  924. INSERT INTO t1 VALUES(7, 8, 9);
  925. SELECT * FROM t1;
  926. }
  927. } {wal 1 2 3 4 5 6 7 8 9}
  928. do_test wal2-11.1.1 {
  929. sqlite3 db2 test.db
  930. execsql { SELECT name FROM sqlite_master } db2
  931. } {t1}
  932. if {$::tcl_version>=8.5} {
  933. # Set all zeroed slots in the first hash table to invalid values.
  934. #
  935. set blob [string range [tvfs shm $::filename] 0 16383]
  936. set I [string range [tvfs shm $::filename] 16384 end]
  937. binary scan $I t* L
  938. set I [list]
  939. foreach p $L {
  940. lappend I [expr $p ? $p : 400]
  941. }
  942. append blob [binary format t* $I]
  943. tvfs shm $::filename $blob
  944. do_test wal2-11.2 {
  945. catchsql { INSERT INTO t1 VALUES(10, 11, 12) }
  946. } {1 {database disk image is malformed}}
  947. # Fill up the hash table on the first page of shared memory with 0x55 bytes.
  948. #
  949. set blob [string range [tvfs shm $::filename] 0 16383]
  950. append blob [string repeat [binary format c 55] 16384]
  951. tvfs shm $::filename $blob
  952. do_test wal2-11.3 {
  953. catchsql { SELECT * FROM t1 } db2
  954. } {1 {database disk image is malformed}}
  955. }
  956. db close
  957. db2 close
  958. tvfs delete
  959. #-------------------------------------------------------------------------
  960. # If a connection is required to create a WAL or SHM file, it creates
  961. # the new files with the same file-system permissions as the database
  962. # file itself. Test this.
  963. #
  964. if {$::tcl_platform(platform) == "unix"} {
  965. faultsim_delete_and_reopen
  966. # Changed on 2012-02-13: umask is deliberately ignored for -wal files.
  967. #set umask [exec /bin/sh -c umask]
  968. set umask 0
  969. do_test wal2-12.1 {
  970. sqlite3 db test.db
  971. execsql {
  972. CREATE TABLE tx(y, z);
  973. PRAGMA journal_mode = WAL;
  974. }
  975. db close
  976. list [file exists test.db-wal] [file exists test.db-shm]
  977. } {0 0}
  978. foreach {tn permissions} {
  979. 1 00644
  980. 2 00666
  981. 3 00600
  982. 4 00755
  983. } {
  984. set effective [format %.5o [expr $permissions & ~$umask]]
  985. do_test wal2-12.2.$tn.1 {
  986. file attributes test.db -permissions $permissions
  987. file attributes test.db -permissions
  988. } $permissions
  989. do_test wal2-12.2.$tn.2 {
  990. list [file exists test.db-wal] [file exists test.db-shm]
  991. } {0 0}
  992. do_test wal2-12.2.$tn.3 {
  993. sqlite3 db test.db
  994. execsql { INSERT INTO tx DEFAULT VALUES }
  995. list [file exists test.db-wal] [file exists test.db-shm]
  996. } {1 1}
  997. do_test wal2-12.2.$tn.4 {
  998. list [file attr test.db-wal -perm] [file attr test.db-shm -perm]
  999. } [list $effective $effective]
  1000. do_test wal2-12.2.$tn.5 {
  1001. db close
  1002. list [file exists test.db-wal] [file exists test.db-shm]
  1003. } {0 0}
  1004. }
  1005. }
  1006. #-------------------------------------------------------------------------
  1007. # Test the libraries response to discovering that one or more of the
  1008. # database, wal or shm files cannot be opened, or can only be opened
  1009. # read-only.
  1010. #
  1011. if {$::tcl_platform(platform) == "unix"} {
  1012. proc perm {} {
  1013. set L [list]
  1014. foreach f {test.db test.db-wal test.db-shm} {
  1015. if {[file exists $f]} {
  1016. lappend L [file attr $f -perm]
  1017. } else {
  1018. lappend L {}
  1019. }
  1020. }
  1021. set L
  1022. }
  1023. faultsim_delete_and_reopen
  1024. execsql {
  1025. PRAGMA journal_mode = WAL;
  1026. CREATE TABLE t1(a, b);
  1027. PRAGMA wal_checkpoint;
  1028. INSERT INTO t1 VALUES('3.14', '2.72');
  1029. }
  1030. do_test wal2-13.1.1 {
  1031. list [file exists test.db-shm] [file exists test.db-wal]
  1032. } {1 1}
  1033. faultsim_save_and_close
  1034. foreach {tn db_perm wal_perm shm_perm can_open can_read can_write} {
  1035. 2 00644 00644 00644 1 1 1
  1036. 3 00644 00400 00644 1 1 0
  1037. 4 00644 00644 00400 1 0 0
  1038. 5 00400 00644 00644 1 1 0
  1039. 7 00644 00000 00644 1 0 0
  1040. 8 00644 00644 00000 1 0 0
  1041. 9 00000 00644 00644 0 0 0
  1042. } {
  1043. faultsim_restore
  1044. do_test wal2-13.$tn.1 {
  1045. file attr test.db -perm $db_perm
  1046. file attr test.db-wal -perm $wal_perm
  1047. file attr test.db-shm -perm $shm_perm
  1048. set L [file attr test.db -perm]
  1049. lappend L [file attr test.db-wal -perm]
  1050. lappend L [file attr test.db-shm -perm]
  1051. } [list $db_perm $wal_perm $shm_perm]
  1052. # If $can_open is true, then it should be possible to open a database
  1053. # handle. Otherwise, if $can_open is 0, attempting to open the db
  1054. # handle throws an "unable to open database file" exception.
  1055. #
  1056. set r(1) {0 ok}
  1057. set r(0) {1 {unable to open database file}}
  1058. do_test wal2-13.$tn.2 {
  1059. list [catch {sqlite3 db test.db ; set {} ok} msg] $msg
  1060. } $r($can_open)
  1061. if {$can_open} {
  1062. # If $can_read is true, then the client should be able to read from
  1063. # the database file. If $can_read is false, attempting to read should
  1064. # throw the "unable to open database file" exception.
  1065. #
  1066. set a(0) {1 {unable to open database file}}
  1067. set a(1) {0 {3.14 2.72}}
  1068. do_test wal2-13.$tn.3 {
  1069. catchsql { SELECT * FROM t1 }
  1070. } $a($can_read)
  1071. # Now try to write to the db file. If the client can read but not
  1072. # write, then it should throw the familiar "unable to open db file"
  1073. # exception. If it can read but not write, the exception should
  1074. # be "attempt to write a read only database".
  1075. #
  1076. # If the client can read and write, the operation should succeed.
  1077. #
  1078. set b(0,0) {1 {unable to open database file}}
  1079. set b(1,0) {1 {attempt to write a readonly database}}
  1080. set b(1,1) {0 {}}
  1081. do_test wal2-13.$tn.4 {
  1082. catchsql { INSERT INTO t1 DEFAULT VALUES }
  1083. } $b($can_read,$can_write)
  1084. }
  1085. catch { db close }
  1086. }
  1087. }
  1088. #-------------------------------------------------------------------------
  1089. # Test that "PRAGMA checkpoint_fullsync" appears to be working.
  1090. #
  1091. foreach {tn sql reslist} {
  1092. 1 { } {10 0 4 0 6 0}
  1093. 2 { PRAGMA checkpoint_fullfsync = 1 } {10 4 4 2 6 2}
  1094. 3 { PRAGMA checkpoint_fullfsync = 0 } {10 0 4 0 6 0}
  1095. } {
  1096. faultsim_delete_and_reopen
  1097. execsql {PRAGMA auto_vacuum = 0}
  1098. execsql $sql
  1099. do_execsql_test wal2-14.$tn.0 { PRAGMA page_size = 4096 } {}
  1100. do_execsql_test wal2-14.$tn.1 { PRAGMA journal_mode = WAL } {wal}
  1101. set sqlite_sync_count 0
  1102. set sqlite_fullsync_count 0
  1103. do_execsql_test wal2-14.$tn.2 {
  1104. PRAGMA wal_autocheckpoint = 10;
  1105. CREATE TABLE t1(a, b); -- 2 wal syncs
  1106. INSERT INTO t1 VALUES(1, 2); -- 2 wal sync
  1107. PRAGMA wal_checkpoint; -- 1 wal sync, 1 db sync
  1108. BEGIN;
  1109. INSERT INTO t1 VALUES(3, 4);
  1110. INSERT INTO t1 VALUES(5, 6);
  1111. COMMIT; -- 2 wal sync
  1112. PRAGMA wal_checkpoint; -- 1 wal sync, 1 db sync
  1113. } {10 0 3 3 0 1 1}
  1114. do_test wal2-14.$tn.3 {
  1115. cond_incr_sync_count 1
  1116. list $sqlite_sync_count $sqlite_fullsync_count
  1117. } [lrange $reslist 0 1]
  1118. set sqlite_sync_count 0
  1119. set sqlite_fullsync_count 0
  1120. do_test wal2-14.$tn.4 {
  1121. execsql { INSERT INTO t1 VALUES(7, zeroblob(12*4096)) }
  1122. list $sqlite_sync_count $sqlite_fullsync_count
  1123. } [lrange $reslist 2 3]
  1124. set sqlite_sync_count 0
  1125. set sqlite_fullsync_count 0
  1126. do_test wal2-14.$tn.5 {
  1127. execsql { PRAGMA wal_autocheckpoint = 1000 }
  1128. execsql { INSERT INTO t1 VALUES(9, 10) }
  1129. execsql { INSERT INTO t1 VALUES(11, 12) }
  1130. execsql { INSERT INTO t1 VALUES(13, 14) }
  1131. db close
  1132. list $sqlite_sync_count $sqlite_fullsync_count
  1133. } [lrange $reslist 4 5]
  1134. }
  1135. catch { db close }
  1136. # PRAGMA checkpoint_fullsync
  1137. # PRAGMA fullfsync
  1138. # PRAGMA synchronous
  1139. #
  1140. foreach {tn settings restart_sync commit_sync ckpt_sync} {
  1141. 1 {0 0 off} {0 0} {0 0} {0 0}
  1142. 2 {0 0 normal} {1 0} {0 0} {2 0}
  1143. 3 {0 0 full} {2 0} {1 0} {2 0}
  1144. 4 {0 1 off} {0 0} {0 0} {0 0}
  1145. 5 {0 1 normal} {0 1} {0 0} {0 2}
  1146. 6 {0 1 full} {0 2} {0 1} {0 2}
  1147. 7 {1 0 off} {0 0} {0 0} {0 0}
  1148. 8 {1 0 normal} {1 0} {0 0} {0 2}
  1149. 9 {1 0 full} {2 0} {1 0} {0 2}
  1150. 10 {1 1 off} {0 0} {0 0} {0 0}
  1151. 11 {1 1 normal} {0 1} {0 0} {0 2}
  1152. 12 {1 1 full} {0 2} {0 1} {0 2}
  1153. } {
  1154. forcedelete test.db
  1155. testvfs tvfs -default 1
  1156. tvfs filter xSync
  1157. tvfs script xSyncCb
  1158. proc xSyncCb {method file fileid flags} {
  1159. incr ::sync($flags)
  1160. }
  1161. sqlite3 db test.db
  1162. do_execsql_test 15.$tn.1 "
  1163. PRAGMA page_size = 4096;
  1164. CREATE TABLE t1(x);
  1165. PRAGMA wal_autocheckpoint = OFF;
  1166. PRAGMA journal_mode = WAL;
  1167. PRAGMA checkpoint_fullfsync = [lindex $settings 0];
  1168. PRAGMA fullfsync = [lindex $settings 1];
  1169. PRAGMA synchronous = [lindex $settings 2];
  1170. " {0 wal}
  1171. if { $tn==2} breakpoint
  1172. do_test 15.$tn.2 {
  1173. set sync(normal) 0
  1174. set sync(full) 0
  1175. execsql { INSERT INTO t1 VALUES('abc') }
  1176. list $::sync(normal) $::sync(full)
  1177. } $restart_sync
  1178. do_test 15.$tn.3 {
  1179. set sync(normal) 0
  1180. set sync(full) 0
  1181. execsql { INSERT INTO t1 VALUES('abc') }
  1182. list $::sync(normal) $::sync(full)
  1183. } $commit_sync
  1184. do_test 15.$tn.4 {
  1185. set sync(normal) 0
  1186. set sync(full) 0
  1187. execsql { INSERT INTO t1 VALUES('def') }
  1188. list $::sync(normal) $::sync(full)
  1189. } $commit_sync
  1190. do_test 15.$tn.5 {
  1191. set sync(normal) 0
  1192. set sync(full) 0
  1193. execsql { PRAGMA wal_checkpoint }
  1194. list $::sync(normal) $::sync(full)
  1195. } $ckpt_sync
  1196. db close
  1197. tvfs delete
  1198. }
  1199. finish_test