notify3.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # 2010 June 30
  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 sqlite3_unlock_notify() API.
  13. #
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. # This script only runs if shared-cache and unlock-notify are available.
  17. #
  18. ifcapable !unlock_notify||!shared_cache {
  19. finish_test
  20. return
  21. }
  22. set esc [sqlite3_enable_shared_cache 1]
  23. sqlite3 db test.db
  24. forcedelete test.db2 test.db2-journal test.db2-wal
  25. sqlite3 db2 test.db2
  26. do_test notify3-1.1 {
  27. execsql {
  28. CREATE TABLE t1(a, b);
  29. INSERT INTO t1 VALUES('t1 A', 't1 B');
  30. }
  31. } {}
  32. do_test notify3-1.2 {
  33. execsql {
  34. CREATE TABLE t2(a, b);
  35. INSERT INTO t2 VALUES('t2 A', 't2 B');
  36. } db2
  37. } {}
  38. do_test notify3-1.3 {
  39. execsql {
  40. BEGIN EXCLUSIVE;
  41. INSERT INTO t2 VALUES('t2 C', 't2 D');
  42. } db2
  43. } {}
  44. do_test notify3-1.4 {
  45. catchsql { ATTACH 'test.db2' AS aux }
  46. } {0 {}}
  47. do_test notify3-1.5 {
  48. catchsql { SELECT * FROM t2 }
  49. } {1 {database schema is locked: aux}}
  50. do_test notify3-1.6 {
  51. list [sqlite3_errcode db] [sqlite3_extended_errcode db]
  52. } {SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE}
  53. do_test notify3-1.7 {
  54. sqlite3_extended_result_codes db 1
  55. catch { set ::stmt [sqlite3_prepare_v2 db "SELECT * FROM t2" -1 tail] } msg
  56. set msg
  57. } {(262) database schema is locked: aux}
  58. do_test notify3-1.8 {
  59. set ::when 1
  60. db unlock_notify { set ::res $::when }
  61. set ::when 2
  62. execsql { COMMIT } db2
  63. set ::res
  64. } {2}
  65. do_test notify3-1.9 {
  66. catchsql { SELECT * FROM t2 }
  67. } {0 {{t2 A} {t2 B} {t2 C} {t2 D}}}
  68. db close
  69. set err {{1 {unable to open database: test.db2}}}
  70. set noerr {{0 {}}}
  71. # When a new database is attached, the connection doing the attaching
  72. # tries to load any unloaded schemas for both the new database and any
  73. # already attached databases (including the main database). If it is
  74. # unable to load any such schemas, then the ATTACH statement fails.
  75. #
  76. # This block tests that if the loading of schemas as a result of an
  77. # ATTACH fails due to locks on the schema table held by other shared-cache
  78. # connections the extended error code is SQLITE_LOCKED_SHAREDCACHE and
  79. # it is possible to use the unlock-notify mechanism to determine when
  80. # the ATTACH might succeed.
  81. #
  82. # This test does not work for test-permutations that specify SQL to
  83. # be executed as part of the [sqlite3] command that opens the database.
  84. # Executing such SQL causes SQLite to load the database schema into memory
  85. # earlier than expected, causing test cases to fail.
  86. #
  87. if {[presql] == ""} {
  88. foreach {
  89. tn
  90. db1_loaded
  91. db2_loaded
  92. enable_extended_errors
  93. result
  94. error1 error2
  95. } "
  96. 0 0 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE
  97. 1 0 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE
  98. 2 0 1 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE
  99. 3 0 1 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE
  100. 4 1 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE
  101. 5 1 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE
  102. 6 1 1 0 $noerr SQLITE_OK SQLITE_OK
  103. 7 1 1 1 $noerr SQLITE_OK SQLITE_OK
  104. " {
  105. do_test notify3-2.$tn.1 {
  106. catch { db1 close }
  107. catch { db2 close }
  108. sqlite3 db1 test.db
  109. sqlite3 db2 test.db2
  110. sqlite3_extended_result_codes db1 $enable_extended_errors
  111. sqlite3_extended_result_codes db2 $enable_extended_errors
  112. if { $db1_loaded } { db1 eval "SELECT * FROM sqlite_master" }
  113. if { $db2_loaded } { db2 eval "SELECT * FROM sqlite_master" }
  114. db2 eval "BEGIN EXCLUSIVE"
  115. catchsql "ATTACH 'test.db2' AS two" db1
  116. } $result
  117. do_test notify3-2.$tn.2 {
  118. list [sqlite3_errcode db1] [sqlite3_extended_errcode db1]
  119. } [list $error1 $error2]
  120. do_test notify3-2.$tn.3 {
  121. db1 unlock_notify {set invoked 1}
  122. set invoked 0
  123. db2 eval commit
  124. set invoked
  125. } [lindex $result 0]
  126. }
  127. }
  128. catch { db1 close }
  129. catch { db2 close }
  130. sqlite3_enable_shared_cache $esc
  131. finish_test