• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 2008 Feb 19
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# The focus of this file is testing that the r-tree correctly handles
13# out-of-memory conditions.
14#
15
16if {![info exists testdir]} {
17  set testdir [file join [file dirname [info script]] .. .. test]
18}
19source $testdir/tester.tcl
20source $testdir/malloc_common.tcl
21ifcapable !rtree {
22  finish_test
23  return
24}
25
26# Test summary:
27#
28#   rtree3-1: Test OOM in simple CREATE TABLE, INSERT, DELETE and SELECT
29#             commands on an almost empty table.
30#
31#   rtree3-2: Test OOM in a DROP TABLE command.
32#
33#   rtree3-3a: Test OOM during a transaction to insert 100 pseudo-random rows.
34#
35#   rtree3-3b: Test OOM during a transaction deleting all entries in the
36#              database constructed in [rtree3-3a] in pseudo-random order.
37#
38#   rtree3-4a: OOM during "SELECT count(*) FROM ..." on a big table.
39#
40#   rtree3-4b: OOM while deleting rows from a big table.
41#
42#   rtree3-5: Test OOM while inserting rows into a big table.
43#
44#   rtree3-6: Test OOM while deleting all rows of a table, one at a time.
45#
46#   rtree3-7: OOM during an ALTER TABLE RENAME TABLE command.
47#
48#   rtree3-8: Test OOM while registering the r-tree module with sqlite.
49#
50
51do_faultsim_test rtree3-1 -faults oom* -prep {
52  faultsim_delete_and_reopen
53} -body {
54  execsql {
55    BEGIN TRANSACTION;
56    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
57    INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
58    INSERT INTO rt VALUES(NULL, 13, 15, 17, 19);
59    DELETE FROM rt WHERE ii = 1;
60    SELECT * FROM rt;
61    SELECT ii FROM rt WHERE ii = 2;
62    COMMIT;
63  }
64}
65
66do_test rtree3-2.prep {
67  faultsim_delete_and_reopen
68  execsql {
69    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
70    INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
71  }
72  faultsim_save_and_close
73} {}
74do_faultsim_test rtree3-2 -faults oom* -prep {
75  faultsim_restore_and_reopen
76} -body {
77  execsql { DROP TABLE rt }
78}
79
80do_malloc_test rtree3-3.prep {
81  faultsim_delete_and_reopen
82  execsql {
83    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
84    INSERT INTO rt VALUES(NULL, 3, 5, 7, 9);
85  }
86  faultsim_save_and_close
87} {}
88
89do_faultsim_test rtree3-3a -faults oom* -prep {
90  faultsim_restore_and_reopen
91} -body {
92  db eval BEGIN
93  for {set ii 0} {$ii < 100} {incr ii} {
94    set f [expr rand()]
95    db eval {INSERT INTO rt VALUES(NULL, $f*10.0, $f*10.0, $f*15.0, $f*15.0)}
96  }
97  db eval COMMIT
98}
99faultsim_save_and_close
100
101do_faultsim_test rtree3-3b -faults oom* -prep {
102  faultsim_restore_and_reopen
103} -body {
104  db eval BEGIN
105  for {set ii 0} {$ii < 100} {incr ii} {
106    set f [expr rand()]
107    db eval { DELETE FROM rt WHERE x1<($f*10.0) AND x1>($f*10.5) }
108  }
109  db eval COMMIT
110}
111
112do_test rtree3-4.prep {
113  faultsim_delete_and_reopen
114  execsql {
115    BEGIN;
116    PRAGMA page_size = 512;
117    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
118  }
119  for {set i 0} {$i < 1500} {incr i} {
120    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
121  }
122  execsql { COMMIT }
123  faultsim_save_and_close
124} {}
125
126do_faultsim_test rtree3-4a -faults oom-* -prep {
127  faultsim_restore_and_reopen
128} -body {
129  db eval { SELECT count(*) FROM rt }
130} -test {
131  faultsim_test_result {0 1500}
132}
133
134do_faultsim_test rtree3-4b -faults oom-transient -prep {
135  faultsim_restore_and_reopen
136} -body {
137  db eval { DELETE FROM rt WHERE ii BETWEEN 1 AND 100 }
138} -test {
139  faultsim_test_result {0 {}}
140}
141
142do_test rtree3-5.prep {
143  faultsim_delete_and_reopen
144  execsql {
145    BEGIN;
146    PRAGMA page_size = 512;
147    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
148  }
149  for {set i 0} {$i < 100} {incr i} {
150    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
151  }
152  execsql { COMMIT }
153  faultsim_save_and_close
154} {}
155do_faultsim_test rtree3-5 -faults oom-* -prep {
156  faultsim_restore_and_reopen
157} -body {
158  for {set i 100} {$i < 110} {incr i} {
159    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
160  }
161} -test {
162  faultsim_test_result {0 {}}
163}
164
165do_test rtree3-6.prep {
166  faultsim_delete_and_reopen
167  execsql {
168    BEGIN;
169    PRAGMA page_size = 512;
170    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2);
171  }
172  for {set i 0} {$i < 50} {incr i} {
173    execsql { INSERT INTO rt VALUES($i, $i, $i+1, $i, $i+1) }
174  }
175  execsql { COMMIT }
176  faultsim_save_and_close
177} {}
178do_faultsim_test rtree3-6 -faults oom-* -prep {
179  faultsim_restore_and_reopen
180} -body {
181  execsql BEGIN
182  for {set i 0} {$i < 50} {incr i} {
183    execsql { DELETE FROM rt WHERE ii=$i }
184  }
185  execsql COMMIT
186} -test {
187  faultsim_test_result {0 {}}
188}
189
190do_test rtree3-7.prep {
191  faultsim_delete_and_reopen
192  execsql { CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2) }
193  faultsim_save_and_close
194} {}
195do_faultsim_test rtree3-7 -faults oom-* -prep {
196  faultsim_restore_and_reopen
197} -body {
198  execsql { ALTER TABLE rt RENAME TO rt2 }
199} -test {
200  faultsim_test_result {0 {}}
201}
202
203do_faultsim_test rtree3-8 -faults oom-* -prep {
204  catch { db close }
205} -body {
206  sqlite3 db test.db
207}
208
209do_faultsim_test rtree3-9 -faults oom-* -prep {
210  sqlite3 db :memory:
211} -body {
212  set rc [register_cube_geom db]
213  if {$rc != "SQLITE_OK"} { error $rc }
214} -test {
215  faultsim_test_result {0 {}} {1 SQLITE_NOMEM}
216}
217
218do_test rtree3-10.prep {
219  faultsim_delete_and_reopen
220  execsql {
221    CREATE VIRTUAL TABLE rt USING rtree(ii, x1, x2, y1, y2, z1, z2);
222    INSERT INTO rt VALUES(1,  10, 10, 10, 11, 11, 11);
223    INSERT INTO rt VALUES(2,  5, 6, 6, 7, 7, 8);
224  }
225  faultsim_save_and_close
226} {}
227do_faultsim_test rtree3-10 -faults oom-* -prep {
228  faultsim_restore_and_reopen
229  register_cube_geom db
230  execsql { SELECT * FROM rt }
231} -body {
232  execsql { SELECT ii FROM rt WHERE ii MATCH cube(4.5, 5.5, 6.5, 1, 1, 1) }
233} -test {
234  faultsim_test_result {0 2}
235}
236
237finish_test
238