summaryrefslogtreecommitdiff
path: root/test/sql/bdb_mvcc.test
blob: 07fda3c690985324482eebc189621af22587f2f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for transaction snapshots
# and pragmas that set bdb environment resources

set testdir [file dirname $argv0]/../../lang/sql/sqlite/test

source $testdir/../../../../test/sql/bdb_util.tcl

set ::txn [db eval { PRAGMA bdbsql_max_txn }]
set ::locks [db eval { PRAGMA bdbsql_max_locks }]
set ::lockers [db eval { PRAGMA bdbsql_max_lockers }]
set ::lkobjects [db eval { PRAGMA bdbsql_max_lock_objects }]

# 
# Test the return values of pragmas read_write_concurrency
# and snapshot_isolation.
do_test bdb_mvcc-1.0 {
  execsql { PRAGMA multiversion=off }
} {0}

do_test bdb_mvcc-1.1 {
  execsql { PRAGMA snapshot_isolation }
} {0}

do_test bdb_mvcc-1.2 {
  execsql { PRAGMA multiversion }
} {0}

do_test bdb_mvcc-1.3 {
  execsql { PRAGMA multiversion=on }
} {1}

do_test bdb_mvcc-1.4 {
  execsql { PRAGMA snapshot_isolation }
} {1}

do_test bdb_mvcc-1.5 {
  execsql { PRAGMA multiversion }
} {1}

# Remaining tests require threads
if {[run_thread_tests]==0} { finish_test ; return }

do_test bdb_mvcc-3.0 {
  execsql { PRAGMA multiversion=on }
} {1}

do_test bdb_mvcc-3.1 {
  execsql {
    BEGIN;
    CREATE TABLE t1(a);
    CREATE TABLE t2(a);
    COMMIT;
  }
} {}

# Use threads to check that read and writes are concurrent
set mvcc_exclusive_thread {
  set key ""
  if {[sqlite -has-codec]} {
    set key "xyzzy"
  }
  set ::DB [sqlthread open test.db $key]
  set rc [
    do_test e1 {
      execsql { BEGIN EXCLUSIVE }
    } {SQLITE_OK}
    do_test e2 {
      execsql { INSERT INTO t2 VALUES(1) }
    } {SQLITE_OK}
    do_test e3 {
      execsql { SELECT * FROM t1 } 
    } {SQLITE_OK}
    do_test e4 {
      execsql { COMMIT } 
    } {SQLITE_OK}
  ]
  sqlite3_close $DB
  set rc
}

#
# If reads and writes are concurrent then the following
# operations will not deadlock
do_test bdb_mvcc-3.2 {
  db eval {
    BEGIN;
    INSERT INTO t1 values(1);
  }
} {}

array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $mvcc_exclusive_thread 

after 10000

do_test bdb_mvcc-3.3 {
  db eval {
    SELECT * from t2;
  }
} {}

do_test bdb_mvcc-3.4 {
  execsql {
    COMMIT;
  }
} {}

do_test bdb_mvcc-3.5 {
    vwait finished(0)
    set ::finished(0)
} {}

#
# Turn off concurrency so that the following operations
# deadlock  
do_test bdb_mvcc-4.0 {
  execsql { PRAGMA snapshot_isolation=off }
} {0}

# Executes operations under an exclusive txn
set mvcc_exclusive_thread2 {
  set key ""
  if {[sqlite -has-codec]} {
    set key "xyzzy"
  }
  set ::DB [sqlthread open test.db $key]
  set rc [
    do_test e21 {
      execsql { BEGIN EXCLUSIVE }
    } {SQLITE_OK} 
    do_test e22 {
      execsql { INSERT INTO t2 VALUES(1) }
    } {SQLITE_OK}
    do_test e23 {
      execsql { SELECT * FROM t1 } 
    } {SQLITE_OK}
    do_test e24 {
      execsql { COMMIT } 
    } {SQLITE_OK}
  ]
  sqlite3_close $DB
  set rc
}

do_test bdb_mvcc-4.1 {
  db eval {
    BEGIN;
    INSERT INTO t1 values(2);
  }
} {}

array unset finished
thread_spawn finished(0) "" $bdb_thread_procs $mvcc_exclusive_thread2

after 10000

do_test bdb_mvcc-4.2 {
  catchsql { SELECT * FROM t2 }
} {1 {database is locked}}

do_test bdb_mvcc-4.3 {
  execsql {
    ROLLBACK
  }
} {}

do_test bdb_mvcc-4.4 {
    vwait finished(0)
    set ::finished(0)
} {}

catch {close db}

finish_test