xref: /sqlite-3.40.0/test/capi3b.test (revision 4dcbdbff)
1# 2004 September 2
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 testing the callback-free C/C++ API and in
13# particular the behavior of sqlite3_step() when trying to commit
14# with lock contention.
15#
16# $Id: capi3b.test,v 1.2 2004/09/03 00:27:57 drh Exp $
17#
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22
23db close
24set DB [sqlite3 db test.db]
25set DB2 [sqlite3 db2 test.db]
26
27# Create some data in the database
28#
29do_test capi3b-1.1 {
30  execsql {
31    CREATE TABLE t1(x);
32    INSERT INTO t1 VALUES(1);
33    INSERT INTO t1 VALUES(2);
34    SELECT * FROM t1
35  }
36} {1 2}
37
38# Make sure the second database connection can see the data
39#
40do_test capi3b-1.2 {
41  execsql {
42    SELECT * FROM t1
43  } db2
44} {1 2}
45
46# First database connection acquires a shared lock
47#
48do_test capi3b-1.3 {
49  execsql {
50    BEGIN;
51    SELECT * FROM t1;
52  }
53} {1 2}
54
55# Second database connection tries to write.  The sqlite3_step()
56# function returns SQLITE_BUSY because it cannot commit.
57#
58do_test capi3b-1.4 {
59  set VM [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(3)} -1 TAIL]
60  sqlite3_step $VM
61} SQLITE_BUSY
62
63# The sqlite3_step call can be repeated multiple times.
64#
65do_test capi3b-1.5.1 {
66  sqlite3_step $VM
67} SQLITE_BUSY
68do_test capi3b-1.5.2 {
69  sqlite3_step $VM
70} SQLITE_BUSY
71
72# The first connection closes its transaction.  This allows the second
73# connections sqlite3_step to succeed.
74#
75do_test capi3b-1.6 {
76  execsql COMMIT
77  sqlite3_step $VM
78} SQLITE_DONE
79do_test capi3b-1.7 {
80  sqlite3_finalize $VM
81} SQLITE_OK
82do_test capi3b-1.8 {
83  execsql {SELECT * FROM t1} db2
84} {1 2 3}
85do_test capi3b-1.9 {
86  execsql {SELECT * FROM t1}
87} {1 2 3}
88
89# Start doing a SELECT with one connection.  This gets a SHARED lock.
90# Then do an INSERT with the other connection.  The INSERT should
91# not be able to complete until the SELECT finishes.
92#
93do_test capi3b-2.1 {
94  set VM1 [sqlite3_prepare $DB {SELECT * FROM t1} -1 TAIL]
95  sqlite3_step $VM1
96} SQLITE_ROW
97do_test capi3b-2.2 {
98  sqlite3_column_text $VM1 0
99} 1
100do_test capi3b-2.3 {
101  set VM2 [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(4)} -1 TAIL]
102  sqlite3_step $VM2
103} SQLITE_BUSY
104do_test capi3b-2.4 {
105  sqlite3_step $VM1
106} SQLITE_ROW
107do_test capi3b-2.5 {
108  sqlite3_column_text $VM1 0
109} 2
110do_test capi3b-2.6 {
111  sqlite3_step $VM2
112} SQLITE_BUSY
113do_test capi3b-2.7 {
114  sqlite3_step $VM1
115} SQLITE_ROW
116do_test capi3b-2.8 {
117  sqlite3_column_text $VM1 0
118} 3
119do_test capi3b-2.9 {
120  sqlite3_step $VM2
121} SQLITE_BUSY
122do_test capi3b-2.10 {
123  sqlite3_step $VM1
124} SQLITE_DONE
125do_test capi3b-2.11 {
126  sqlite3_step $VM2
127} SQLITE_DONE
128do_test capi3b-2.12 {
129  sqlite3_finalize $VM1
130  sqlite3_finalize $VM2
131  execsql {SELECT * FROM t1}
132} {1 2 3 4}
133
134catch {db2 close}
135finish_test
136