persistentstorage/sqlite3api/TEST/TclScript/mutex1.test
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 # 2008 June 17
       
     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: mutex1.test,v 1.14 2008/09/03 01:08:02 drh Exp $
       
    13 
       
    14 set testdir [file dirname $argv0]
       
    15 source $testdir/tester.tcl
       
    16 
       
    17 if {[info exists tester_do_binarylog]} {
       
    18   finish_test
       
    19   return
       
    20 }
       
    21 
       
    22 sqlite3_reset_auto_extension
       
    23 
       
    24 proc mutex_counters {varname} {
       
    25   upvar $varname var
       
    26   set var(total) 0
       
    27   foreach {name value} [read_mutex_counters] {
       
    28     set var($name) $value
       
    29     incr var(total) $value
       
    30   }
       
    31 }
       
    32 
       
    33 #-------------------------------------------------------------------------
       
    34 # Tests mutex1-1.* test that sqlite3_config() returns SQLITE_MISUSE if
       
    35 # is called at the wrong time. And that the first time sqlite3_initialize 
       
    36 # is called it obtains the 'static_master' mutex 3 times and a recursive
       
    37 # mutex (sqlite3Config.pInitMutex) twice. Subsequent calls are no-ops 
       
    38 # that do not require any mutexes.
       
    39 #
       
    40 do_test mutex1-1.0 {
       
    41   install_mutex_counters 1
       
    42 } {SQLITE_MISUSE}
       
    43 
       
    44 do_test mutex1-1.1 {
       
    45   db close
       
    46   install_mutex_counters 1
       
    47 } {SQLITE_MISUSE}
       
    48 
       
    49 do_test mutex1-1.2 {
       
    50   sqlite3_shutdown
       
    51   install_mutex_counters 1
       
    52 } {SQLITE_OK}
       
    53 
       
    54 do_test mutex1-1.3 {
       
    55   install_mutex_counters 0
       
    56 } {SQLITE_OK}
       
    57 
       
    58 do_test mutex1-1.4 {
       
    59   install_mutex_counters 1
       
    60 } {SQLITE_OK}
       
    61 
       
    62 do_test mutex1-1.5 {
       
    63   mutex_counters counters
       
    64   set counters(total)
       
    65 } {0}
       
    66 
       
    67 do_test mutex1-1.6 {
       
    68   sqlite3_initialize
       
    69 } {SQLITE_OK}
       
    70 
       
    71 do_test mutex1-1.7 {
       
    72   mutex_counters counters
       
    73   # list $counters(total) $counters(static_master)
       
    74   expr {$counters(total)>0}
       
    75 } {1}
       
    76 
       
    77 do_test mutex1-1.8 {
       
    78   clear_mutex_counters
       
    79   sqlite3_initialize
       
    80 } {SQLITE_OK}
       
    81 
       
    82 do_test mutex1-1.9 {
       
    83   mutex_counters counters
       
    84   list $counters(total) $counters(static_master)
       
    85 } {0 0}
       
    86 
       
    87 #-------------------------------------------------------------------------
       
    88 # Tests mutex1-2.* test the three thread-safety related modes that
       
    89 # can be selected using sqlite3_config:
       
    90 #
       
    91 #   * Serialized mode,
       
    92 #   * Multi-threaded mode,
       
    93 #   * Single-threaded mode.
       
    94 #
       
    95 set enable_shared_cache [sqlite3_enable_shared_cache 1]
       
    96 ifcapable threadsafe {
       
    97   foreach {mode mutexes} {
       
    98     singlethread {}
       
    99     multithread  {fast static_lru static_master static_mem static_prng }
       
   100     serialized  {fast recursive static_lru static_master static_mem static_prng}
       
   101   } {
       
   102 
       
   103     do_test mutex1.2.$mode.1 {
       
   104       catch {db close}
       
   105       sqlite3_shutdown
       
   106       sqlite3_config $mode
       
   107     } SQLITE_OK
       
   108 
       
   109     do_test mutex1.2.$mode.2 {
       
   110       sqlite3_initialize
       
   111       clear_mutex_counters
       
   112       sqlite3 db test.db -nomutex 0
       
   113       catchsql { CREATE TABLE abc(a, b, c) }
       
   114       db eval {
       
   115         INSERT INTO abc VALUES(1, 2, 3);
       
   116       }
       
   117     } {}
       
   118   
       
   119     do_test mutex1.2.$mode.3 {
       
   120       mutex_counters counters
       
   121   
       
   122       set res [list]
       
   123       foreach {key value} [array get counters] {
       
   124         if {$key ne "total" && $value > 0} {
       
   125           lappend res $key
       
   126         }
       
   127       }
       
   128       lsort $res
       
   129     } [lsort $mutexes]
       
   130   }
       
   131 }
       
   132 sqlite3_enable_shared_cache $enable_shared_cache
       
   133 
       
   134 # Open and use a connection in "nomutex" mode. Test that no recursive
       
   135 # mutexes are obtained.
       
   136 ifcapable threadsafe {
       
   137   do_test mutex1.3.1 {
       
   138     catch {db close}
       
   139     clear_mutex_counters
       
   140     sqlite3 db test.db -nomutex 1
       
   141     execsql { SELECT * FROM abc }
       
   142   } {1 2 3 1 2 3 1 2 3}
       
   143   do_test mutex1.3.2 {
       
   144     mutex_counters counters
       
   145     set counters(recursive)
       
   146   } {0}
       
   147 }
       
   148 
       
   149 do_test mutex1-X {
       
   150   catch {db close}
       
   151   sqlite3_shutdown
       
   152   clear_mutex_counters
       
   153   install_mutex_counters 0
       
   154   sqlite3_initialize
       
   155 } {SQLITE_OK}
       
   156 
       
   157 autoinstall_test_functions
       
   158 finish_test