os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/lock4.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/lock4.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,115 @@
     1.4 +# 2007 April 6
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +# This file implements regression tests for SQLite library.  The
    1.15 +# focus of this script is database locks.
    1.16 +#
    1.17 +# $Id: lock4.test,v 1.8 2008/03/14 08:57:42 danielk1977 Exp $
    1.18 +
    1.19 +
    1.20 +set testdir [file dirname $argv0]
    1.21 +source $testdir/tester.tcl
    1.22 +
    1.23 +# Initialize the test.db database so that it is non-empty
    1.24 +#
    1.25 +do_test lock4-1.1 {
    1.26 +  db eval {
    1.27 +     PRAGMA auto_vacuum=OFF;
    1.28 +     CREATE TABLE t1(x);
    1.29 +  }
    1.30 +  file delete -force test2.db test2.db-journal
    1.31 +  sqlite3 db2 test2.db
    1.32 +  db2 eval {
    1.33 +     PRAGMA auto_vacuum=OFF;
    1.34 +     CREATE TABLE t2(x)
    1.35 +  }
    1.36 +  db2 close
    1.37 +  list [file size test.db] [file size test2.db]
    1.38 +} {2048 2048}
    1.39 +
    1.40 +# Create a script to drive a separate process that will
    1.41 +#
    1.42 +#     1.  Create a second database test2.db
    1.43 +#     2.  Get an exclusive lock on test2.db
    1.44 +#     3.  Add an entry to test.db in table t1, waiting as necessary.
    1.45 +#     4.  Commit the change to test2.db.
    1.46 +#
    1.47 +# Meanwhile, this process will:
    1.48 +# 
    1.49 +#     A.  Get an exclusive lock on test.db
    1.50 +#     B.  Attempt to read from test2.db but get an SQLITE_BUSY error.
    1.51 +#     C.  Commit the changes to test.db thus alloing the other process
    1.52 +#         to continue.
    1.53 +#
    1.54 +do_test lock4-1.2 {
    1.55 + 
    1.56 +  # Create a script for the second process to run.
    1.57 +  #
    1.58 +  set out [open test2-script.tcl w]
    1.59 +  puts $out "set sqlite_pending_byte [set sqlite_pending_byte]"
    1.60 +  puts $out {
    1.61 +     sqlite3 db2 test2.db
    1.62 +     db2 eval {
    1.63 +        BEGIN;
    1.64 +        INSERT INTO t2 VALUES(2);
    1.65 +     }
    1.66 +     sqlite3 db test.db
    1.67 +     db timeout 1000000
    1.68 +     db eval {
    1.69 +        INSERT INTO t1 VALUES(2);
    1.70 +     }
    1.71 +     db close
    1.72 +     db2 eval COMMIT
    1.73 +     exit
    1.74 +  }
    1.75 +  close $out
    1.76 +
    1.77 +  # Begin a transaction on test.db.
    1.78 +  db eval {
    1.79 +     BEGIN EXCLUSIVE;
    1.80 +     INSERT INTO t1 VALUES(1);
    1.81 +  }
    1.82 +
    1.83 +  # Kick off the second process.
    1.84 +  exec [info nameofexec] ./test2-script.tcl &
    1.85 +
    1.86 +  # Wait until the second process has started its transaction on test2.db.
    1.87 +  while {![file exists test2.db-journal]} {
    1.88 +    after 10
    1.89 +  }
    1.90 +
    1.91 +  # Try to write to test2.db. We are locked out.
    1.92 +  sqlite3 db2 test2.db
    1.93 +  catchsql {
    1.94 +    INSERT INTO t2 VALUES(1)
    1.95 +  } db2
    1.96 +} {1 {database is locked}}
    1.97 +do_test lock4-1.3 {
    1.98 +  db eval {
    1.99 +     COMMIT;
   1.100 +  }
   1.101 +  while {[file exists test2.db-journal]} {
   1.102 +    after 10
   1.103 +  }
   1.104 +  # The other process has committed its transaction on test2.db by 
   1.105 +  # deleting the journal file. But it might retain the lock for a 
   1.106 +  # fraction longer 
   1.107 +  #
   1.108 +  db2 eval {
   1.109 +     SELECT * FROM t2
   1.110 +  }
   1.111 +} {2}
   1.112 +
   1.113 +    
   1.114 +do_test lock4-999.1 {
   1.115 +  rename db2 {}
   1.116 +} {}
   1.117 +
   1.118 +finish_test