os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/mutex1.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/mutex1.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,158 @@
     1.4 +# 2008 June 17
     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 +#
    1.15 +# $Id: mutex1.test,v 1.14 2008/09/03 01:08:02 drh Exp $
    1.16 +
    1.17 +set testdir [file dirname $argv0]
    1.18 +source $testdir/tester.tcl
    1.19 +
    1.20 +if {[info exists tester_do_binarylog]} {
    1.21 +  finish_test
    1.22 +  return
    1.23 +}
    1.24 +
    1.25 +sqlite3_reset_auto_extension
    1.26 +
    1.27 +proc mutex_counters {varname} {
    1.28 +  upvar $varname var
    1.29 +  set var(total) 0
    1.30 +  foreach {name value} [read_mutex_counters] {
    1.31 +    set var($name) $value
    1.32 +    incr var(total) $value
    1.33 +  }
    1.34 +}
    1.35 +
    1.36 +#-------------------------------------------------------------------------
    1.37 +# Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if
    1.38 +# is called at the wrong time. And that the first time sqlite3_initialize 
    1.39 +# is called it obtains the 'static_master' mutex 3 times and a recursive
    1.40 +# mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops 
    1.41 +# that do not require any mutexes.
    1.42 +#
    1.43 +do_test mutex1-1.0 {
    1.44 +  install_mutex_counters 1
    1.45 +} {SQLITE_MISUSE}
    1.46 +
    1.47 +do_test mutex1-1.1 {
    1.48 +  db close
    1.49 +  install_mutex_counters 1
    1.50 +} {SQLITE_MISUSE}
    1.51 +
    1.52 +do_test mutex1-1.2 {
    1.53 +  sqlite3_shutdown
    1.54 +  install_mutex_counters 1
    1.55 +} {SQLITE_OK}
    1.56 +
    1.57 +do_test mutex1-1.3 {
    1.58 +  install_mutex_counters 0
    1.59 +} {SQLITE_OK}
    1.60 +
    1.61 +do_test mutex1-1.4 {
    1.62 +  install_mutex_counters 1
    1.63 +} {SQLITE_OK}
    1.64 +
    1.65 +do_test mutex1-1.5 {
    1.66 +  mutex_counters counters
    1.67 +  set counters(total)
    1.68 +} {0}
    1.69 +
    1.70 +do_test mutex1-1.6 {
    1.71 +  sqlite3_initialize
    1.72 +} {SQLITE_OK}
    1.73 +
    1.74 +do_test mutex1-1.7 {
    1.75 +  mutex_counters counters
    1.76 +  # list $counters(total) $counters(static_master)
    1.77 +  expr {$counters(total)>0}
    1.78 +} {1}
    1.79 +
    1.80 +do_test mutex1-1.8 {
    1.81 +  clear_mutex_counters
    1.82 +  sqlite3_initialize
    1.83 +} {SQLITE_OK}
    1.84 +
    1.85 +do_test mutex1-1.9 {
    1.86 +  mutex_counters counters
    1.87 +  list $counters(total) $counters(static_master)
    1.88 +} {0 0}
    1.89 +
    1.90 +#-------------------------------------------------------------------------
    1.91 +# Tests mutex1-2.* test the three thread-safety related modes that
    1.92 +# can be selected using sqlite3_config:
    1.93 +#
    1.94 +#   * Serialized mode,
    1.95 +#   * Multi-threaded mode,
    1.96 +#   * Single-threaded mode.
    1.97 +#
    1.98 +set enable_shared_cache [sqlite3_enable_shared_cache 1]
    1.99 +ifcapable threadsafe {
   1.100 +  foreach {mode mutexes} {
   1.101 +    singlethread {}
   1.102 +    multithread  {fast static_lru static_master static_mem static_prng }
   1.103 +    serialized  {fast recursive static_lru static_master static_mem static_prng}
   1.104 +  } {
   1.105 +
   1.106 +    do_test mutex1.2.$mode.1 {
   1.107 +      catch {db close}
   1.108 +      sqlite3_shutdown
   1.109 +      sqlite3_config $mode
   1.110 +    } SQLITE_OK
   1.111 +
   1.112 +    do_test mutex1.2.$mode.2 {
   1.113 +      sqlite3_initialize
   1.114 +      clear_mutex_counters
   1.115 +      sqlite3 db test.db -nomutex 0
   1.116 +      catchsql { CREATE TABLE abc(a, b, c) }
   1.117 +      db eval {
   1.118 +        INSERT INTO abc VALUES(1, 2, 3);
   1.119 +      }
   1.120 +    } {}
   1.121 +  
   1.122 +    do_test mutex1.2.$mode.3 {
   1.123 +      mutex_counters counters
   1.124 +  
   1.125 +      set res [list]
   1.126 +      foreach {key value} [array get counters] {
   1.127 +        if {$key ne "total" && $value > 0} {
   1.128 +          lappend res $key
   1.129 +        }
   1.130 +      }
   1.131 +      lsort $res
   1.132 +    } [lsort $mutexes]
   1.133 +  }
   1.134 +}
   1.135 +sqlite3_enable_shared_cache $enable_shared_cache
   1.136 +
   1.137 +# Open and use a connection in "nomutex" mode. Test that no recursive
   1.138 +# mutexes are obtained.
   1.139 +ifcapable threadsafe {
   1.140 +  do_test mutex1.3.1 {
   1.141 +    catch {db close}
   1.142 +    clear_mutex_counters
   1.143 +    sqlite3 db test.db -nomutex 1
   1.144 +    execsql { SELECT * FROM abc }
   1.145 +  } {1 2 3 1 2 3 1 2 3}
   1.146 +  do_test mutex1.3.2 {
   1.147 +    mutex_counters counters
   1.148 +    set counters(recursive)
   1.149 +  } {0}
   1.150 +}
   1.151 +
   1.152 +do_test mutex1-X {
   1.153 +  catch {db close}
   1.154 +  sqlite3_shutdown
   1.155 +  clear_mutex_counters
   1.156 +  install_mutex_counters 0
   1.157 +  sqlite3_initialize
   1.158 +} {SQLITE_OK}
   1.159 +
   1.160 +autoinstall_test_functions
   1.161 +finish_test