superlock.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # 2010 November 19
  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. #
  12. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. source $testdir/lock_common.tcl
  15. set testprefix superlock
  16. # Test organization:
  17. #
  18. # 1.*: Test superlock on a rollback database. Test that once the db is
  19. # superlocked, it is not possible for a second client to read from
  20. # it.
  21. #
  22. # 2.*: Test superlock on a WAL database with zero frames in the WAL file.
  23. # Test that once the db is superlocked, it is not possible to read,
  24. # write or checkpoint the db.
  25. #
  26. # 3.*: As 2.*, for WAL databases with one or more frames in the WAL.
  27. #
  28. # 4.*: As 2.*, for WAL databases with one or more checkpointed frames
  29. # in the WAL.
  30. #
  31. # 5.*: Test that a call to sqlite3demo_superlock() uses the busy handler
  32. # correctly to wait for existing clients to clear on a WAL database.
  33. # And returns SQLITE_BUSY if no busy handler is defined or the busy
  34. # handler returns 0 before said clients relinquish their locks.
  35. #
  36. # 6.*: Test that if a superlocked WAL database is overwritten, existing
  37. # clients run the recovery to build the new wal-index after the
  38. # superlock is released.
  39. #
  40. #
  41. do_execsql_test 1.1 {
  42. CREATE TABLE t1(a, b);
  43. INSERT INTO t1 VALUES(1, 2);
  44. PRAGMA journal_mode = DELETE;
  45. } {delete}
  46. ifcapable !wal {
  47. finish_test
  48. return
  49. }
  50. do_test 1.2 { sqlite3demo_superlock unlock test.db } {unlock}
  51. do_catchsql_test 1.3 { SELECT * FROM t1 } {1 {database is locked}}
  52. do_test 1.4 { unlock } {}
  53. do_execsql_test 2.1 {
  54. INSERT INTO t1 VALUES(3, 4);
  55. PRAGMA journal_mode = WAL;
  56. } {wal}
  57. do_test 2.2 { sqlite3demo_superlock unlock test.db } {unlock}
  58. do_catchsql_test 2.3 { SELECT * FROM t1 } {1 {database is locked}}
  59. do_catchsql_test 2.4 { INSERT INTO t1 VALUES(5, 6)} {1 {database is locked}}
  60. do_catchsql_test 2.5 { PRAGMA wal_checkpoint } {0 {1 -1 -1}}
  61. do_test 2.6 { unlock } {}
  62. do_execsql_test 3.1 { INSERT INTO t1 VALUES(3, 4) }
  63. do_test 3.2 { sqlite3demo_superlock unlock test.db } {unlock}
  64. do_catchsql_test 3.3 { SELECT * FROM t1 } {1 {database is locked}}
  65. do_catchsql_test 3.4 { INSERT INTO t1 VALUES(5, 6)} {1 {database is locked}}
  66. do_catchsql_test 3.5 { PRAGMA wal_checkpoint } {0 {1 -1 -1}}
  67. do_test 3.6 { unlock } {}
  68. # At this point the WAL file consists of a single frame only - written
  69. # by test case 3.1. If the ZERO_DAMAGE flag were not set, it would consist
  70. # of two frames - the frame written by 3.1 and a padding frame.
  71. do_execsql_test 4.1 { PRAGMA wal_checkpoint } {0 1 1}
  72. do_test 4.2 { sqlite3demo_superlock unlock test.db } {unlock}
  73. do_catchsql_test 4.3 { SELECT * FROM t1 } {1 {database is locked}}
  74. do_catchsql_test 4.4 { INSERT INTO t1 VALUES(5, 6)} {1 {database is locked}}
  75. do_catchsql_test 4.5 { PRAGMA wal_checkpoint } {0 {1 -1 -1}}
  76. do_test 4.6 { unlock } {}
  77. do_multiclient_test tn {
  78. proc busyhandler {x} {
  79. switch -- $x {
  80. 1 { sql1 "COMMIT" }
  81. 2 { sql2 "COMMIT" }
  82. 3 { sql3 "COMMIT" }
  83. }
  84. lappend ::busylist $x
  85. return 1
  86. }
  87. set ::busylist [list]
  88. do_test 5.$tn.1 {
  89. sql1 {
  90. CREATE TABLE t1(a, b);
  91. PRAGMA journal_mode = WAL;
  92. INSERT INTO t1 VALUES(1, 2);
  93. }
  94. } {wal}
  95. do_test 5.$tn.2 {
  96. sql1 { BEGIN ; SELECT * FROM t1 }
  97. sql2 { BEGIN ; INSERT INTO t1 VALUES(3, 4) }
  98. sql3 { BEGIN ; SELECT * FROM t1 }
  99. } {1 2}
  100. do_test 5.$tn.3 {
  101. set ::busylist [list]
  102. sqlite3demo_superlock unlock test.db "" busyhandler
  103. set ::busylist
  104. } {0 1 2 3}
  105. do_test 5.$tn.4 { csql2 { SELECT * FROM t1 } } {1 {database is locked}}
  106. do_test 5.$tn.5 {
  107. csql3 { INSERT INTO t1 VALUES(5, 6) }
  108. } {1 {database is locked}}
  109. do_test 5.$tn.6 { csql1 "PRAGMA wal_checkpoint" } {0 {1 -1 -1}}
  110. do_test 5.$tn.7 { unlock } {}
  111. do_test 5.$tn.8 {
  112. sql1 { BEGIN ; SELECT * FROM t1 }
  113. sql2 { BEGIN ; INSERT INTO t1 VALUES(5, 6) }
  114. sql3 { BEGIN ; SELECT * FROM t1 }
  115. } {1 2 3 4}
  116. do_test 5.$tn.9 {
  117. list [catch {sqlite3demo_superlock unlock test.db} msg] $msg
  118. } {1 {database is locked}}
  119. do_test 5.$tn.10 {
  120. sql1 COMMIT
  121. list [catch {sqlite3demo_superlock unlock test.db} msg] $msg
  122. } {1 {database is locked}}
  123. do_test 5.$tn.11 {
  124. sql2 COMMIT
  125. list [catch {sqlite3demo_superlock unlock test.db} msg] $msg
  126. } {1 {database is locked}}
  127. do_test 5.$tn.12 {
  128. sql3 COMMIT
  129. list [catch {sqlite3demo_superlock unlock test.db} msg] $msg
  130. } {0 unlock}
  131. unlock
  132. do_test 5.$tn.13 { sql1 { SELECT * FROM t1 } } {1 2 3 4 5 6}
  133. do_test 5.$tn.14 { sql2 { SELECT * FROM t1 } } {1 2 3 4 5 6}
  134. do_test 5.$tn.15 { sqlite3demo_superlock unlock test.db } {unlock}
  135. do_test 5.$tn.16 { unlock } {}
  136. do_test 5.$tn.17 { sql2 { SELECT * FROM t1 } } {1 2 3 4 5 6}
  137. do_test 5.$tn.18 { sql1 { SELECT * FROM t1 } } {1 2 3 4 5 6}
  138. do_test 5.$tn.19 { sql2 { SELECT * FROM t1 } } {1 2 3 4 5 6}
  139. }
  140. proc read_content {file} {
  141. if {[file exists $file]==0} {return ""}
  142. set fd [open $file]
  143. fconfigure $fd -encoding binary -translation binary
  144. set content [read $fd]
  145. close $fd
  146. return $content
  147. }
  148. proc write_content {file content} {
  149. set fd [open $file w+]
  150. fconfigure $fd -encoding binary -translation binary
  151. puts -nonewline $fd $content
  152. close $fd
  153. }
  154. # Both $file1 and $file2 are database files. This function takes a
  155. # superlock on each, then exchanges the content of the two files (i.e.
  156. # overwrites $file1 with the initial contents of $file2, and overwrites
  157. # $file2 with the initial contents of $file1). The contents of any WAL
  158. # file is also exchanged.
  159. #
  160. proc db_swap {file1 file2} {
  161. sqlite3demo_superlock unlock1 $file1
  162. sqlite3demo_superlock unlock2 $file2
  163. set db1 [read_content $file1]
  164. set db2 [read_content $file2]
  165. write_content $file1 $db2
  166. write_content $file2 $db1
  167. set wal1 [read_content ${file1}-wal]
  168. set wal2 [read_content ${file2}-wal]
  169. write_content ${file1}-wal $wal2
  170. write_content ${file2}-wal $wal1
  171. unlock1
  172. unlock2
  173. }
  174. forcedelete test.db
  175. sqlite3 db test.db
  176. do_execsql_test 6.1 {
  177. ATTACH 'test.db2' AS aux;
  178. PRAGMA aux.journal_mode = wal;
  179. CREATE TABLE aux.t2(x, y);
  180. INSERT INTO aux.t2 VALUES('a', 'b');
  181. PRAGMA schema_version = 450;
  182. DETACH aux;
  183. PRAGMA main.journal_mode = wal;
  184. CREATE TABLE t1(a, b);
  185. INSERT INTO t1 VALUES(1, 2);
  186. INSERT INTO t1 VALUES(3, 4);
  187. SELECT * FROM t1;
  188. } {wal wal 1 2 3 4}
  189. db_swap test.db2 test.db
  190. do_catchsql_test 6.2 { SELECT * FROM t1 } {1 {no such table: t1}}
  191. do_catchsql_test 6.3 { SELECT * FROM t2 } {0 {a b}}
  192. db_swap test.db2 test.db
  193. do_catchsql_test 6.4 { SELECT * FROM t1 } {0 {1 2 3 4}}
  194. do_catchsql_test 6.5 { SELECT * FROM t2 } {1 {no such table: t2}}
  195. do_execsql_test 6.6 { PRAGMA wal_checkpoint } {0 0 0}
  196. db_swap test.db2 test.db
  197. do_catchsql_test 6.7 { SELECT * FROM t1 } {1 {no such table: t1}}
  198. do_catchsql_test 6.8 { SELECT * FROM t2 } {0 {a b}}
  199. db_swap test.db2 test.db
  200. do_catchsql_test 6.9 { SELECT * FROM t1 } {0 {1 2 3 4}}
  201. do_catchsql_test 6.10 { SELECT * FROM t2 } {1 {no such table: t2}}
  202. do_execsql_test 6.11 {
  203. PRAGMA journal_mode = delete;
  204. PRAGMA page_size = 512;
  205. VACUUM;
  206. PRAGMA journal_mode = wal;
  207. INSERT INTO t1 VALUES(5, 6);
  208. } {delete wal}
  209. db_swap test.db2 test.db
  210. do_catchsql_test 6.12 { SELECT * FROM t1 } {1 {no such table: t1}}
  211. do_catchsql_test 6.13 { SELECT * FROM t2 } {0 {a b}}
  212. db_swap test.db2 test.db
  213. do_catchsql_test 6.14 { SELECT * FROM t1 } {0 {1 2 3 4 5 6}}
  214. do_catchsql_test 6.15 { SELECT * FROM t2 } {1 {no such table: t2}}
  215. finish_test