os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread2.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2006 January 14
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
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this script is multithreading behavior
sl@0
    13
#
sl@0
    14
# $Id: thread2.test,v 1.2 2006/01/18 18:33:42 danielk1977 Exp $
sl@0
    15
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
sl@0
    20
# This file swaps database connections between threads. This
sl@0
    21
# is illegal if memory-management is enabled, so skip this file
sl@0
    22
# in that case.
sl@0
    23
ifcapable memorymanage {
sl@0
    24
  finish_test
sl@0
    25
  return
sl@0
    26
}
sl@0
    27
sl@0
    28
sl@0
    29
# Skip this whole file if the thread testing code is not enabled
sl@0
    30
#
sl@0
    31
if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
sl@0
    32
  finish_test
sl@0
    33
  return
sl@0
    34
}
sl@0
    35
if {![info exists threadsOverrideEachOthersLocks]} {
sl@0
    36
  finish_test
sl@0
    37
  return
sl@0
    38
}
sl@0
    39
sl@0
    40
# Create some data to work with
sl@0
    41
#
sl@0
    42
do_test thread1-1.1 {
sl@0
    43
  execsql {
sl@0
    44
    CREATE TABLE t1(a,b);
sl@0
    45
    INSERT INTO t1 VALUES(1,'abcdefgh');
sl@0
    46
    INSERT INTO t1 SELECT a+1, b||b FROM t1;
sl@0
    47
    INSERT INTO t1 SELECT a+2, b||b FROM t1;
sl@0
    48
    INSERT INTO t1 SELECT a+4, b||b FROM t1;
sl@0
    49
    SELECT count(*), max(length(b)) FROM t1;
sl@0
    50
  }
sl@0
    51
} {8 64}
sl@0
    52
sl@0
    53
# Use the thread_swap command to move the database connections between
sl@0
    54
# threads, then verify that they still work.
sl@0
    55
#
sl@0
    56
do_test thread2-1.2 {
sl@0
    57
  db close
sl@0
    58
  thread_create A test.db
sl@0
    59
  thread_create B test.db
sl@0
    60
  thread_swap A B
sl@0
    61
  thread_compile A {SELECT a FROM t1 LIMIT 1}
sl@0
    62
  thread_result A
sl@0
    63
} {SQLITE_OK}
sl@0
    64
do_test thread2-1.3 {
sl@0
    65
  thread_step A
sl@0
    66
  thread_result A
sl@0
    67
} {SQLITE_ROW}
sl@0
    68
do_test thread2-1.4 {
sl@0
    69
  thread_argv A 0
sl@0
    70
} {1}
sl@0
    71
do_test thread2-1.5 {
sl@0
    72
  thread_finalize A
sl@0
    73
  thread_result A
sl@0
    74
} {SQLITE_OK}
sl@0
    75
do_test thread2-1.6 {
sl@0
    76
  thread_compile B {SELECT a FROM t1 LIMIT 1}
sl@0
    77
  thread_result B
sl@0
    78
} {SQLITE_OK}
sl@0
    79
do_test thread2-1.7 {
sl@0
    80
  thread_step B
sl@0
    81
  thread_result B
sl@0
    82
} {SQLITE_ROW}
sl@0
    83
do_test thread2-1.8 {
sl@0
    84
  thread_argv B 0
sl@0
    85
} {1}
sl@0
    86
do_test thread2-1.9 {
sl@0
    87
  thread_finalize B
sl@0
    88
  thread_result B
sl@0
    89
} {SQLITE_OK}
sl@0
    90
sl@0
    91
# Swap them again.
sl@0
    92
#
sl@0
    93
do_test thread2-2.2 {
sl@0
    94
  thread_swap A B
sl@0
    95
  thread_compile A {SELECT a FROM t1 LIMIT 1}
sl@0
    96
  thread_result A
sl@0
    97
} {SQLITE_OK}
sl@0
    98
do_test thread2-2.3 {
sl@0
    99
  thread_step A
sl@0
   100
  thread_result A
sl@0
   101
} {SQLITE_ROW}
sl@0
   102
do_test thread2-2.4 {
sl@0
   103
  thread_argv A 0
sl@0
   104
} {1}
sl@0
   105
do_test thread2-2.5 {
sl@0
   106
  thread_finalize A
sl@0
   107
  thread_result A
sl@0
   108
} {SQLITE_OK}
sl@0
   109
do_test thread2-2.6 {
sl@0
   110
  thread_compile B {SELECT a FROM t1 LIMIT 1}
sl@0
   111
  thread_result B
sl@0
   112
} {SQLITE_OK}
sl@0
   113
do_test thread2-2.7 {
sl@0
   114
  thread_step B
sl@0
   115
  thread_result B
sl@0
   116
} {SQLITE_ROW}
sl@0
   117
do_test thread2-2.8 {
sl@0
   118
  thread_argv B 0
sl@0
   119
} {1}
sl@0
   120
do_test thread2-2.9 {
sl@0
   121
  thread_finalize B
sl@0
   122
  thread_result B
sl@0
   123
} {SQLITE_OK}
sl@0
   124
thread_halt A
sl@0
   125
thread_halt B
sl@0
   126
sl@0
   127
# Save the original (correct) value of threadsOverrideEachOthersLocks
sl@0
   128
# so that it can be restored.  If this value is left set incorrectly, lots
sl@0
   129
# of things will go wrong in future tests.
sl@0
   130
#
sl@0
   131
set orig_threadOverride $threadsOverrideEachOthersLocks
sl@0
   132
sl@0
   133
