jrnlmode2.test 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # 2009 March 24
  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. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. ifcapable {!pager_pragmas} {
  15. finish_test
  16. return
  17. }
  18. #-------------------------------------------------------------------------
  19. # The tests in this file check that the following two bugs (both now fixed)
  20. # do not reappear.
  21. #
  22. # jrnlmode2-1.*: Demonstrate bug #3745:
  23. #
  24. # In persistent journal mode, if:
  25. #
  26. # * There is a persistent journal in the file-system, AND
  27. # * there exists a connection with a shared lock on the db file,
  28. #
  29. # then a second connection cannot open a read-transaction on the database.
  30. # The reason is because while determining that the persistent-journal is
  31. # not a hot-journal, SQLite currently grabs an exclusive lock on the
  32. # database file. If this fails because another connection has a shared
  33. # lock, then SQLITE_BUSY is returned to the user.
  34. #
  35. # jrnlmode2-2.*: Demonstrate bug #3751:
  36. #
  37. # If a connection is opened in SQLITE_OPEN_READONLY mode, the underlying
  38. # unix file descriptor on the database file is opened in O_RDONLY mode.
  39. #
  40. # When SQLite queries the database file for the schema in order to compile
  41. # the SELECT statement, it sees the empty journal in the file system, it
  42. # attempts to obtain an exclusive lock on the database file (this is a
  43. # bug). The attempt to obtain an exclusive (write) lock on a read-only file
  44. # fails at the OS level. Under unix, fcntl() reports an EBADF - "Bad file
  45. # descriptor" - error.
  46. #
  47. do_test jrnlmode2-1.1 {
  48. execsql {
  49. PRAGMA journal_mode = persist;
  50. CREATE TABLE t1(a, b);
  51. INSERT INTO t1 VALUES(1, 2);
  52. }
  53. } {persist}
  54. do_test jrnlmode2-1.2 {
  55. file exists test.db-journal
  56. } {1}
  57. do_test jrnlmode2-1.3 {
  58. sqlite3 db2 test.db
  59. execsql { SELECT * FROM t1 } db2
  60. } {1 2}
  61. do_test jrnlmode2-1.4 {
  62. execsql {
  63. INSERT INTO t1 VALUES(3, 4);
  64. }
  65. execsql {
  66. BEGIN;
  67. SELECT * FROM t1;
  68. }
  69. execsql { PRAGMA lock_status }
  70. } {main shared temp closed}
  71. do_test jrnlmode2-1.5 {
  72. file exists test.db-journal
  73. } {1}
  74. do_test jrnlmode2-1.6 {
  75. catchsql { SELECT * FROM t1 } db2
  76. } {0 {1 2 3 4}}
  77. do_test jrnlmode2-1.7 {
  78. execsql { COMMIT }
  79. catchsql { SELECT * FROM t1 } db2
  80. } {0 {1 2 3 4}}
  81. do_test jrnlmode2-2.1 {
  82. db2 close
  83. execsql { PRAGMA journal_mode = truncate }
  84. execsql { INSERT INTO t1 VALUES(5, 6) }
  85. } {}
  86. do_test jrnlmode2-2.2 {
  87. file exists test.db-journal
  88. } {1}
  89. do_test jrnlmode2-2.3 {
  90. file size test.db-journal
  91. } {0}
  92. do_test jrnlmode2-2.4 {
  93. sqlite3 db2 test.db -readonly 1
  94. catchsql { SELECT * FROM t1 } db2
  95. } {0 {1 2 3 4 5 6}}
  96. do_test jrnlmode2-2.5 {
  97. db close
  98. delete_file test.db-journal
  99. } {}
  100. do_test jrnlmode2-2.6 {
  101. sqlite3 db2 test.db -readonly 1
  102. catchsql { SELECT * FROM t1 } db2
  103. } {0 {1 2 3 4 5 6}}
  104. catch { db2 close }
  105. finish_test