rtree3.test 6.8 KB

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