walhook.test 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # 2010 April 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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the operation of the library in
  13. # "PRAGMA journal_mode=WAL" mode.
  14. #
  15. # More specifically, this file contains regression tests for the
  16. # sqlite3_wal_hook() mechanism, including the sqlite3_wal_autocheckpoint()
  17. # and "PRAGMA wal_autocheckpoint" convenience interfaces.
  18. #
  19. set testdir [file dirname $argv0]
  20. source $testdir/tester.tcl
  21. source $testdir/wal_common.tcl
  22. ifcapable !wal {finish_test ; return }
  23. set ::wal_hook [list]
  24. proc wal_hook {zDb nEntry} {
  25. lappend ::wal_hook $zDb $nEntry
  26. return 0
  27. }
  28. db wal_hook wal_hook
  29. do_test walhook-1.1 {
  30. execsql {
  31. PRAGMA page_size = 1024;
  32. PRAGMA auto_vacuum = 0;
  33. PRAGMA journal_mode = wal;
  34. PRAGMA synchronous = normal;
  35. CREATE TABLE t1(i PRIMARY KEY, j);
  36. }
  37. set ::wal_hook
  38. } {main 3}
  39. do_test walhook-1.2 {
  40. set ::wal_hook [list]
  41. execsql { INSERT INTO t1 VALUES(1, 'one') }
  42. set ::wal_hook
  43. } {main 5}
  44. do_test walhook-1.3 {
  45. proc wal_hook {args} { db eval {PRAGMA wal_checkpoint}; return 0 }
  46. execsql { INSERT INTO t1 VALUES(2, 'two') }
  47. file size test.db
  48. } [expr 3*1024]
  49. do_test walhook-1.4 {
  50. proc wal_hook {zDb nEntry} {
  51. execsql { PRAGMA wal_checkpoint }
  52. return 0
  53. }
  54. execsql { CREATE TABLE t2(a, b) }
  55. file size test.db
  56. } [expr 4*1024]
  57. do_test walhook-1.5 {
  58. sqlite3 db2 test.db
  59. proc wal_hook {zDb nEntry} {
  60. execsql { PRAGMA wal_checkpoint } db2
  61. return 0
  62. }
  63. execsql { CREATE TABLE t3(a PRIMARY KEY, b) }
  64. file size test.db
  65. } [expr 6*1024]
  66. db2 close
  67. db close
  68. sqlite3 db test.db
  69. do_test walhook-2.1 {
  70. execsql { PRAGMA synchronous = NORMAL }
  71. execsql { PRAGMA wal_autocheckpoint }
  72. } {1000}
  73. do_test walhook-2.2 {
  74. execsql { PRAGMA wal_autocheckpoint = 10}
  75. } {10}
  76. do_test walhook-2.3 {
  77. execsql { PRAGMA wal_autocheckpoint }
  78. } {10}
  79. #
  80. # The database connection is configured with "PRAGMA wal_autocheckpoint = 10".
  81. # Check that transactions are written to the log file until it contains at
  82. # least 10 frames, then the database is checkpointed. Subsequent transactions
  83. # are written into the start of the log file.
  84. #
  85. foreach {tn sql dbpages logpages} {
  86. 4 "CREATE TABLE t4(x PRIMARY KEY, y)" 6 3
  87. 5 "INSERT INTO t4 VALUES(1, 'one')" 6 5
  88. 6 "INSERT INTO t4 VALUES(2, 'two')" 6 7
  89. 7 "INSERT INTO t4 VALUES(3, 'three')" 6 9
  90. 8 "INSERT INTO t4 VALUES(4, 'four')" 8 11
  91. 9 "INSERT INTO t4 VALUES(5, 'five')" 8 11
  92. } {
  93. do_test walhook-2.$tn {
  94. execsql $sql
  95. list [file size test.db] [file size test.db-wal]
  96. } [list [expr $dbpages*1024] [wal_file_size $logpages 1024]]
  97. }
  98. catch { db2 close }
  99. catch { db close }
  100. finish_test