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