wal6.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # 2010 December 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 the operation of the library in
  13. # "PRAGMA journal_mode=WAL" mode.
  14. #
  15. set testdir [file dirname $argv0]
  16. set testprefix wal6
  17. source $testdir/tester.tcl
  18. source $testdir/lock_common.tcl
  19. source $testdir/wal_common.tcl
  20. source $testdir/malloc_common.tcl
  21. ifcapable !wal {finish_test ; return }
  22. #-------------------------------------------------------------------------
  23. # Changing to WAL mode in one connection forces the change in others.
  24. #
  25. db close
  26. forcedelete test.db
  27. set all_journal_modes {delete persist truncate memory off}
  28. foreach jmode $all_journal_modes {
  29. do_test wal6-1.0.$jmode {
  30. sqlite3 db test.db
  31. execsql "PRAGMA journal_mode = $jmode;"
  32. } $jmode
  33. do_test wal6-1.1.$jmode {
  34. execsql {
  35. CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  36. INSERT INTO t1 VALUES(1,2);
  37. SELECT * FROM t1;
  38. }
  39. } {1 2}
  40. # Under Windows, you'll get an error trying to delete
  41. # a file this is already opened. Close the first connection
  42. # so the other tests work.
  43. if {$tcl_platform(platform)=="windows"} {
  44. if {$jmode=="persist" || $jmode=="truncate"} {
  45. db close
  46. }
  47. }
  48. do_test wal6-1.2.$jmode {
  49. sqlite3 db2 test.db
  50. execsql {
  51. PRAGMA journal_mode=WAL;
  52. INSERT INTO t1 VALUES(3,4);
  53. SELECT * FROM t1 ORDER BY a;
  54. } db2
  55. } {wal 1 2 3 4}
  56. if {$tcl_platform(platform)=="windows"} {
  57. if {$jmode=="persist" || $jmode=="truncate"} {
  58. sqlite3 db test.db
  59. }
  60. }
  61. do_test wal6-1.3.$jmode {
  62. execsql {
  63. SELECT * FROM t1 ORDER BY a;
  64. }
  65. } {1 2 3 4}
  66. db close
  67. db2 close
  68. forcedelete test.db
  69. }
  70. #-------------------------------------------------------------------------
  71. # Test that SQLITE_BUSY_SNAPSHOT is returned as expected.
  72. #
  73. reset_db
  74. sqlite3 db2 test.db
  75. do_execsql_test 2.1 {
  76. PRAGMA journal_mode = WAL;
  77. CREATE TABLE t1(a PRIMARY KEY, b TEXT);
  78. INSERT INTO t1 VALUES(1, 'one');
  79. INSERT INTO t1 VALUES(2, 'two');
  80. BEGIN;
  81. SELECT * FROM t1;
  82. } {wal 1 one 2 two}
  83. do_test 2.2 {
  84. execsql {
  85. SELECT * FROM t1;
  86. INSERT INTO t1 VALUES(3, 'three');
  87. } db2
  88. } {1 one 2 two}
  89. do_catchsql_test 2.3 {
  90. INSERT INTO t1 VALUES('x', 'x')
  91. } {1 {database is locked}}
  92. do_test 2.4 {
  93. list [sqlite3_errcode db] [sqlite3_extended_errcode db]
  94. } {SQLITE_BUSY SQLITE_BUSY_SNAPSHOT}
  95. do_execsql_test 2.5 {
  96. SELECT * FROM t1;
  97. COMMIT;
  98. INSERT INTO t1 VALUES('x', 'x')
  99. } {1 one 2 two}
  100. proc test3 {prefix} {
  101. do_test $prefix.1 {
  102. execsql { SELECT count(*) FROM t1 }
  103. } {0}
  104. do_test $prefix.2 {
  105. execsql { INSERT INTO t1 VALUES('x', 'x') } db2
  106. } {}
  107. do_test $prefix.3 {
  108. execsql { INSERT INTO t1 VALUES('y', 'y') }
  109. } {}
  110. do_test $prefix.4 {
  111. execsql { SELECT count(*) FROM t1 }
  112. } {2}
  113. }
  114. do_execsql_test 2.6.1 { DELETE FROM t1 }
  115. test3 2.6.2
  116. db func test3 test3
  117. do_execsql_test 2.6.3 { DELETE FROM t1 }
  118. db eval {SELECT test3('2.6.4')}
  119. do_test 2.x {
  120. db2 close
  121. } {}
  122. #-------------------------------------------------------------------------
  123. # Check that if BEGIN IMMEDIATE fails, it does not leave the user with
  124. # an open read-transaction (unless one was already open before the BEGIN
  125. # IMMEDIATE). Even if there are other active VMs.
  126. #
  127. proc test4 {prefix} {
  128. do_test $prefix.1 {
  129. catchsql { BEGIN IMMEDIATE }
  130. } {1 {database is locked}}
  131. do_test $prefix.2 {
  132. execsql { COMMIT } db2
  133. } {}
  134. do_test $prefix.3 {
  135. execsql { BEGIN IMMEDIATE }
  136. } {}
  137. do_test $prefix.4 {
  138. execsql { COMMIT }
  139. } {}
  140. }
  141. reset_db
  142. sqlite3 db2 test.db
  143. do_execsql_test 3.1 {
  144. PRAGMA journal_mode = WAL;
  145. CREATE TABLE ab(a PRIMARY KEY, b);
  146. } {wal}
  147. do_test 3.2.1 {
  148. execsql {
  149. BEGIN;
  150. INSERT INTO ab VALUES(1, 2);
  151. } db2
  152. } {}
  153. test4 3.2.2
  154. db func test4 test4
  155. do_test 3.3.1 {
  156. execsql {
  157. BEGIN;
  158. INSERT INTO ab VALUES(3, 4);
  159. } db2
  160. } {}
  161. db eval {SELECT test4('3.3.2')}
  162. do_test 3.x {
  163. db2 close
  164. } {}
  165. finish_test