ioerr2.test 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # 2007 April 2
  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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing for correct handling of I/O errors
  13. # such as writes failing because the disk is full.
  14. #
  15. # The tests in this file use special facilities that are only
  16. # available in the SQLite test fixture.
  17. #
  18. # $Id: ioerr2.test,v 1.12 2009/06/05 17:09:12 drh Exp $
  19. set testdir [file dirname $argv0]
  20. source $testdir/tester.tcl
  21. ifcapable !integrityck {
  22. finish_test
  23. return
  24. }
  25. do_test ioerr2-1.1 {
  26. execsql {
  27. PRAGMA cache_size = 10;
  28. PRAGMA default_cache_size = 10;
  29. CREATE TABLE t1(a, b, PRIMARY KEY(a, b));
  30. INSERT INTO t1 VALUES(randstr(400,400),randstr(400,400));
  31. INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 2
  32. INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 4
  33. INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 8
  34. INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 16
  35. INSERT INTO t1 SELECT randstr(400,400), randstr(400,400) FROM t1; -- 32
  36. }
  37. } {}
  38. set ::cksum [execsql {SELECT md5sum(a, b) FROM t1}]
  39. proc check_db {testname} {
  40. # Make sure no I/O errors are simulated in this proc.
  41. set ::sqlite_io_error_hit 0
  42. set ::sqlite_io_error_persist 0
  43. set ::sqlite_io_error_pending 0
  44. # Run an integrity-check. If "disk I/O error" is returned, the
  45. # pager must be in error state. In this case open a new database
  46. # connection. Otherwise, try a ROLLBACK, in case a transaction
  47. # is still active.
  48. set rc [catch {execsql {PRAGMA integrity_check}} msg]
  49. if {$rc && ($msg eq "disk I/O error" || $msg eq "database is locked")} {
  50. db close
  51. sqlite3 db test.db
  52. set refcnt 0
  53. } else {
  54. if {$rc || $msg ne "ok"} {
  55. error $msg
  56. }
  57. catch {execsql ROLLBACK}
  58. }
  59. # Check that the database checksum is still $::cksum, and that
  60. # the integrity-check passes.
  61. set ck [execsql {SELECT md5sum(a, b) FROM t1}]
  62. do_test ${testname}.cksum [list set ck $ck] $::cksum
  63. integrity_check ${testname}.integrity
  64. do_test ${testname}.refcnt {
  65. lindex [sqlite3_pager_refcounts db] 0
  66. } 0
  67. }
  68. check_db ioerr2-2
  69. set sql {
  70. PRAGMA cache_size = 10;
  71. PRAGMA default_cache_size = 10;
  72. BEGIN;
  73. DELETE FROM t1 WHERE (oid%7)==0;
  74. INSERT INTO t1 SELECT randstr(400,400), randstr(400,400)
  75. WHERE (random()%7)==0;
  76. UPDATE t1 SET a = randstr(400,400), b = randstr(400,400)
  77. WHERE (random()%7)==0;
  78. ROLLBACK;
  79. }
  80. foreach bPersist [list 0 1] {
  81. set ::go 1
  82. for {set ::N 1} {$::go} {incr ::N} {
  83. db close
  84. sqlite3 db test.db
  85. set ::sqlite_io_error_hit 0
  86. set ::sqlite_io_error_persist $bPersist
  87. set ::sqlite_io_error_pending $::N
  88. foreach {::go res} [catchsql $sql] {}
  89. check_db ioerr2-3.$bPersist.$::N
  90. }
  91. }
  92. foreach bPersist [list 0 1] {
  93. set ::go 1
  94. for {set ::N 1} {$::go} {incr ::N} {
  95. set ::sqlite_io_error_hit 0
  96. set ::sqlite_io_error_persist $bPersist
  97. set ::sqlite_io_error_pending $::N
  98. foreach {::go res} [catchsql $sql] {}
  99. check_db ioerr2-4.[expr {$bPersist+2}].$::N
  100. }
  101. }
  102. do_test ioerr2-5 {
  103. execsql {
  104. CREATE TABLE t2 AS SELECT * FROM t1;
  105. PRAGMA temp_store = memory;
  106. }
  107. set ::sqlite_io_error_persist 0
  108. set ::go 1
  109. set rc [catch {
  110. for {set ::N 2} {$::N<200} {incr ::N} {
  111. db eval {SELECT * FROM t1 WHERE rowid IN (1, 5, 10, 15, 20)} {
  112. set ::sqlite_io_error_hit 0
  113. set ::sqlite_io_error_pending $::N
  114. set sql {UPDATE t2 SET b = randstr(400,400)}
  115. foreach {::go res} [catchsql $sql] {}
  116. }
  117. }
  118. } msg]
  119. list $rc $msg
  120. } {1 {abort due to ROLLBACK}}
  121. if {$::tcl_platform(platform) == "unix"} {
  122. # Cause the call to xAccess used by [pragma temp_store_directory] to
  123. # determine if the specified directory is writable to fail. This causes
  124. # SQLite to report "not a writable directory", which is probably the
  125. # right answer.
  126. #
  127. do_test ioerr2-6 {
  128. set ::sqlite_io_error_hit 0
  129. set ::sqlite_io_error_pending 1
  130. catchsql {PRAGMA temp_store_directory = '/tmp/'}
  131. } {1 {not a writable directory}}
  132. }
  133. do_ioerr_test ioerr2-7 -persist 0 -sqlprep {
  134. PRAGMA cache_size = 10;
  135. PRAGMA auto_vacuum = 1;
  136. CREATE TABLE ab(a, b);
  137. CREATE TABLE de(d, e);
  138. INSERT INTO ab VALUES(1, randstr(200,200));
  139. INSERT INTO ab SELECT a+1, randstr(200,200) FROM ab;
  140. INSERT INTO ab SELECT a+2, randstr(200,200) FROM ab;
  141. INSERT INTO ab SELECT a+4, randstr(200,200) FROM ab;
  142. INSERT INTO ab SELECT a+8, randstr(200,200) FROM ab;
  143. INSERT INTO ab SELECT a+16, randstr(200,200) FROM ab;
  144. INSERT INTO ab SELECT a+32, randstr(200,200) FROM ab;
  145. INSERT INTO ab SELECT a+64, randstr(200,200) FROM ab;
  146. INSERT INTO de SELECT * FROM ab;
  147. } -sqlbody {
  148. BEGIN;
  149. UPDATE ab SET b = randstr(200,200);
  150. UPDATE de SET e = randstr(200,200) WHERE d = (SELECT max(d) FROM de);
  151. DELETE FROM ab;
  152. COMMIT;
  153. }
  154. finish_test