sharedlock.test 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # 2009 July 2
  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. # $Id: sharedlock.test,v 1.1 2009/07/02 17:21:58 danielk1977 Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. set testprefix sharedlock
  16. db close
  17. ifcapable !shared_cache {
  18. finish_test
  19. return
  20. }
  21. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  22. sqlite3 db test.db
  23. sqlite3 db2 test.db
  24. do_test sharedlock-1.1 {
  25. execsql {
  26. CREATE TABLE t1(a, b);
  27. INSERT INTO t1 VALUES(1, 'one');
  28. INSERT INTO t1 VALUES(2, 'two');
  29. }
  30. } {}
  31. do_test sharedlock-1.2 {
  32. set res [list]
  33. db eval { SELECT * FROM t1 ORDER BY rowid } {
  34. lappend res $a $b
  35. if {$a == 1} { catch { db eval "INSERT INTO t1 VALUES(3, 'three')" } }
  36. # This should fail. Connection [db] has a read-lock on t1, which should
  37. # prevent connection [db2] from obtaining the write-lock it needs to
  38. # modify t1. At one point there was a bug causing the previous INSERT
  39. # to drop the read-lock belonging to [db].
  40. if {$a == 2} { catch { db2 eval "INSERT INTO t1 VALUES(4, 'four')" } }
  41. }
  42. set res
  43. } {1 one 2 two 3 three}
  44. #-------------------------------------------------------------------------
  45. # Test that a write-lock is taken on a table when its entire contents
  46. # are deleted using the OP_Clear optimization.
  47. #
  48. foreach {tn delete_sql} {
  49. 1 { DELETE FROM t2 WHERE 1 }
  50. 2 { DELETE FROM t2 }
  51. } {
  52. do_execsql_test 2.1 {
  53. DROP TABLE IF EXISTS t2;
  54. CREATE TABLE t2(x, y);
  55. INSERT INTO t2 VALUES(1, 2);
  56. INSERT INTO t2 VALUES(3, 4);
  57. }
  58. do_test 2.2 { execsql { SELECT * FROM t2 } db2 } {1 2 3 4}
  59. do_execsql_test 2.3 " BEGIN; $delete_sql; "
  60. do_test 2.4 {
  61. catchsql { SELECT * FROM t2 } db2
  62. } {1 {database table is locked: t2}}
  63. do_execsql_test 2.5 COMMIT
  64. }
  65. db close
  66. db2 close
  67. sqlite3_enable_shared_cache $::enable_shared_cache
  68. finish_test