capi3b.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # 2004 September 2
  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 testing the callback-free C/C++ API and in
  13. # particular the behavior of sqlite3_step() when trying to commit
  14. # with lock contention.
  15. #
  16. # $Id: capi3b.test,v 1.4 2007/08/10 19:46:14 drh Exp $
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # These tests depend on the pager holding changes in cache
  21. # until it is time to commit. But that won't happen if the
  22. # soft-heap-limit is set too low. So disable the soft heap limit
  23. # for the duration of this test.
  24. #
  25. sqlite3_soft_heap_limit 0
  26. set DB [sqlite3_connection_pointer db]
  27. sqlite3 db2 test.db
  28. set DB2 [sqlite3_connection_pointer db2]
  29. # Create some data in the database
  30. #
  31. do_test capi3b-1.1 {
  32. execsql {
  33. CREATE TABLE t1(x);
  34. INSERT INTO t1 VALUES(1);
  35. INSERT INTO t1 VALUES(2);
  36. SELECT * FROM t1
  37. }
  38. } {1 2}
  39. # Make sure the second database connection can see the data
  40. #
  41. do_test capi3b-1.2 {
  42. execsql {
  43. SELECT * FROM t1
  44. } db2
  45. } {1 2}
  46. # First database connection acquires a shared lock
  47. #
  48. do_test capi3b-1.3 {
  49. execsql {
  50. BEGIN;
  51. SELECT * FROM t1;
  52. }
  53. } {1 2}
  54. # Second database connection tries to write. The sqlite3_step()
  55. # function returns SQLITE_BUSY because it cannot commit.
  56. #
  57. do_test capi3b-1.4 {
  58. set VM [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(3)} -1 TAIL]
  59. sqlite3_step $VM
  60. } SQLITE_BUSY
  61. # The sqlite3_step call can be repeated multiple times.
  62. #
  63. do_test capi3b-1.5.1 {
  64. sqlite3_step $VM
  65. } SQLITE_BUSY
  66. do_test capi3b-1.5.2 {
  67. sqlite3_step $VM
  68. } SQLITE_BUSY
  69. # The first connection closes its transaction. This allows the second
  70. # connections sqlite3_step to succeed.
  71. #
  72. do_test capi3b-1.6 {
  73. execsql COMMIT
  74. sqlite3_step $VM
  75. } SQLITE_DONE
  76. do_test capi3b-1.7 {
  77. sqlite3_finalize $VM
  78. } SQLITE_OK
  79. do_test capi3b-1.8 {
  80. execsql {SELECT * FROM t1} db2
  81. } {1 2 3}
  82. do_test capi3b-1.9 {
  83. execsql {SELECT * FROM t1}
  84. } {1 2 3}
  85. # Start doing a SELECT with one connection. This gets a SHARED lock.
  86. # Then do an INSERT with the other connection. The INSERT should
  87. # not be able to complete until the SELECT finishes.
  88. #
  89. do_test capi3b-2.1 {
  90. set VM1 [sqlite3_prepare $DB {SELECT * FROM t1} -1 TAIL]
  91. sqlite3_step $VM1
  92. } SQLITE_ROW
  93. do_test capi3b-2.2 {
  94. sqlite3_column_text $VM1 0
  95. } 1
  96. do_test capi3b-2.3 {
  97. set VM2 [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(4)} -1 TAIL]
  98. sqlite3_step $VM2
  99. } SQLITE_BUSY
  100. do_test capi3b-2.4 {
  101. sqlite3_step $VM1
  102. } SQLITE_ROW
  103. do_test capi3b-2.5 {
  104. sqlite3_column_text $VM1 0
  105. } 2
  106. do_test capi3b-2.6 {
  107. sqlite3_step $VM2
  108. } SQLITE_BUSY
  109. do_test capi3b-2.7 {
  110. sqlite3_step $VM1
  111. } SQLITE_ROW
  112. do_test capi3b-2.8 {
  113. sqlite3_column_text $VM1 0
  114. } 3
  115. do_test capi3b-2.9 {
  116. sqlite3_step $VM2
  117. } SQLITE_BUSY
  118. do_test capi3b-2.10 {
  119. sqlite3_step $VM1
  120. } SQLITE_DONE
  121. do_test capi3b-2.11 {
  122. sqlite3_step $VM2
  123. } SQLITE_DONE
  124. do_test capi3b-2.12 {
  125. sqlite3_finalize $VM1
  126. sqlite3_finalize $VM2
  127. execsql {SELECT * FROM t1}
  128. } {1 2 3 4}
  129. catch {db2 close}
  130. sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
  131. finish_test