mutex1.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # 2008 June 17
  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. #
  12. # $Id: mutex1.test,v 1.20 2009/04/23 14:58:40 danielk1977 Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. ifcapable !mutex {
  16. finish_test
  17. return
  18. }
  19. if {[info exists tester_do_binarylog]} {
  20. finish_test
  21. return
  22. }
  23. sqlite3_reset_auto_extension
  24. clear_mutex_counters
  25. proc mutex_counters {varname} {
  26. upvar $varname var
  27. set var(total) 0
  28. foreach {name value} [read_mutex_counters] {
  29. set var($name) $value
  30. incr var(total) $value
  31. }
  32. }
  33. #-------------------------------------------------------------------------
  34. # Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if
  35. # is called at the wrong time. And that the first time sqlite3_initialize
  36. # is called it obtains the 'static_master' mutex 3 times and a recursive
  37. # mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops
  38. # that do not require any mutexes.
  39. #
  40. do_test mutex1-1.0 {
  41. install_mutex_counters 1
  42. } {SQLITE_MISUSE}
  43. do_test mutex1-1.1 {
  44. db close
  45. install_mutex_counters 1
  46. } {SQLITE_MISUSE}
  47. do_test mutex1-1.2 {
  48. sqlite3_shutdown
  49. install_mutex_counters 1
  50. } {SQLITE_OK}
  51. do_test mutex1-1.3 {
  52. install_mutex_counters 0
  53. } {SQLITE_OK}
  54. do_test mutex1-1.4 {
  55. install_mutex_counters 1
  56. } {SQLITE_OK}
  57. do_test mutex1-1.5 {
  58. mutex_counters counters
  59. set counters(total)
  60. } {0}
  61. do_test mutex1-1.6 {
  62. sqlite3_initialize
  63. } {SQLITE_OK}
  64. do_test mutex1-1.7 {
  65. mutex_counters counters
  66. # list $counters(total) $counters(static_master)
  67. expr {$counters(total)>0}
  68. } {1}
  69. do_test mutex1-1.8 {
  70. clear_mutex_counters
  71. sqlite3_initialize
  72. } {SQLITE_OK}
  73. do_test mutex1-1.9 {
  74. mutex_counters counters
  75. list $counters(total) $counters(static_master)
  76. } {0 0}
  77. #-------------------------------------------------------------------------
  78. # Tests mutex1-2.* test the three thread-safety related modes that
  79. # can be selected using sqlite3_config:
  80. #
  81. # * Serialized mode,
  82. # * Multi-threaded mode,
  83. # * Single-threaded mode.
  84. #
  85. ifcapable threadsafe&&shared_cache {
  86. set enable_shared_cache [sqlite3_enable_shared_cache 1]
  87. foreach {mode mutexes} {
  88. singlethread {}
  89. multithread {
  90. fast static_lru static_master static_mem static_open static_prng
  91. static_pmem
  92. }
  93. serialized {
  94. fast recursive static_lru static_master static_mem static_open
  95. static_prng static_pmem
  96. }
  97. } {
  98. do_test mutex1.2.$mode.1 {
  99. catch {db close}
  100. sqlite3_shutdown
  101. sqlite3_config $mode
  102. } SQLITE_OK
  103. do_test mutex1.2.$mode.2 {
  104. sqlite3_initialize
  105. clear_mutex_counters
  106. sqlite3 db test.db -nomutex 0 -fullmutex 0
  107. catchsql { CREATE TABLE abc(a, b, c) }
  108. db eval {
  109. INSERT INTO abc VALUES(1, 2, 3);
  110. }
  111. } {}
  112. ifcapable !memorymanage {
  113. regsub { static_lru} $mutexes {} mutexes
  114. }
  115. do_test mutex1.2.$mode.3 {
  116. mutex_counters counters
  117. set res [list]
  118. foreach {key value} [array get counters] {
  119. if {$key ne "total" && $value > 0} {
  120. lappend res $key
  121. }
  122. }
  123. lsort $res
  124. } [lsort $mutexes]
  125. }
  126. sqlite3_enable_shared_cache $enable_shared_cache
  127. # Open and use a connection in "nomutex" mode. Test that no recursive
  128. # mutexes are obtained.
  129. do_test mutex1.3.1 {
  130. catch {db close}
  131. clear_mutex_counters
  132. sqlite3 db test.db -nomutex 1
  133. execsql { SELECT * FROM abc }
  134. } {1 2 3 1 2 3 1 2 3}
  135. do_test mutex1.3.2 {
  136. mutex_counters counters
  137. set counters(recursive)
  138. } {0}
  139. }
  140. # Test the sqlite3_db_mutex() function.
  141. #
  142. do_test mutex1.4.1 {
  143. catch {db close}
  144. sqlite3 db test.db
  145. enter_db_mutex db
  146. db eval {SELECT 1, 2, 3}
  147. } {1 2 3}
  148. do_test mutex1.4.2 {
  149. leave_db_mutex db
  150. db eval {SELECT 1, 2, 3}
  151. } {1 2 3}
  152. do_test mutex1.4.3 {
  153. catch {db close}
  154. sqlite3 db test.db -nomutex 1
  155. enter_db_mutex db
  156. db eval {SELECT 1, 2, 3}
  157. } {1 2 3}
  158. do_test mutex1.4.4 {
  159. leave_db_mutex db
  160. db eval {SELECT 1, 2, 3}
  161. } {1 2 3}
  162. do_test mutex1-X {
  163. catch {db close}
  164. sqlite3_shutdown
  165. clear_mutex_counters
  166. install_mutex_counters 0
  167. sqlite3_initialize
  168. } {SQLITE_OK}
  169. autoinstall_test_functions
  170. finish_test