thread1.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # 2003 December 18
  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 multithreading behavior
  13. #
  14. # $Id: thread1.test,v 1.8 2008/10/07 15:25:49 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Skip this whole file if the thread testing code is not enabled
  18. #
  19. if {[run_thread_tests]==0} { finish_test ; return }
  20. if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
  21. finish_test
  22. return
  23. }
  24. # Create some data to work with
  25. #
  26. do_test thread1-1.1 {
  27. execsql {
  28. CREATE TABLE t1(a,b);
  29. INSERT INTO t1 VALUES(1,'abcdefgh');
  30. INSERT INTO t1 SELECT a+1, b||b FROM t1;
  31. INSERT INTO t1 SELECT a+2, b||b FROM t1;
  32. INSERT INTO t1 SELECT a+4, b||b FROM t1;
  33. SELECT count(*), max(length(b)) FROM t1;
  34. }
  35. } {8 64}
  36. # Interleave two threads on read access. Then make sure a third
  37. # thread can write the database. In other words:
  38. #
  39. # read-lock A
  40. # read-lock B
  41. # unlock A
  42. # unlock B
  43. # write-lock C
  44. #
  45. # At one point, the write-lock of C would fail on Linux.
  46. #
  47. do_test thread1-1.2 {
  48. thread_create A test.db
  49. thread_create B test.db
  50. thread_create C test.db
  51. thread_compile A {SELECT a FROM t1}
  52. thread_step A
  53. thread_result A
  54. } SQLITE_ROW
  55. do_test thread1-1.3 {
  56. thread_argc A
  57. } 1
  58. do_test thread1-1.4 {
  59. thread_argv A 0
  60. } 1
  61. do_test thread1-1.5 {
  62. thread_compile B {SELECT b FROM t1}
  63. thread_step B
  64. thread_result B
  65. } SQLITE_ROW
  66. do_test thread1-1.6 {
  67. thread_argc B
  68. } 1
  69. do_test thread1-1.7 {
  70. thread_argv B 0
  71. } abcdefgh
  72. do_test thread1-1.8 {
  73. thread_finalize A
  74. thread_result A
  75. } SQLITE_OK
  76. do_test thread1-1.9 {
  77. thread_finalize B
  78. thread_result B
  79. } SQLITE_OK
  80. do_test thread1-1.10 {
  81. thread_compile C {CREATE TABLE t2(x,y)}
  82. thread_step C
  83. thread_result C
  84. } SQLITE_DONE
  85. do_test thread1-1.11 {
  86. thread_finalize C
  87. thread_result C
  88. } SQLITE_OK
  89. do_test thread1-1.12 {
  90. catchsql {SELECT name FROM sqlite_master}
  91. execsql {SELECT name FROM sqlite_master}
  92. } {t1 t2}
  93. #
  94. # The following tests - thread1-2.* - test the following scenario:
  95. #
  96. # 1: Read-lock thread A
  97. # 2: Read-lock thread B
  98. # 3: Attempt to write in thread C -> SQLITE_BUSY
  99. # 4: Check db write failed from main thread.
  100. # 5: Unlock from thread A.
  101. # 6: Attempt to write in thread C -> SQLITE_BUSY
  102. # 7: Check db write failed from main thread.
  103. # 8: Unlock from thread B.
  104. # 9: Attempt to write in thread C -> SQLITE_DONE
  105. # 10: Finalize the write from thread C
  106. # 11: Check db write succeeded from main thread.
  107. #
  108. do_test thread1-2.1 {
  109. thread_halt *
  110. thread_create A test.db
  111. thread_compile A {SELECT a FROM t1}
  112. thread_step A
  113. thread_result A
  114. } SQLITE_ROW
  115. do_test thread1-2.2 {
  116. thread_create B test.db
  117. thread_compile B {SELECT b FROM t1}
  118. thread_step B
  119. thread_result B
  120. } SQLITE_ROW
  121. do_test thread1-2.3 {
  122. thread_create C test.db
  123. thread_compile C {INSERT INTO t2 VALUES(98,99)}
  124. thread_step C
  125. thread_result C
  126. thread_finalize C
  127. thread_result C
  128. } SQLITE_BUSY
  129. do_test thread1-2.4 {
  130. execsql {SELECT * FROM t2}
  131. } {}
  132. do_test thread1-2.5 {
  133. thread_finalize A
  134. thread_result A
  135. } SQLITE_OK
  136. do_test thread1-2.6 {
  137. thread_compile C {INSERT INTO t2 VALUES(98,99)}
  138. thread_step C
  139. thread_result C
  140. thread_finalize C
  141. thread_result C
  142. } SQLITE_BUSY
  143. do_test thread1-2.7 {
  144. execsql {SELECT * FROM t2}
  145. } {}
  146. do_test thread1-2.8 {
  147. thread_finalize B
  148. thread_result B
  149. } SQLITE_OK
  150. do_test thread1-2.9 {
  151. thread_compile C {INSERT INTO t2 VALUES(98,99)}
  152. thread_step C
  153. thread_result C
  154. } SQLITE_DONE
  155. do_test thread1-2.10 {
  156. thread_finalize C
  157. thread_result C
  158. } SQLITE_OK
  159. do_test thread1-2.11 {
  160. execsql {SELECT * FROM t2}
  161. } {98 99}
  162. thread_halt *
  163. finish_test