CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
thread1.test174 linesDownload Raw Back to test
1# 2003 December 182#3# The author disclaims copyright to this source code.  In place of4# 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.  The12# focus of this script is multithreading behavior13#14# $Id: thread1.test,v 1.8 2008/10/07 15:25:49 drh Exp $15 16 17set testdir [file dirname $argv0]18source $testdir/tester.tcl19 20# Skip this whole file if the thread testing code is not enabled21#22if {[run_thread_tests]==0} { finish_test ; return }23if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {24  finish_test25  return26}27 28# Create some data to work with29#30do_test thread1-1.1 {31  execsql {32    CREATE TABLE t1(a,b);33    INSERT INTO t1 VALUES(1,'abcdefgh');34    INSERT INTO t1 SELECT a+1, b||b FROM t1;35    INSERT INTO t1 SELECT a+2, b||b FROM t1;36    INSERT INTO t1 SELECT a+4, b||b FROM t1;37    SELECT count(*), max(length(b)) FROM t1;38  }39} {8 64}40 41# Interleave two threads on read access.  Then make sure a third42# thread can write the database.  In other words:43#44#    read-lock A45#    read-lock B46#    unlock A47#    unlock B48#    write-lock C49#50# At one point, the write-lock of C would fail on Linux. 51#52do_test thread1-1.2 {53  thread_create A test.db54  thread_create B test.db55  thread_create C test.db56  thread_compile A {SELECT a FROM t1}57  thread_step A58  thread_result A59} SQLITE_ROW60do_test thread1-1.3 {61  thread_argc A62} 163do_test thread1-1.4 {64  thread_argv A 065} 166do_test thread1-1.5 {67  thread_compile B {SELECT b FROM t1}68  thread_step B69  thread_result B70} SQLITE_ROW71do_test thread1-1.6 {72  thread_argc B73} 174do_test thread1-1.7 {75  thread_argv B 076} abcdefgh77do_test thread1-1.8 {78  thread_finalize A79  thread_result A80} SQLITE_OK81do_test thread1-1.9 {82  thread_finalize B83  thread_result B84} SQLITE_OK85do_test thread1-1.10 {86  thread_compile C {CREATE TABLE t2(x,y)}87  thread_step C88  thread_result C89} SQLITE_DONE90do_test thread1-1.11 {91  thread_finalize C92  thread_result C93} SQLITE_OK94do_test thread1-1.12 {95  catchsql {SELECT name FROM sqlite_master}96  execsql {SELECT name FROM sqlite_master}97} {t1 t2}98 99 100#101# The following tests - thread1-2.* - test the following scenario:102#103# 1:  Read-lock thread A104# 2:  Read-lock thread B105# 3:  Attempt to write in thread C -> SQLITE_BUSY106# 4:  Check db write failed from main thread.107# 5:  Unlock from thread A.108# 6:  Attempt to write in thread C -> SQLITE_BUSY109# 7:  Check db write failed from main thread.110# 8:  Unlock from thread B.111# 9:  Attempt to write in thread C -> SQLITE_DONE112# 10: Finalize the write from thread C113# 11: Check db write succeeded from main thread.114#115do_test thread1-2.1 {116  thread_halt *117  thread_create A test.db118  thread_compile A {SELECT a FROM t1}119  thread_step A120  thread_result A121} SQLITE_ROW122do_test thread1-2.2 {123  thread_create B test.db124  thread_compile B {SELECT b FROM t1}125  thread_step B126  thread_result B127} SQLITE_ROW128do_test thread1-2.3 {129  thread_create C test.db130  thread_compile C {INSERT INTO t2 VALUES(98,99)}131  thread_step C132  thread_result C133  thread_finalize C134  thread_result C135} SQLITE_BUSY136 137do_test thread1-2.4 {138  execsql {SELECT * FROM t2}139} {}140 141do_test thread1-2.5 {142  thread_finalize A143  thread_result A144} SQLITE_OK145do_test thread1-2.6 {146  thread_compile C {INSERT INTO t2 VALUES(98,99)}147  thread_step C148  thread_result C149  thread_finalize C150  thread_result C151} SQLITE_BUSY152do_test thread1-2.7 {153  execsql {SELECT * FROM t2}154} {}155do_test thread1-2.8 {156  thread_finalize B157  thread_result B158} SQLITE_OK159do_test thread1-2.9 {160  thread_compile C {INSERT INTO t2 VALUES(98,99)}161  thread_step C162  thread_result C163} SQLITE_DONE164do_test thread1-2.10 {165  thread_finalize C166  thread_result C167} SQLITE_OK168do_test thread1-2.11 {169  execsql {SELECT * FROM t2}170} {98 99}171 172thread_halt *   173finish_test174