server1.test 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # 2006 January 09
  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 testing the server mode of SQLite.
  13. #
  14. # This file is derived from thread1.test
  15. #
  16. # $Id: server1.test,v 1.5 2007/08/29 18:20:17 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Skip this whole file if the server testing code is not enabled
  20. #
  21. if {[llength [info command client_step]]==0 || [sqlite3 -has-codec]} {
  22. finish_test
  23. return
  24. }
  25. # The sample server implementation does not work right when memory
  26. # management is enabled.
  27. #
  28. ifcapable (memorymanage||mutex_noop) {
  29. finish_test
  30. return
  31. }
  32. # Create some data to work with
  33. #
  34. do_test server1-1.1 {
  35. execsql {
  36. CREATE TABLE t1(a,b);
  37. INSERT INTO t1 VALUES(1,'abcdefgh');
  38. INSERT INTO t1 SELECT a+1, b||b FROM t1;
  39. INSERT INTO t1 SELECT a+2, b||b FROM t1;
  40. INSERT INTO t1 SELECT a+4, b||b FROM t1;
  41. SELECT count(*), max(length(b)) FROM t1;
  42. }
  43. } {8 64}
  44. # Interleave two threads on read access. Then make sure a third
  45. # thread can write the database. In other words:
  46. #
  47. # read-lock A
  48. # read-lock B
  49. # unlock A
  50. # unlock B
  51. # write-lock C
  52. #
  53. do_test server1-1.2 {
  54. client_create A test.db
  55. client_create B test.db
  56. client_create C test.db
  57. client_compile A {SELECT a FROM t1}
  58. client_step A
  59. client_result A
  60. } SQLITE_ROW
  61. do_test server1-1.3 {
  62. client_argc A
  63. } 1
  64. do_test server1-1.4 {
  65. client_argv A 0
  66. } 1
  67. do_test server1-1.5 {
  68. client_compile B {SELECT b FROM t1}
  69. client_step B
  70. client_result B
  71. } SQLITE_ROW
  72. do_test server1-1.6 {
  73. client_argc B
  74. } 1
  75. do_test server1-1.7 {
  76. client_argv B 0
  77. } abcdefgh
  78. do_test server1-1.8 {
  79. client_finalize A
  80. client_result A
  81. } SQLITE_OK
  82. do_test server1-1.9 {
  83. client_finalize B
  84. client_result B
  85. } SQLITE_OK
  86. do_test server1-1.10 {
  87. client_compile C {CREATE TABLE t2(x,y)}
  88. client_step C
  89. client_result C
  90. } SQLITE_DONE
  91. do_test server1-1.11 {
  92. client_finalize C
  93. client_result C
  94. } SQLITE_OK
  95. do_test server1-1.12 {
  96. catchsql {SELECT name FROM sqlite_master}
  97. execsql {SELECT name FROM sqlite_master}
  98. } {t1 t2}
  99. # Read from table t1. Do not finalize the statement. This
  100. # will leave the lock pending.
  101. #
  102. do_test server1-2.1 {
  103. client_halt *
  104. client_create A test.db
  105. client_compile A {SELECT a FROM t1}
  106. client_step A
  107. client_result A
  108. } SQLITE_ROW
  109. # Read from the same table from another thread. This is allows.
  110. #
  111. do_test server1-2.2 {
  112. client_create B test.db
  113. client_compile B {SELECT b FROM t1}
  114. client_step B
  115. client_result B
  116. } SQLITE_ROW
  117. # Write to a different table from another thread. This is allowed
  118. # because in server mode with a shared cache we have table-level locking.
  119. #
  120. do_test server1-2.3 {
  121. client_create C test.db
  122. client_compile C {INSERT INTO t2 VALUES(98,99)}
  123. client_step C
  124. client_result C
  125. client_finalize C
  126. client_result C
  127. } SQLITE_OK
  128. # But we cannot insert into table t1 because threads A and B have it locked.
  129. #
  130. do_test server1-2.4 {
  131. client_compile C {INSERT INTO t1 VALUES(98,99)}
  132. client_step C
  133. client_result C
  134. client_finalize C
  135. client_result C
  136. } SQLITE_LOCKED
  137. do_test server1-2.5 {
  138. client_finalize B
  139. client_wait B
  140. client_compile C {INSERT INTO t1 VALUES(98,99)}
  141. client_step C
  142. client_result C
  143. client_finalize C
  144. client_result C
  145. } SQLITE_LOCKED
  146. # Insert into t1 is successful after finishing the other two threads.
  147. do_test server1-2.6 {
  148. client_finalize A
  149. client_wait A
  150. client_compile C {INSERT INTO t1 VALUES(98,99)}
  151. client_step C
  152. client_result C
  153. client_finalize C
  154. client_result C
  155. } SQLITE_OK
  156. client_halt *
  157. sqlite3_enable_shared_cache 0
  158. finish_test