tkt4018.test 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # 2009 August 20
  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.
  12. #
  13. # This file implements tests to verify that ticket #4018 has been
  14. # fixed.
  15. #
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. proc testsql {sql} {
  19. set fd [open tf_main.tcl w]
  20. puts $fd [subst -nocommands {
  21. sqlite3_test_control_pending_byte 0x0010000
  22. sqlite3 db test.db
  23. set rc [catch { db eval {$sql} } msg]
  24. puts -nonewline "[set rc] {[set msg]}"
  25. flush stdout
  26. exit
  27. }]
  28. close $fd
  29. set fd [open "| [info nameofexec] ./tf_main.tcl" r]
  30. set res [read $fd]
  31. close $fd
  32. return $res
  33. }
  34. do_test tkt4018-1.1 {
  35. execsql {
  36. CREATE TABLE t1(a, b);
  37. BEGIN;
  38. SELECT * FROM t1;
  39. }
  40. } {}
  41. # The database is locked by connection [db]. Open and close a second
  42. # connection to test.db 10000 times. If file-descriptors are not being
  43. # reused, then the process will quickly exceed its maximum number of
  44. # file descriptors (1024 by default on linux).
  45. do_test tkt4018-1.2 {
  46. for {set i 0} {$i < 10000} {incr i} {
  47. sqlite3 db2 test.db
  48. db2 close
  49. }
  50. } {}
  51. # Now check that connection [db] is still holding a SHARED lock by
  52. # having a second process try to write the db.
  53. do_test tkt4018-1.3 {
  54. testsql {INSERT INTO t1 VALUES(3, 4)}
  55. } {1 {database is locked}}
  56. # Sanity checking. Have [db] release the lock and then retry the
  57. # INSERT from the previous test case.
  58. do_test tkt4018-1.4 {
  59. db eval COMMIT
  60. testsql {INSERT INTO t1 VALUES(3, 4)}
  61. } {0 {}}
  62. # Check that reusing a file descriptor cannot change a read-only
  63. # connection into a read-write connection.
  64. do_test tkt4018-2.1 {
  65. sqlite3 db2 test.db
  66. execsql {INSERT INTO t1 VALUES(1, 2)} db2
  67. } {}
  68. do_test tkt4018-2.2 {
  69. execsql {
  70. BEGIN;
  71. SELECT * FROM t1 ORDER BY a;
  72. }
  73. } {1 2 3 4}
  74. do_test tkt4018-2.3 {
  75. db2 close
  76. sqlite3 db2 test.db -readonly 1
  77. execsql COMMIT
  78. catchsql {INSERT INTO t1 VALUES(5, 6)} db2
  79. } {1 {attempt to write a readonly database}}
  80. db2 close
  81. finish_test