lock6.test 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # 2008 October 6
  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 is database locks.
  13. #
  14. # $Id: lock6.test,v 1.3 2009/02/05 16:31:46 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Launch another testfixture process to be controlled by this one. A
  18. # channel name is returned that may be passed as the first argument to proc
  19. # 'testfixture' to execute a command. The child testfixture process is shut
  20. # down by closing the channel.
  21. proc launch_testfixture {} {
  22. set prg [info nameofexec]
  23. if {$prg eq ""} {
  24. set prg [file join . testfixture]
  25. }
  26. set chan [open "|$prg tf_main2.tcl" r+]
  27. fconfigure $chan -buffering line
  28. return $chan
  29. }
  30. # Execute a command in a child testfixture process, connected by two-way
  31. # channel $chan. Return the result of the command, or an error message.
  32. proc testfixture {chan cmd} {
  33. puts $chan $cmd
  34. puts $chan OVER
  35. set r ""
  36. while { 1 } {
  37. set line [gets $chan]
  38. if { $line == "OVER" } {
  39. return $r
  40. }
  41. append r $line
  42. }
  43. }
  44. # Write the main loop for the child testfixture processes into file
  45. # tf_main2.tcl. The parent (this script) interacts with the child processes
  46. # via a two way pipe. The parent writes a script to the stdin of the child
  47. # process, followed by the word "OVER" on a line of its own. The child
  48. # process evaluates the script and writes the results to stdout, followed
  49. # by an "OVER" of its own.
  50. set f [open tf_main2.tcl w]
  51. puts $f {
  52. set l [open log w]
  53. set script ""
  54. while {![eof stdin]} {
  55. flush stdout
  56. set line [gets stdin]
  57. puts $l "READ $line"
  58. if { $line == "OVER" } {
  59. catch {eval $script} result
  60. puts $result
  61. puts $l "WRITE $result"
  62. puts OVER
  63. puts $l "WRITE OVER"
  64. flush stdout
  65. set script ""
  66. } else {
  67. append script $line
  68. append script " ; "
  69. }
  70. }
  71. close $l
  72. }
  73. close $f
  74. ifcapable lock_proxy_pragmas&&prefer_proxy_locking {
  75. set sqlite_hostid_num 1
  76. set using_proxy 0
  77. foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
  78. set using_proxy $value
  79. }
  80. # Test the lock_proxy_file pragmas.
  81. #
  82. set env(SQLITE_FORCE_PROXY_LOCKING) "1"
  83. do_test lock6-1.1 {
  84. set ::tf1 [launch_testfixture]
  85. testfixture $::tf1 "sqlite3_test_control_pending_byte $::sqlite_pending_byte"
  86. testfixture $::tf1 {
  87. set sqlite_hostid_num 2
  88. sqlite3 db test.db -key xyzzy
  89. set lockpath [db eval {
  90. PRAGMA lock_proxy_file=":auto:";
  91. select * from sqlite_master;
  92. PRAGMA lock_proxy_file;
  93. }]
  94. string match "*test.db:auto:" $lockpath
  95. }
  96. } {1}
  97. set sqlite_hostid_num 3
  98. do_test lock6-1.2 {
  99. execsql {pragma lock_status}
  100. } {main unlocked temp closed}
  101. sqlite3_soft_heap_limit 0
  102. do_test lock6-1.3 {
  103. list [catch {
  104. sqlite3 db test.db
  105. execsql { select * from sqlite_master }
  106. } msg] $msg
  107. } {1 {database is locked}}
  108. do_test lock6-1.4 {
  109. set lockpath [execsql {
  110. PRAGMA lock_proxy_file=":auto:";
  111. PRAGMA lock_proxy_file;
  112. } db]
  113. set lockpath
  114. } {{:auto: (not held)}}
  115. do_test lock6-1.4.1 {
  116. catchsql {
  117. PRAGMA lock_proxy_file="notmine";
  118. select * from sqlite_master;
  119. } db
  120. } {1 {database is locked}}
  121. do_test lock6-1.4.2 {
  122. execsql {
  123. PRAGMA lock_proxy_file;
  124. } db
  125. } {notmine}
  126. do_test lock6-1.5 {
  127. testfixture $::tf1 {
  128. db eval {
  129. BEGIN;
  130. SELECT * FROM sqlite_master;
  131. }
  132. }
  133. } {}
  134. catch {testfixture $::tf1 {db close}}
  135. do_test lock6-1.6 {
  136. execsql {
  137. PRAGMA lock_proxy_file="mine";
  138. select * from sqlite_master;
  139. } db
  140. } {}
  141. catch {close $::tf1}
  142. set env(SQLITE_FORCE_PROXY_LOCKING) $using_proxy
  143. set sqlite_hostid_num 0
  144. sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
  145. }
  146. finish_test