thread001.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # 2007 September 7
  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: thread001.test,v 1.10 2009/03/26 14:48:07 danielk1977 Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. if {[run_thread_tests]==0} { finish_test ; return }
  16. set ::enable_shared_cache [sqlite3_enable_shared_cache]
  17. set ::NTHREAD 10
  18. # Run this test three times:
  19. #
  20. # 1) All threads use the same database handle.
  21. # 2) All threads use their own database handles.
  22. # 3) All threads use their own database handles, shared-cache is enabled.
  23. #
  24. #
  25. #
  26. foreach {tn same_db shared_cache} [list \
  27. 1 1 0 \
  28. 2 0 0 \
  29. 3 0 1 \
  30. ] {
  31. # Empty the database.
  32. #
  33. catchsql { DROP TABLE ab; }
  34. do_test thread001.$tn.0 {
  35. db close
  36. sqlite3_enable_shared_cache $shared_cache
  37. sqlite3_enable_shared_cache $shared_cache
  38. } $shared_cache
  39. sqlite3 db test.db -fullmutex 1 -key xyzzy
  40. set dbconfig ""
  41. if {$same_db} {
  42. set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
  43. }
  44. # Set up a database and a schema. The database contains a single
  45. # table with two columns. The first column ("a") is an INTEGER PRIMARY
  46. # KEY. The second contains the md5sum of all rows in the table with
  47. # a smaller value stored in column "a".
  48. #
  49. do_test thread001.$tn.1 {
  50. execsql {
  51. CREATE TABLE ab(a INTEGER PRIMARY KEY, b);
  52. CREATE INDEX ab_i ON ab(b);
  53. INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab;
  54. SELECT count(*) FROM ab;
  55. }
  56. } {1}
  57. do_test thread001.$tn.2 {
  58. execsql {
  59. SELECT
  60. (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
  61. (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
  62. }
  63. } {1}
  64. do_test thread001.$tn.3 {
  65. execsql { PRAGMA integrity_check }
  66. } {ok}
  67. set thread_program {
  68. #sqlthread parent {puts STARTING..}
  69. set needToClose 0
  70. if {![info exists ::DB]} {
  71. set ::DB [sqlthread open test.db xyzzy]
  72. #sqlthread parent "puts \"OPEN $::DB\""
  73. set needToClose 1
  74. }
  75. for {set i 0} {$i < 100} {incr i} {
  76. # Test that the invariant is true.
  77. do_test t1 {
  78. execsql {
  79. SELECT
  80. (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
  81. (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
  82. }
  83. } {1}
  84. # Add another row to the database.
  85. execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }
  86. }
  87. if {$needToClose} {
  88. #sqlthread parent "puts \"CLOSE $::DB\""
  89. sqlite3_close $::DB
  90. }
  91. #sqlthread parent "puts \"DONE\""
  92. list OK
  93. }
  94. # Kick off $::NTHREAD threads:
  95. #
  96. array unset finished
  97. for {set i 0} {$i < $::NTHREAD} {incr i} {
  98. thread_spawn finished($i) $dbconfig $thread_procs $thread_program
  99. }
  100. # Wait for all threads to finish, then check they all returned "OK".
  101. #
  102. for {set i 0} {$i < $::NTHREAD} {incr i} {
  103. if {![info exists finished($i)]} {
  104. vwait finished($i)
  105. }
  106. do_test thread001.$tn.4.$i {
  107. set ::finished($i)
  108. } OK
  109. }
  110. # Check the database still looks Ok.
  111. #
  112. do_test thread001.$tn.5 {
  113. execsql { SELECT count(*) FROM ab; }
  114. } [expr {1 + $::NTHREAD*100}]
  115. do_test thread001.$tn.6 {
  116. execsql {
  117. SELECT
  118. (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
  119. (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
  120. }
  121. } {1}
  122. do_test thread001.$tn.7 {
  123. execsql { PRAGMA integrity_check }
  124. } {ok}
  125. }
  126. sqlite3_enable_shared_cache $::enable_shared_cache
  127. set sqlite_open_file_count 0
  128. finish_test