lock4.test 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # 2007 April 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: lock4.test,v 1.10 2009/05/06 00:52:41 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. do_not_use_codec
  18. # Initialize the test.db database so that it is non-empty
  19. #
  20. do_test lock4-1.1 {
  21. db eval {
  22. PRAGMA auto_vacuum=OFF;
  23. CREATE TABLE t1(x);
  24. }
  25. forcedelete test2.db test2.db-journal
  26. sqlite3 db2 test2.db
  27. db2 eval {
  28. PRAGMA auto_vacuum=OFF;
  29. CREATE TABLE t2(x)
  30. }
  31. db2 close
  32. list [file size test.db] [file size test2.db]
  33. } {2048 2048}
  34. # Create a script to drive a separate process that will
  35. #
  36. # 1. Create a second database test2.db
  37. # 2. Get an exclusive lock on test2.db
  38. # 3. Add an entry to test.db in table t1, waiting as necessary.
  39. # 4. Commit the change to test2.db.
  40. #
  41. # Meanwhile, this process will:
  42. #
  43. # A. Get an exclusive lock on test.db
  44. # B. Attempt to read from test2.db but get an SQLITE_BUSY error.
  45. # C. Commit the changes to test.db thus alloing the other process
  46. # to continue.
  47. #
  48. do_test lock4-1.2 {
  49. # Create a script for the second process to run.
  50. #
  51. set out [open test2-script.tcl w]
  52. puts $out "sqlite3_test_control_pending_byte [set sqlite_pending_byte]"
  53. puts $out {
  54. sqlite3 db2 test2.db
  55. db2 eval {
  56. BEGIN;
  57. INSERT INTO t2 VALUES(2);
  58. }
  59. sqlite3 db test.db
  60. db timeout 1000000
  61. db eval {
  62. INSERT INTO t1 VALUES(2);
  63. }
  64. db close
  65. db2 eval COMMIT
  66. exit
  67. }
  68. close $out
  69. # Begin a transaction on test.db.
  70. db eval {
  71. BEGIN EXCLUSIVE;
  72. INSERT INTO t1 VALUES(1);
  73. }
  74. # Kick off the second process.
  75. exec [info nameofexec] ./test2-script.tcl &
  76. # Wait until the second process has started its transaction on test2.db.
  77. while {![file exists test2.db-journal]} {
  78. after 10
  79. }
  80. # Try to write to test2.db. We are locked out.
  81. sqlite3 db2 test2.db
  82. catchsql {
  83. INSERT INTO t2 VALUES(1)
  84. } db2
  85. } {1 {database is locked}}
  86. do_test lock4-1.3 {
  87. db eval {
  88. COMMIT;
  89. }
  90. while {[file exists test2.db-journal]} {
  91. after 10
  92. }
  93. # The other process has committed its transaction on test2.db by
  94. # deleting the journal file. But it might retain the lock for a
  95. # fraction longer
  96. #
  97. after 25
  98. db2 eval {
  99. SELECT * FROM t2
  100. }
  101. } {2}
  102. do_test lock4-999.1 {
  103. rename db2 {}
  104. } {}
  105. finish_test