# Pretend we are on a system (like RedHat9) were threads do not
sl@0
   134
# override each others locks.
sl@0
   135
#
sl@0
   136
set threadsOverrideEachOthersLocks 0
sl@0
   137
sl@0
   138
# Verify that we can move database connections between threads as
sl@0
   139
# long as no locks are held.
sl@0
   140
#
sl@0
   141
do_test thread2-3.1 {
sl@0
   142
  thread_create A test.db
sl@0
   143
  set DB [thread_db_get A]
sl@0
   144
  thread_halt A
sl@0
   145
} {}
sl@0
   146
do_test thread2-3.2 {
sl@0
   147
  set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
sl@0
   148
  sqlite3_step $STMT
sl@0
   149
} SQLITE_ROW
sl@0
   150
do_test thread2-3.3 {
sl@0
   151
  sqlite3_column_int $STMT 0
sl@0
   152
} 1
sl@0
   153
do_test thread2-3.4 {
sl@0
   154
  sqlite3_finalize $STMT
sl@0
   155
} SQLITE_OK
sl@0
   156
do_test thread2-3.5 {
sl@0
   157
  set STMT [sqlite3_prepare $DB {SELECT max(a) FROM t1} -1 TAIL]
sl@0
   158
  sqlite3_step $STMT
sl@0
   159
} SQLITE_ROW
sl@0
   160
do_test thread2-3.6 {
sl@0
   161
  sqlite3_column_int $STMT 0
sl@0
   162
} 8
sl@0
   163
do_test thread2-3.7 {
sl@0
   164
  sqlite3_finalize $STMT
sl@0
   165
} SQLITE_OK
sl@0
   166
do_test thread2-3.8 {
sl@0
   167
  sqlite3_close $DB
sl@0
   168
} {SQLITE_OK}
sl@0
   169
sl@0
   170
do_test thread2-3.10 {
sl@0
   171
  thread_create A test.db
sl@0
   172
  thread_compile A {SELECT a FROM t1 LIMIT 1}
sl@0
   173
  thread_step A
sl@0
   174
  thread_finalize A
sl@0
   175
  set DB [thread_db_get A]
sl@0
   176
  thread_halt A
sl@0
   177
} {}
sl@0
   178
do_test thread2-3.11 {
sl@0
   179
  set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
sl@0
   180
  sqlite3_step $STMT
sl@0
   181
} SQLITE_ROW
sl@0
   182
do_test thread2-3.12 {
sl@0
   183
  sqlite3_column_int $STMT 0
sl@0
   184
} 1
sl@0
   185
do_test thread2-3.13 {
sl@0
   186
  sqlite3_finalize $STMT
sl@0
   187
} SQLITE_OK
sl@0
   188
do_test thread2-3.14 {
sl@0
   189
  sqlite3_close $DB
sl@0
   190
} SQLITE_OK
sl@0
   191
sl@0
   192
do_test thread2-3.20 {
sl@0
   193
  thread_create A test.db
sl@0
   194
  thread_compile A {SELECT a FROM t1 LIMIT 3}
sl@0
   195
  thread_step A
sl@0
   196
  set STMT [thread_stmt_get A]
sl@0
   197
  set DB [thread_db_get A]
sl@0
   198
  thread_halt A
sl@0
   199
} {}
sl@0
   200
do_test thread2-3.21 {
sl@0
   201
  sqlite3_step $STMT
sl@0
   202
} SQLITE_ROW
sl@0
   203
do_test thread2-3.22 {
sl@0
   204
  sqlite3_column_int $STMT 0
sl@0
   205
} 2
sl@0
   206
do_test thread2-3.23 {
sl@0
   207
  # The unlock fails here.  But because we never check the return
sl@0
   208
  # code from sqlite3OsUnlock (because we cannot do anything about it
sl@0
   209
  # if it fails) we do not realize that an error has occurred.
sl@0
   210
  sqlite3_finalize $STMT
sl@0
   211
} SQLITE_OK
sl@0
   212
do_test thread2-3.25 {
sl@0
   213
  sqlite3_close $DB
sl@0
   214
} SQLITE_OK
sl@0
   215
sl@0
   216
do_test thread2-3.30 {
sl@0
   217
  thread_create A test.db
sl@0
   218
  thread_compile A {BEGIN}
sl@0
   219
  thread_step A
sl@0
   220
  thread_finalize A
sl@0
   221
  thread_compile A {SELECT a FROM t1 LIMIT 1}
sl@0
   222
  thread_step A
sl@0
   223
  thread_finalize A
sl@0
   224
  set DB [thread_db_get A]
sl@0
   225
  thread_halt A
sl@0
   226
} {}
sl@0
   227
do_test thread2-3.31 {
sl@0
   228
  set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(99,'error')} -1 TAIL]
sl@0
   229
  sqlite3_step $STMT
sl@0
   230
} SQLITE_ERROR
sl@0
   231
do_test thread2-3.32 {
sl@0
   232
  sqlite3_finalize $STMT
sl@0
   233
} SQLITE_MISUSE
sl@0
   234
do_test thread2-3.33 {
sl@0
   235
  sqlite3_close $DB
sl@0
   236
} SQLITE_OK
sl@0
   237
sl@0
   238
# VERY important to set the override flag back to its true value.
sl@0
   239
#
sl@0
   240
set threadsOverrideEachOthersLocks $orig_threadOverride
sl@0
   241
sl@0
   242
# Also important to halt the worker threads, which are using spin
sl@0
   243
# locks and eating away CPU cycles.
sl@0
   244
#
sl@0
   245
thread_halt *   
sl@0
   246
finish_test