unixexcl.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # 2011 March 30
  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 file contains tests for the "unix-excl" VFS module (part of
  13. # os_unix.c).
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. source $testdir/lock_common.tcl
  18. source $testdir/malloc_common.tcl
  19. if {$::tcl_platform(platform)!="unix" || [info commands test_syscall]==""} {
  20. finish_test
  21. return
  22. }
  23. set testprefix unixexcl
  24. # Test that when using VFS "unix-excl", the first time the database is read
  25. # a process-wide exclusive lock is taken on it. This means other connections
  26. # within the process may still access the db normally, but connections from
  27. # outside the process cannot.
  28. #
  29. do_multiclient_test tn {
  30. do_test unixexcl-1.$tn.1 {
  31. sql1 {
  32. CREATE TABLE t1(a, b);
  33. INSERT INTO t1 VALUES('hello', 'world');
  34. }
  35. } {}
  36. do_test unixexcl-1.$tn.2 { sql2 { SELECT * FROM t1 } } {hello world}
  37. do_test unixexcl-1.$tn.3 {
  38. code1 {
  39. db close
  40. sqlite3 db test.db -vfs unix-excl
  41. db eval { SELECT * FROM t1 }
  42. }
  43. } {hello world}
  44. if {$tn==1} {
  45. do_test unixexcl-1.$tn.4.multiproc {
  46. csql2 { SELECT * FROM t1 }
  47. } {1 {database is locked}}
  48. } else {
  49. do_test unixexcl-1.$tn.4.singleproc {
  50. csql2 { SELECT * FROM t1 }
  51. } {0 {hello world}}
  52. }
  53. }
  54. # Test that when using VFS "unix-excl", if a file is opened in read-only mode
  55. # the behaviour is the same as if VFS "unix" were used.
  56. #
  57. do_multiclient_test tn {
  58. do_test unixexcl-2.$tn.1 {
  59. sql1 {
  60. CREATE TABLE t1(a, b);
  61. INSERT INTO t1 VALUES('hello', 'world');
  62. }
  63. } {}
  64. do_test unixexcl-2.$tn.2 { sql2 { SELECT * FROM t1 } } {hello world}
  65. do_test unixexcl-2.$tn.3 {
  66. code1 {
  67. db close
  68. sqlite3 db test.db -readonly yes -vfs unix-excl
  69. db eval { SELECT * FROM t1 }
  70. }
  71. } {hello world}
  72. do_test unixexcl-2.$tn.4 {
  73. csql2 { SELECT * FROM t1 }
  74. } {0 {hello world}}
  75. }
  76. do_multiclient_test tn {
  77. do_test unixexcl-3.$tn.1 {
  78. code1 { db close; sqlite3 db file:test.db?psow=0 -vfs unix-excl -uri 1 }
  79. code2 { db2 close; sqlite3 db2 file:test.db?psow=0 -vfs unix-excl -uri 1 }
  80. sql1 {
  81. PRAGMA auto_vacuum = 0;
  82. PRAGMA journal_mode = WAL;
  83. CREATE TABLE t1(a, b);
  84. INSERT INTO t1 VALUES(1, 2);
  85. }
  86. } {wal}
  87. if {$tn==1} {
  88. do_test unixexcl-3.$tn.1.multiproc {
  89. csql2 { SELECT * FROM t1; }
  90. } {1 {database is locked}}
  91. } else {
  92. do_test unixexcl-3.$tn.1.singleproc {
  93. sql2 { SELECT * FROM t1; }
  94. } {1 2}
  95. do_test unixexcl-3.$tn.2 {
  96. sql2 {
  97. BEGIN;
  98. SELECT * FROM t1;
  99. }
  100. } {1 2}
  101. do_test unixexcl-3.$tn.3 {
  102. sql1 { PRAGMA wal_checkpoint; INSERT INTO t1 VALUES(3, 4); }
  103. } {0 3 3}
  104. do_test unixexcl-3.$tn.4 {
  105. sql2 { SELECT * FROM t1; }
  106. } {1 2}
  107. do_test unixexcl-3.$tn.5 {
  108. sql1 { SELECT * FROM t1; }
  109. } {1 2 3 4}
  110. do_test unixexcl-3.$tn.6 {
  111. sql2 { COMMIT; SELECT * FROM t1; }
  112. } {1 2 3 4}
  113. do_test unixexcl-3.$tn.7 {
  114. sql1 { PRAGMA wal_checkpoint; }
  115. } {0 4 4}
  116. }
  117. }
  118. finish_test