xref: /sqlite-3.40.0/test/capi3b.test (revision c1a60c51)
192f02c31Sdrh# 2004 September 2
292f02c31Sdrh#
392f02c31Sdrh# The author disclaims copyright to this source code.  In place of
492f02c31Sdrh# a legal notice, here is a blessing:
592f02c31Sdrh#
692f02c31Sdrh#    May you do good and not evil.
792f02c31Sdrh#    May you find forgiveness for yourself and forgive others.
892f02c31Sdrh#    May you share freely, never taking more than you give.
992f02c31Sdrh#
1092f02c31Sdrh#***********************************************************************
1192f02c31Sdrh# This file implements regression tests for SQLite library.  The
1292f02c31Sdrh# focus of this script testing the callback-free C/C++ API and in
1392f02c31Sdrh# particular the behavior of sqlite3_step() when trying to commit
1492f02c31Sdrh# with lock contention.
1592f02c31Sdrh#
16b771228eSdrh# $Id: capi3b.test,v 1.4 2007/08/10 19:46:14 drh Exp $
1792f02c31Sdrh#
1892f02c31Sdrh
1992f02c31Sdrhset testdir [file dirname $argv0]
2092f02c31Sdrhsource $testdir/tester.tcl
2192f02c31Sdrh
2292f02c31Sdrh
23b771228eSdrh# These tests depend on the pager holding changes in cache
24b771228eSdrh# until it is time to commit.  But that won't happen if the
25b771228eSdrh# soft-heap-limit is set too low.  So disable the soft heap limit
26b771228eSdrh# for the duration of this test.
27b771228eSdrh#
28b771228eSdrhsqlite3_soft_heap_limit 0
29b771228eSdrh
30b771228eSdrh
31dddca286Sdrhset DB [sqlite3_connection_pointer db]
32dddca286Sdrhsqlite3 db2 test.db
33dddca286Sdrhset DB2 [sqlite3_connection_pointer db2]
3492f02c31Sdrh
3592f02c31Sdrh# Create some data in the database
3692f02c31Sdrh#
3792f02c31Sdrhdo_test capi3b-1.1 {
3892f02c31Sdrh  execsql {
3992f02c31Sdrh    CREATE TABLE t1(x);
4092f02c31Sdrh    INSERT INTO t1 VALUES(1);
4192f02c31Sdrh    INSERT INTO t1 VALUES(2);
4292f02c31Sdrh    SELECT * FROM t1
4392f02c31Sdrh  }
4492f02c31Sdrh} {1 2}
4592f02c31Sdrh
4692f02c31Sdrh# Make sure the second database connection can see the data
4792f02c31Sdrh#
4892f02c31Sdrhdo_test capi3b-1.2 {
4992f02c31Sdrh  execsql {
5092f02c31Sdrh    SELECT * FROM t1
5192f02c31Sdrh  } db2
5292f02c31Sdrh} {1 2}
5392f02c31Sdrh
5492f02c31Sdrh# First database connection acquires a shared lock
5592f02c31Sdrh#
5692f02c31Sdrhdo_test capi3b-1.3 {
5792f02c31Sdrh  execsql {
5892f02c31Sdrh    BEGIN;
5992f02c31Sdrh    SELECT * FROM t1;
6092f02c31Sdrh  }
6192f02c31Sdrh} {1 2}
6292f02c31Sdrh
6392f02c31Sdrh# Second database connection tries to write.  The sqlite3_step()
6492f02c31Sdrh# function returns SQLITE_BUSY because it cannot commit.
6592f02c31Sdrh#
6692f02c31Sdrhdo_test capi3b-1.4 {
6792f02c31Sdrh  set VM [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(3)} -1 TAIL]
6892f02c31Sdrh  sqlite3_step $VM
6992f02c31Sdrh} SQLITE_BUSY
7092f02c31Sdrh
7192f02c31Sdrh# The sqlite3_step call can be repeated multiple times.
7292f02c31Sdrh#
7392f02c31Sdrhdo_test capi3b-1.5.1 {
7492f02c31Sdrh  sqlite3_step $VM
7592f02c31Sdrh} SQLITE_BUSY
7692f02c31Sdrhdo_test capi3b-1.5.2 {
7792f02c31Sdrh  sqlite3_step $VM
7892f02c31Sdrh} SQLITE_BUSY
7992f02c31Sdrh
8092f02c31Sdrh# The first connection closes its transaction.  This allows the second
8192f02c31Sdrh# connections sqlite3_step to succeed.
8292f02c31Sdrh#
8392f02c31Sdrhdo_test capi3b-1.6 {
8492f02c31Sdrh  execsql COMMIT
8592f02c31Sdrh  sqlite3_step $VM
8692f02c31Sdrh} SQLITE_DONE
8792f02c31Sdrhdo_test capi3b-1.7 {
8892f02c31Sdrh  sqlite3_finalize $VM
8992f02c31Sdrh} SQLITE_OK
9092f02c31Sdrhdo_test capi3b-1.8 {
9192f02c31Sdrh  execsql {SELECT * FROM t1} db2
9292f02c31Sdrh} {1 2 3}
9392f02c31Sdrhdo_test capi3b-1.9 {
9492f02c31Sdrh  execsql {SELECT * FROM t1}
9592f02c31Sdrh} {1 2 3}
9692f02c31Sdrh
972c7e5679Sdrh# Start doing a SELECT with one connection.  This gets a SHARED lock.
982c7e5679Sdrh# Then do an INSERT with the other connection.  The INSERT should
992c7e5679Sdrh# not be able to complete until the SELECT finishes.
1002c7e5679Sdrh#
1012c7e5679Sdrhdo_test capi3b-2.1 {
1022c7e5679Sdrh  set VM1 [sqlite3_prepare $DB {SELECT * FROM t1} -1 TAIL]
1032c7e5679Sdrh  sqlite3_step $VM1
1042c7e5679Sdrh} SQLITE_ROW
1052c7e5679Sdrhdo_test capi3b-2.2 {
1062c7e5679Sdrh  sqlite3_column_text $VM1 0
1072c7e5679Sdrh} 1
1082c7e5679Sdrhdo_test capi3b-2.3 {
1092c7e5679Sdrh  set VM2 [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(4)} -1 TAIL]
1102c7e5679Sdrh  sqlite3_step $VM2
1112c7e5679Sdrh} SQLITE_BUSY
1122c7e5679Sdrhdo_test capi3b-2.4 {
1132c7e5679Sdrh  sqlite3_step $VM1
1142c7e5679Sdrh} SQLITE_ROW
1152c7e5679Sdrhdo_test capi3b-2.5 {
1162c7e5679Sdrh  sqlite3_column_text $VM1 0
1172c7e5679Sdrh} 2
1182c7e5679Sdrhdo_test capi3b-2.6 {
1192c7e5679Sdrh  sqlite3_step $VM2
1202c7e5679Sdrh} SQLITE_BUSY
1212c7e5679Sdrhdo_test capi3b-2.7 {
1222c7e5679Sdrh  sqlite3_step $VM1
1232c7e5679Sdrh} SQLITE_ROW
1242c7e5679Sdrhdo_test capi3b-2.8 {
1252c7e5679Sdrh  sqlite3_column_text $VM1 0
1262c7e5679Sdrh} 3
1272c7e5679Sdrhdo_test capi3b-2.9 {
1282c7e5679Sdrh  sqlite3_step $VM2
1292c7e5679Sdrh} SQLITE_BUSY
1302c7e5679Sdrhdo_test capi3b-2.10 {
1312c7e5679Sdrh  sqlite3_step $VM1
1322c7e5679Sdrh} SQLITE_DONE
1332c7e5679Sdrhdo_test capi3b-2.11 {
1342c7e5679Sdrh  sqlite3_step $VM2
1352c7e5679Sdrh} SQLITE_DONE
1362c7e5679Sdrhdo_test capi3b-2.12 {
1372c7e5679Sdrh  sqlite3_finalize $VM1
1382c7e5679Sdrh  sqlite3_finalize $VM2
1392c7e5679Sdrh  execsql {SELECT * FROM t1}
1402c7e5679Sdrh} {1 2 3 4}
1412c7e5679Sdrh
14292f02c31Sdrhcatch {db2 close}
143b771228eSdrh
144*c1a60c51Sdansqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
14592f02c31Sdrhfinish_test
146