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

     1  # 2006 January 09
     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.  The
    12  # focus of this script is testing the server mode of SQLite.
    13  #
    14  # This file is derived from thread1.test
    15  #
    16  # $Id: server1.test,v 1.5 2007/08/29 18:20:17 drh Exp $
    17  
    18  
    19  set testdir [file dirname $argv0]
    20  source $testdir/tester.tcl
    21  
    22  # Skip this whole file if the server testing code is not enabled
    23  #
    24  if {[llength [info command client_step]]==0 || [sqlite3 -has-codec]} {
    25    finish_test
    26    return
    27  }
    28  
    29  # This test does not work on older PPC Macs due to problems in the
    30  # pthreads library.  So skip it.
    31  #
    32  if {$tcl_platform(machine)=="Power Macintosh" && 
    33      $tcl_platform(byteOrder)=="bigEndian"} {
    34    finish_test
    35    return
    36  }
    37  
    38  # The sample server implementation does not work right when memory
    39  # management is enabled.
    40  #
    41  ifcapable (memorymanage||mutex_noop) {
    42    finish_test
    43    return
    44  }
    45  
    46  # Create some data to work with
    47  #
    48  do_test server1-1.1 {
    49    execsql {
    50      CREATE TABLE t1(a,b);
    51      INSERT INTO t1 VALUES(1,'abcdefgh');
    52      INSERT INTO t1 SELECT a+1, b||b FROM t1;
    53      INSERT INTO t1 SELECT a+2, b||b FROM t1;
    54      INSERT INTO t1 SELECT a+4, b||b FROM t1;
    55      SELECT count(*), max(length(b)) FROM t1;
    56    }
    57  } {8 64}
    58  
    59  # Interleave two threads on read access.  Then make sure a third
    60  # thread can write the database.  In other words:
    61  #
    62  #    read-lock A
    63  #    read-lock B
    64  #    unlock A
    65  #    unlock B
    66  #    write-lock C
    67  #
    68  do_test server1-1.2 {
    69    client_create A test.db
    70    client_create B test.db
    71    client_create C test.db
    72    client_compile A {SELECT a FROM t1}
    73    client_step A
    74    client_result A
    75  } SQLITE_ROW
    76  do_test server1-1.3 {
    77    client_argc A
    78  } 1
    79  do_test server1-1.4 {
    80    client_argv A 0
    81  } 1
    82  do_test server1-1.5 {
    83    client_compile B {SELECT b FROM t1}
    84    client_step B
    85    client_result B
    86  } SQLITE_ROW
    87  do_test server1-1.6 {
    88    client_argc B
    89  } 1
    90  do_test server1-1.7 {
    91    client_argv B 0
    92  } abcdefgh
    93  do_test server1-1.8 {
    94    client_finalize A
    95    client_result A
    96  } SQLITE_OK
    97  do_test server1-1.9 {
    98    client_finalize B
    99    client_result B
   100  } SQLITE_OK
   101  do_test server1-1.10 {
   102    client_compile C {CREATE TABLE t2(x,y)}
   103    client_step C
   104    client_result C
   105  } SQLITE_DONE
   106  do_test server1-1.11 {
   107    client_finalize C
   108    client_result C
   109  } SQLITE_OK
   110  do_test server1-1.12 {
   111    catchsql {SELECT name FROM sqlite_master}
   112    execsql {SELECT name FROM sqlite_master}
   113  } {t1 t2}
   114  
   115  
   116  # Read from table t1.  Do not finalize the statement.  This
   117  # will leave the lock pending.
   118  #
   119  do_test server1-2.1 {
   120    client_halt *
   121    client_create A test.db
   122    client_compile A {SELECT a FROM t1}
   123    client_step A
   124    client_result A
   125  } SQLITE_ROW
   126  
   127  # Read from the same table from another thread.  This is allows.
   128  #
   129  do_test server1-2.2 {
   130    client_create B test.db
   131    client_compile B {SELECT b FROM t1}
   132    client_step B
   133    client_result B
   134  } SQLITE_ROW
   135  
   136  # Write to a different table from another thread.  This is allowed
   137  # because in server mode with a shared cache we have table-level locking.
   138  #
   139  do_test server1-2.3 {
   140    client_create C test.db
   141    client_compile C {INSERT INTO t2 VALUES(98,99)}
   142    client_step C
   143    client_result C
   144    client_finalize C
   145    client_result C
   146  } SQLITE_OK
   147  
   148  # But we cannot insert into table t1 because threads A and B have it locked.
   149  #
   150  do_test server1-2.4 {
   151    client_compile C {INSERT INTO t1 VALUES(98,99)}
   152    client_step C
   153    client_result C
   154    client_finalize C
   155    client_result C
   156  } SQLITE_LOCKED
   157  do_test server1-2.5 {
   158    client_finalize B
   159    client_wait B
   160    client_compile C {INSERT INTO t1 VALUES(98,99)}
   161    client_step C
   162    client_result C
   163    client_finalize C
   164    client_result C
   165  } SQLITE_LOCKED
   166  
   167  # Insert into t1 is successful after finishing the other two threads.
   168  do_test server1-2.6 {
   169    client_finalize A
   170    client_wait A
   171    client_compile C {INSERT INTO t1 VALUES(98,99)}
   172    client_step C
   173    client_result C
   174    client_finalize C
   175    client_result C
   176  } SQLITE_OK
   177  
   178  client_halt *   
   179  sqlite3_enable_shared_cache 0
   180  finish_test