1
0

walnoshm.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # 2010 November 1
  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 that WAL databases may be accessed without
  13. # using the xShm primitives if the connection is in exclusive-mode.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. set testprefix walnoshm
  18. ifcapable !wal {finish_test ; return }
  19. db close
  20. testvfs tvfsshm
  21. testvfs tvfs -default 1 -iversion 1
  22. sqlite3 db test.db
  23. #--------------------------------------------------------------------------
  24. # Test that when using a version 1 VFS, a database can only be converted
  25. # to WAL mode after setting locking_mode=EXCLUSIVE. Also, test that if a
  26. # WAL database is opened using heap-memory for the WAL index, the connection
  27. # cannot change back to locking_mode=NORMAL while the database is still in
  28. # WAL mode.
  29. #
  30. do_execsql_test 1.1 {
  31. CREATE TABLE t1(x, y);
  32. INSERT INTO t1 VALUES(1, 2);
  33. }
  34. do_execsql_test 1.2 {
  35. PRAGMA journal_mode = WAL;
  36. SELECT * FROM t1;
  37. } {delete 1 2}
  38. do_test 1.3 { file exists test.db-wal } {0}
  39. do_execsql_test 1.4 {
  40. PRAGMA locking_mode = exclusive;
  41. PRAGMA journal_mode = WAL;
  42. SELECT * FROM t1;
  43. } {exclusive wal 1 2}
  44. do_test 1.5 { file exists test.db-wal } {1}
  45. do_execsql_test 1.6 { INSERT INTO t1 VALUES(3, 4) }
  46. do_execsql_test 1.7 {
  47. PRAGMA locking_mode = normal;
  48. } {exclusive}
  49. do_execsql_test 1.8 {
  50. PRAGMA journal_mode = delete;
  51. PRAGMA main.locking_mode;
  52. } {delete exclusive}
  53. do_execsql_test 1.9 {
  54. PRAGMA locking_mode = normal;
  55. } {normal}
  56. do_execsql_test 1.10 {
  57. SELECT * FROM t1;
  58. } {1 2 3 4}
  59. do_test 1.11 { file exists test.db-wal } {0}
  60. #-------------------------------------------------------------------------
  61. #
  62. # 2.1.*: Test that a connection using a version 1 VFS can open a WAL database
  63. # and convert it to rollback mode if it is set to use
  64. # locking_mode=exclusive.
  65. #
  66. # 2.2.*: Test that if the exclusive lock cannot be obtained while attempting
  67. # the above, the operation fails and the WAL file is not opened.
  68. #
  69. do_execsql_test 2.1.1 {
  70. CREATE TABLE t2(x, y);
  71. INSERT INTO t2 VALUES('a', 'b');
  72. INSERT INTO t2 VALUES('c', 'd');
  73. }
  74. do_execsql_test 2.1.2 {
  75. PRAGMA locking_mode = exclusive;
  76. PRAGMA journal_mode = WAL;
  77. INSERT INTO t2 VALUES('e', 'f');
  78. INSERT INTO t2 VALUES('g', 'h');
  79. } {exclusive wal}
  80. do_test 2.1.3 {
  81. forcecopy test.db test2.db
  82. forcecopy test.db-wal test2.db-wal
  83. sqlite3 db2 test2.db
  84. catchsql { SELECT * FROM t2 } db2
  85. } {1 {unable to open database file}}
  86. do_test 2.1.4 {
  87. catchsql { PRAGMA journal_mode = delete } db2
  88. } {1 {unable to open database file}}
  89. do_test 2.1.5 {
  90. execsql {
  91. PRAGMA locking_mode = exclusive;
  92. PRAGMA journal_mode = delete;
  93. SELECT * FROM t2;
  94. } db2
  95. } {exclusive delete a b c d e f g h}
  96. do_test 2.2.1 {
  97. forcecopy test.db test2.db
  98. forcecopy test.db-wal test2.db-wal
  99. sqlite3 db3 test2.db -vfs tvfsshm
  100. sqlite3 db2 test2.db
  101. execsql { SELECT * FROM t2 } db3
  102. } {a b c d e f g h}
  103. do_test 2.2.2 {
  104. execsql { PRAGMA locking_mode = exclusive } db2
  105. catchsql { PRAGMA journal_mode = delete } db2
  106. } {1 {database is locked}}
  107. do_test 2.2.3 {
  108. # This is to test that [db2] is not holding a PENDING lock (which can
  109. # happen when an attempt to obtain an EXCLUSIVE lock fails).
  110. sqlite3 db4 test2.db -vfs tvfsshm
  111. execsql { SELECT * FROM t2 } db4
  112. } {a b c d e f g h}
  113. do_test 2.2.4 {
  114. catchsql { SELECT * FROM t2 } db2
  115. } {1 {database is locked}}
  116. do_test 2.2.5 {
  117. db4 close
  118. sqlite3 db4 test2.db -vfs tvfsshm
  119. execsql { SELECT * FROM t2 } db4
  120. } {a b c d e f g h}
  121. do_test 2.2.6 {
  122. db3 close
  123. db4 close
  124. execsql { SELECT * FROM t2 } db2
  125. } {a b c d e f g h}
  126. db2 close
  127. db close
  128. #-------------------------------------------------------------------------
  129. #
  130. # 3.1: Test that if locking_mode=EXCLUSIVE is set after the wal file is
  131. # opened, it is possible to drop back to locking_mode=NORMAL.
  132. #
  133. # 3.2: Test that if locking_mode=EXCLUSIVE is set before the wal file is
  134. # opened, it is not.
  135. #
  136. do_test 3.1 {
  137. sqlite3 db test.db -vfs tvfsshm
  138. execsql {
  139. SELECT * FROM t1;
  140. PRAGMA locking_mode = EXCLUSIVE;
  141. INSERT INTO t1 VALUES(5, 6);
  142. PRAGMA locking_mode = NORMAL;
  143. INSERT INTO t1 VALUES(7, 8);
  144. }
  145. sqlite3 db2 test.db -vfs tvfsshm
  146. execsql { SELECT * FROM t1 } db2
  147. } {1 2 3 4 5 6 7 8}
  148. db close
  149. db2 close
  150. do_test 3.2 {
  151. sqlite3 db test.db -vfs tvfsshm
  152. execsql {
  153. PRAGMA locking_mode = EXCLUSIVE;
  154. INSERT INTO t1 VALUES(9, 10);
  155. PRAGMA locking_mode = NORMAL;
  156. INSERT INTO t1 VALUES(11, 12);
  157. }
  158. sqlite3 db2 test.db -vfs tvfsshm
  159. catchsql { SELECT * FROM t1 } db2
  160. } {1 {database is locked}}
  161. db close
  162. db2 close
  163. tvfs delete
  164. tvfsshm delete
  165. finish_test