drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 1 | # 2006 January 14 |
| 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 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this script is multithreading behavior |
| 13 | # |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 14 | # $Id: thread2.test,v 1.3 2008/10/07 15:25:49 drh Exp $ |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 20 | ifcapable !mutex { |
danielk1977 | e7823cb | 2006-01-18 18:33:42 +0000 | [diff] [blame] | 21 | finish_test |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 26 | # Skip this whole file if the thread testing code is not enabled |
| 27 | # |
| 28 | if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} { |
| 29 | finish_test |
| 30 | return |
| 31 | } |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 32 | |
| 33 | # Create some data to work with |
| 34 | # |
| 35 | do_test thread1-1.1 { |
| 36 | execsql { |
| 37 | CREATE TABLE t1(a,b); |
| 38 | INSERT INTO t1 VALUES(1,'abcdefgh'); |
| 39 | INSERT INTO t1 SELECT a+1, b||b FROM t1; |
| 40 | INSERT INTO t1 SELECT a+2, b||b FROM t1; |
| 41 | INSERT INTO t1 SELECT a+4, b||b FROM t1; |
| 42 | SELECT count(*), max(length(b)) FROM t1; |
| 43 | } |
| 44 | } {8 64} |
| 45 | |
| 46 | # Use the thread_swap command to move the database connections between |
| 47 | # threads, then verify that they still work. |
| 48 | # |
| 49 | do_test thread2-1.2 { |
| 50 | db close |
| 51 | thread_create A test.db |
| 52 | thread_create B test.db |
| 53 | thread_swap A B |
| 54 | thread_compile A {SELECT a FROM t1 LIMIT 1} |
| 55 | thread_result A |
| 56 | } {SQLITE_OK} |
| 57 | do_test thread2-1.3 { |
| 58 | thread_step A |
| 59 | thread_result A |
| 60 | } {SQLITE_ROW} |
| 61 | do_test thread2-1.4 { |
| 62 | thread_argv A 0 |
| 63 | } {1} |
| 64 | do_test thread2-1.5 { |
| 65 | thread_finalize A |
| 66 | thread_result A |
| 67 | } {SQLITE_OK} |
| 68 | do_test thread2-1.6 { |
| 69 | thread_compile B {SELECT a FROM t1 LIMIT 1} |
| 70 | thread_result B |
| 71 | } {SQLITE_OK} |
| 72 | do_test thread2-1.7 { |
| 73 | thread_step B |
| 74 | thread_result B |
| 75 | } {SQLITE_ROW} |
| 76 | do_test thread2-1.8 { |
| 77 | thread_argv B 0 |
| 78 | } {1} |
| 79 | do_test thread2-1.9 { |
| 80 | thread_finalize B |
| 81 | thread_result B |
| 82 | } {SQLITE_OK} |
| 83 | |
| 84 | # Swap them again. |
| 85 | # |
| 86 | do_test thread2-2.2 { |
| 87 | thread_swap A B |
| 88 | thread_compile A {SELECT a FROM t1 LIMIT 1} |
| 89 | thread_result A |
| 90 | } {SQLITE_OK} |
| 91 | do_test thread2-2.3 { |
| 92 | thread_step A |
| 93 | thread_result A |
| 94 | } {SQLITE_ROW} |
| 95 | do_test thread2-2.4 { |
| 96 | thread_argv A 0 |
| 97 | } {1} |
| 98 | do_test thread2-2.5 { |
| 99 | thread_finalize A |
| 100 | thread_result A |
| 101 | } {SQLITE_OK} |
| 102 | do_test thread2-2.6 { |
| 103 | thread_compile B {SELECT a FROM t1 LIMIT 1} |
| 104 | thread_result B |
| 105 | } {SQLITE_OK} |
| 106 | do_test thread2-2.7 { |
| 107 | thread_step B |
| 108 | thread_result B |
| 109 | } {SQLITE_ROW} |
| 110 | do_test thread2-2.8 { |
| 111 | thread_argv B 0 |
| 112 | } {1} |
| 113 | do_test thread2-2.9 { |
| 114 | thread_finalize B |
| 115 | thread_result B |
| 116 | } {SQLITE_OK} |
| 117 | thread_halt A |
| 118 | thread_halt B |
| 119 | |
drh | 64b1bea | 2006-01-15 02:30:57 +0000 | [diff] [blame] | 120 | # Also important to halt the worker threads, which are using spin |
| 121 | # locks and eating away CPU cycles. |
| 122 | # |
| 123 | thread_halt * |
| 124 | finish_test |