xref: /sqlite-3.40.0/ext/rbu/rbu6.test (revision 6ab91a7a)
1# 2014 October 21
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#
12# This file contains tests for the RBU module. Specifically, it tests the
13# outcome of some other client writing to the database while an RBU update
14# is being applied.
15
16if {![info exists testdir]} {
17  set testdir [file join [file dirname [info script]] .. .. test]
18}
19source $testdir/tester.tcl
20set ::testprefix rbu6
21
22proc setup_test {} {
23  reset_db
24  execsql {
25    CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
26    CREATE TABLE t2(a INTEGER PRIMARY KEY, b UNIQUE);
27    CREATE TABLE t3(a INTEGER PRIMARY KEY, b UNIQUE);
28  }
29  db close
30
31  forcedelete rbu.db
32  sqlite3 rbu rbu.db
33  rbu eval {
34    CREATE TABLE data_t1(a, b, rbu_control);
35    CREATE TABLE data_t2(a, b, rbu_control);
36    CREATE TABLE data_t3(a, b, rbu_control);
37    INSERT INTO data_t1 VALUES(1, 't1', 0);
38    INSERT INTO data_t2 VALUES(2, 't2', 0);
39    INSERT INTO data_t3 VALUES(3, 't3', 0);
40  }
41  rbu close
42}
43
44# Test the outcome of some other client writing the db while the *-oal
45# file is being generated. Once this has happened, the update cannot be
46# progressed.
47#
48for {set nStep 1} {$nStep < 8} {incr nStep} {
49  do_test 1.$nStep.1 {
50    setup_test
51    sqlite3rbu rbu test.db rbu.db
52    for {set i 0} {$i<$nStep} {incr i} {rbu step}
53
54    rbu close
55    sqlite3 db test.db
56    execsql { INSERT INTO t1 VALUES(5, 'hello') }
57    sqlite3rbu rbu test.db rbu.db
58    rbu step
59  } {SQLITE_BUSY}
60  do_test 1.$nStep.2 {
61    rbu step
62  } {SQLITE_BUSY}
63  do_test 1.$nStep.3 {
64    list [file exists test.db-oal] [file exists test.db-wal]
65  } {1 0}
66  do_test 1.$nStep.4 {
67    list [catch { rbu close } msg] $msg
68  } {1 {SQLITE_BUSY - database modified during rbu update}}
69}
70
71# Test the outcome of some other client writing the db after the *-oal
72# file has been copied to the *-wal path. Once this has happened, any
73# other client writing to the db causes RBU to consider its job finished.
74#
75for {set nStep 8} {$nStep < 20} {incr nStep} {
76  do_test 1.$nStep.1 {
77    setup_test
78    sqlite3rbu rbu test.db rbu.db
79    for {set i 0} {$i<$nStep} {incr i} {rbu step}
80    rbu close
81    sqlite3 db test.db
82    execsql { INSERT INTO t1 VALUES(5, 'hello') }
83    sqlite3rbu rbu test.db rbu.db
84    rbu step
85  } {SQLITE_DONE}
86  do_test 1.$nStep.2 {
87    rbu step
88  } {SQLITE_DONE}
89  do_test 1.$nStep.3 {
90    file exists test.db-oal
91  } {0}
92  do_test 1.$nStep.4 {
93    list [catch { rbu close } msg] $msg
94  } {0 SQLITE_DONE}
95
96  do_execsql_test 1.$nStep.5 {
97    SELECT * FROM t1;
98  } {1 t1 5 hello}
99}
100
101
102finish_test
103