1
0

thread002.test 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # 2007 September 10
  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. # This test attempts to deadlock SQLite in shared-cache mode.
  13. #
  14. #
  15. # $Id: thread002.test,v 1.9 2009/03/26 14:48:07 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. set do_not_use_codec 1
  18. source $testdir/tester.tcl
  19. if {[run_thread_tests]==0} { finish_test ; return }
  20. db close
  21. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  22. set ::NTHREAD 10
  23. do_test thread002.1 {
  24. # Create 3 databases with identical schemas:
  25. for {set ii 0} {$ii < 3} {incr ii} {
  26. forcedelete test${ii}.db
  27. sqlite3 db test${ii}.db
  28. execsql {
  29. CREATE TABLE t1(k, v);
  30. CREATE INDEX t1_i ON t1(v);
  31. INSERT INTO t1(v) VALUES(1.0);
  32. }
  33. db close
  34. }
  35. } {}
  36. set thread_program {
  37. set ::DB [sqlite3_open test.db]
  38. for {set ii 1} {$ii <= 3} {incr ii} {
  39. set T [lindex $order [expr $ii-1]]
  40. execsql "ATTACH 'test${T}.db' AS aux${ii}"
  41. }
  42. for {set ii 0} {$ii < 100} {incr ii} {
  43. execsql { SELECT * FROM aux1.t1 }
  44. execsql { INSERT INTO aux1.t1(v) SELECT sum(v) FROM aux2.t1 }
  45. execsql { SELECT * FROM aux2.t1 }
  46. execsql { INSERT INTO aux2.t1(v) SELECT sum(v) FROM aux3.t1 }
  47. execsql { SELECT * FROM aux3.t1 }
  48. execsql { INSERT INTO aux3.t1(v) SELECT sum(v) FROM aux1.t1 }
  49. execsql { CREATE TABLE IF NOT EXISTS aux1.t2(a,b) }
  50. execsql { DROP TABLE IF EXISTS aux1.t2 }
  51. # if {($ii%10)==0} {puts -nonewline . ; flush stdout}
  52. puts -nonewline . ; flush stdout
  53. }
  54. sqlite3_close $::DB
  55. list OK
  56. }
  57. set order_list [list {0 1 2} {0 2 1} {1 0 2} {1 2 0} {2 0 1} {2 1 0}]
  58. array unset finished
  59. for {set ii 0} {$ii < $::NTHREAD} {incr ii} {
  60. set order [lindex $order_list [expr $ii%6]]
  61. thread_spawn finished($ii) $thread_procs "set order {$order}" $thread_program
  62. }
  63. # Wait for all threads to finish, then check they all returned "OK".
  64. #
  65. for {set i 0} {$i < $::NTHREAD} {incr i} {
  66. if {![info exists finished($i)]} {
  67. vwait finished($i)
  68. }
  69. do_test thread002.2.$i {
  70. set ::finished($i)
  71. } OK
  72. }
  73. # Check all three databases are Ok.
  74. for {set ii 0} {$ii < 3} {incr ii} {
  75. do_test thread002.3.$ii {
  76. sqlite3 db test${ii}.db
  77. set res [list \
  78. [execsql {SELECT count(*) FROM t1}] \
  79. [execsql {PRAGMA integrity_check}] \
  80. ]
  81. db close
  82. set res
  83. } [list [expr 1 + $::NTHREAD*100] ok]
  84. }
  85. sqlite3_enable_shared_cache $::enable_shared_cache
  86. set sqlite_open_file_count 0
  87. finish_test