gitlab.com/CoiaPrant/sqlite3@v1.19.1/testdata/tcl/busy.test (about)

     1  # 2005 july 8
     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 test the busy handler
    12  #
    13  
    14  
    15  set testdir [file dirname $argv0]
    16  source $testdir/tester.tcl
    17  set testprefix busy
    18  
    19  do_test busy-1.1 {
    20    sqlite3 db2 test.db
    21    execsql {
    22      CREATE TABLE t1(x);
    23      INSERT INTO t1 VALUES(1);
    24      SELECT * FROM t1
    25    }
    26  } 1
    27  proc busy x {
    28    lappend ::busyargs $x
    29    if {$x>2} {return 1}
    30    return 0
    31  }
    32  set busyargs {}
    33  do_test busy-1.2 {
    34    db busy busy
    35    db2 eval {BEGIN EXCLUSIVE}
    36    catchsql {BEGIN IMMEDIATE}
    37  } {1 {database is locked}}
    38  do_test busy-1.3 {
    39    set busyargs
    40  } {0 1 2 3}
    41  do_test busy-1.4 {
    42    set busyargs {}
    43    catchsql {BEGIN IMMEDIATE}
    44    set busyargs
    45  } {0 1 2 3}
    46  
    47  do_test busy-2.1 {
    48    db2 eval {COMMIT}
    49    db eval {BEGIN; INSERT INTO t1 VALUES(5)}
    50    db2 eval {BEGIN; SELECT * FROM t1}
    51    set busyargs {}
    52    catchsql COMMIT
    53  } {1 {database is locked}}
    54  do_test busy-2.2 {
    55    set busyargs
    56  } {0 1 2 3}
    57  
    58  db2 close
    59  
    60  #-------------------------------------------------------------------------
    61  # Test that the busy-handler is invoked correctly for "PRAGMA optimize"
    62  # and ANALYZE commnds.
    63  ifcapable pragma&&analyze&&!stat4 {
    64  
    65  reset_db
    66  
    67  do_execsql_test 3.1 {
    68    CREATE TABLE t1(x);
    69    CREATE TABLE t2(y);
    70    CREATE TABLE t3(z);
    71  
    72    CREATE INDEX i1 ON t1(x);
    73    CREATE INDEX i2 ON t2(y);
    74  
    75    INSERT INTO t1 VALUES(1);
    76    INSERT INTO t2 VALUES(1);
    77    ANALYZE;
    78  
    79    SELECT * FROM t1 WHERE x=1;
    80    SELECT * FROM t2 WHERE y=1;
    81  } {1 1}
    82  
    83  do_test 3.2 {
    84    sqlite3 db2 test.db
    85    execsql { BEGIN EXCLUSIVE } db2
    86    catchsql { PRAGMA optimize }
    87  } {1 {database is locked}}
    88  
    89  proc busy_handler {n} {
    90    if {$n>1000} { execsql { COMMIT } db2 }
    91    return 0
    92  }
    93  db busy busy_handler
    94  
    95  do_test 3.3 {
    96    catchsql { PRAGMA optimize }
    97  } {0 {}}
    98  
    99  do_test 3.4 {
   100    execsql {
   101      BEGIN;
   102      SELECT count(*) FROM sqlite_master;
   103    } db2
   104  } {6}
   105  
   106  proc busy_handler {n} { return 1 }
   107  do_test 3.5 {
   108    catchsql { PRAGMA optimize }
   109  } {0 {}}
   110  
   111  do_test 3.6 {
   112    execsql { COMMIT } db2
   113    execsql {
   114      WITH s(i) AS (
   115        SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000
   116      )
   117      INSERT INTO t1 SELECT i FROM s;
   118    }
   119    execsql {
   120      BEGIN;
   121      SELECT count(*) FROM sqlite_master;
   122    } db2
   123  } {6}
   124  
   125  do_test 3.7 {
   126    catchsql { PRAGMA optimize }
   127  } {1 {database is locked}}
   128  
   129  proc busy_handler {n} {
   130    if {$n>1000} { execsql { COMMIT } db2 }
   131    return 0
   132  }
   133  do_test 3.8 {
   134    catchsql { PRAGMA optimize }
   135  } {0 {}}
   136  
   137  }
   138  
   139  finish_test