lock.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # 2001 September 15
  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 script is database locks.
  13. #
  14. # $Id: lock.test,v 1.14 2002/05/10 13:14:08 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Create an alternative connection to the database
  18. #
  19. do_test lock-1.0 {
  20. sqlite db2 ./test.db
  21. set dummy {}
  22. } {}
  23. do_test lock-1.1 {
  24. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  25. } {}
  26. do_test lock-1.2 {
  27. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name} db2
  28. } {}
  29. do_test lock-1.3 {
  30. execsql {CREATE TABLE t1(a int, b int)}
  31. execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
  32. } {t1}
  33. do_test lock-1.4 {
  34. catchsql {
  35. SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
  36. } db2
  37. } {1 {database schema has changed}}
  38. do_test lock-1.5 {
  39. catchsql {
  40. SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
  41. } db2
  42. } {0 t1}
  43. do_test lock-1.6 {
  44. execsql {INSERT INTO t1 VALUES(1,2)}
  45. execsql {SELECT * FROM t1}
  46. } {1 2}
  47. do_test lock-1.7 {
  48. execsql {SELECT * FROM t1} db2
  49. } {1 2}
  50. do_test lock-1.8 {
  51. execsql {UPDATE t1 SET a=b, b=a} db2
  52. execsql {SELECT * FROM t1} db2
  53. } {2 1}
  54. do_test lock-1.9 {
  55. execsql {SELECT * FROM t1}
  56. } {2 1}
  57. do_test lock-1.10 {
  58. execsql {BEGIN TRANSACTION}
  59. execsql {SELECT * FROM t1}
  60. } {2 1}
  61. do_test lock-1.11 {
  62. catchsql {SELECT * FROM t1} db2
  63. } {1 {database is locked}}
  64. do_test lock-1.12 {
  65. execsql {ROLLBACK}
  66. catchsql {SELECT * FROM t1}
  67. } {0 {2 1}}
  68. do_test lock-1.13 {
  69. execsql {CREATE TABLE t2(x int, y int)}
  70. execsql {INSERT INTO t2 VALUES(8,9)}
  71. execsql {SELECT * FROM t2}
  72. } {8 9}
  73. do_test lock-1.14 {
  74. catchsql {SELECT * FROM t1} db2
  75. } {1 {database schema has changed}}
  76. do_test lock-1.15 {
  77. catchsql {SELECT * FROM t2} db2
  78. } {0 {8 9}}
  79. do_test lock-1.16 {
  80. db eval {SELECT * FROM t1} qv {
  81. set x [db eval {SELECT * FROM t1}]
  82. }
  83. set x
  84. } {2 1}
  85. do_test lock-1.17 {
  86. db eval {SELECT * FROM t1} qv {
  87. set x [db eval {SELECT * FROM t2}]
  88. }
  89. set x
  90. } {8 9}
  91. # You cannot UPDATE a table from within the callback of a SELECT
  92. # on that same table because the SELECT has the table locked.
  93. #
  94. do_test lock-1.18 {
  95. db eval {SELECT * FROM t1} qv {
  96. set r [catch {db eval {UPDATE t1 SET a=b, b=a}} msg]
  97. lappend r $msg
  98. }
  99. set r
  100. } {1 {database table is locked}}
  101. # But you can UPDATE a different table from the one that is used in
  102. # the SELECT.
  103. #
  104. do_test lock-1.19 {
  105. db eval {SELECT * FROM t1} qv {
  106. set r [catch {db eval {UPDATE t2 SET x=y, y=x}} msg]
  107. lappend r $msg
  108. }
  109. set r
  110. } {0 {}}
  111. do_test lock-1.20 {
  112. execsql {SELECT * FROM t2}
  113. } {9 8}
  114. # It is possible to do a SELECT of the same table within the
  115. # callback of another SELECT on that same table because two
  116. # or more read-only cursors can be open at once.
  117. #
  118. do_test lock-1.21 {
  119. db eval {SELECT * FROM t1} qv {
  120. set r [catch {db eval {SELECT a FROM t1}} msg]
  121. lappend r $msg
  122. }
  123. set r
  124. } {0 2}
  125. # Under UNIX you can do two SELECTs at once with different database
  126. # connections, because UNIX supports reader/writer locks. Under windows,
  127. # this is not possible.
  128. #
  129. if {$::tcl_platform(platform)=="unix"} {
  130. do_test lock-1.22 {
  131. db eval {SELECT * FROM t1} qv {
  132. set r [catch {db2 eval {SELECT a FROM t1}} msg]
  133. lappend r $msg
  134. }
  135. set r
  136. } {0 2}
  137. }
  138. # If one thread has a transaction another thread cannot start
  139. # a transaction.
  140. #
  141. do_test lock-2.1 {
  142. execsql {BEGIN TRANSACTION}
  143. set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
  144. lappend r $msg
  145. } {1 {database is locked}}
  146. # Nor can the other thread do a query.
  147. #
  148. do_test lock-2.2 {
  149. set r [catch {execsql {SELECT * FROM t2} db2} msg]
  150. lappend r $msg
  151. } {1 {database is locked}}
  152. # If the other thread (the one that does not hold the transaction)
  153. # tries to start a transaction, we get a busy callback.
  154. #
  155. do_test lock-2.3 {
  156. proc callback {args} {
  157. set ::callback_value $args
  158. break
  159. }
  160. set ::callback_value {}
  161. db2 busy callback
  162. set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
  163. lappend r $msg
  164. lappend r $::callback_value
  165. } {1 {database is locked} {{} 1}}
  166. do_test lock-2.4 {
  167. proc callback {file count} {
  168. lappend ::callback_value $count
  169. if {$count>4} break
  170. }
  171. set ::callback_value {}
  172. db2 busy callback
  173. set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
  174. lappend r $msg
  175. lappend r $::callback_value
  176. } {1 {database is locked} {1 2 3 4 5}}
  177. do_test lock-2.5 {
  178. proc callback {file count} {
  179. lappend ::callback_value $count
  180. if {$count>4} break
  181. }
  182. set ::callback_value {}
  183. db2 busy callback
  184. set r [catch {execsql {SELECT * FROM t1} db2} msg]
  185. lappend r $msg
  186. lappend r $::callback_value
  187. } {1 {database is locked} {1 2 3 4 5}}
  188. # In this test, the 3rd invocation of the busy callback causes
  189. # the first thread to release its transaction. That allows the
  190. # second thread to continue.
  191. #
  192. do_test lock-2.6 {
  193. proc callback {file count} {
  194. lappend ::callback_value $count
  195. if {$count>2} {
  196. execsql {ROLLBACK}
  197. }
  198. }
  199. set ::callback_value {}
  200. db2 busy callback
  201. set r [catch {execsql {SELECT * FROM t2} db2} msg]
  202. lappend r $msg
  203. lappend r $::callback_value
  204. } {0 {9 8} {1 2 3}}
  205. do_test lock-2.7 {
  206. execsql {BEGIN TRANSACTION}
  207. proc callback {file count} {
  208. lappend ::callback_value $count
  209. if {$count>2} {
  210. execsql {ROLLBACK}
  211. }
  212. }
  213. set ::callback_value {}
  214. db2 busy callback
  215. set r [catch {execsql {BEGIN TRANSACTION} db2} msg]
  216. execsql {ROLLBACK} db2
  217. lappend r $msg
  218. lappend r $::callback_value
  219. } {0 {} {1 2 3}}
  220. # Try to start two transactions in a row
  221. #
  222. do_test lock-3.1 {
  223. execsql {BEGIN TRANSACTION}
  224. set r [catch {execsql {BEGIN TRANSACTION}} msg]
  225. execsql {ROLLBACK}
  226. lappend r $msg
  227. } {0 {}}
  228. do_test lock-999.1 {
  229. rename db2 {}
  230. } {}
  231. finish_test