pager.test 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. # 2001 September 15
  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 script is page cache subsystem.
  13. #
  14. # $Id: pager.test,v 1.11 2002/02/02 18:49:21 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. if {[info commands pager_open]!=""} {
  18. # Basic sanity check. Open and close a pager.
  19. #
  20. do_test pager-1.0 {
  21. catch {file delete -force ptf1.db}
  22. catch {file delete -force ptf1.db-journal}
  23. set v [catch {
  24. set ::p1 [pager_open ptf1.db 10]
  25. } msg]
  26. } {0}
  27. do_test pager-1.1 {
  28. pager_stats $::p1
  29. } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
  30. do_test pager-1.2 {
  31. pager_pagecount $::p1
  32. } {0}
  33. do_test pager-1.3 {
  34. pager_stats $::p1
  35. } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
  36. do_test pager-1.4 {
  37. pager_close $::p1
  38. } {}
  39. # Try to write a few pages.
  40. #
  41. do_test pager-2.1 {
  42. set v [catch {
  43. set ::p1 [pager_open ptf1.db 10]
  44. } msg]
  45. } {0}
  46. do_test pager-2.2 {
  47. set v [catch {
  48. set ::g1 [page_get $::p1 0]
  49. } msg]
  50. lappend v $msg
  51. } {1 SQLITE_ERROR}
  52. do_test pager-2.3.1 {
  53. set ::gx [page_lookup $::p1 1]
  54. } {}
  55. do_test pager-2.3.2 {
  56. pager_stats $::p1
  57. } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 0 ovfl 0}
  58. do_test pager-2.3.3 {
  59. set v [catch {
  60. set ::g1 [page_get $::p1 1]
  61. } msg]
  62. if {$v} {lappend v $msg}
  63. set v
  64. } {0}
  65. do_test pager-2.3.3 {
  66. pager_stats $::p1
  67. } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
  68. do_test pager-2.3.4 {
  69. set ::gx [page_lookup $::p1 1]
  70. expr {$::gx!=""}
  71. } {1}
  72. do_test pager-2.3.5 {
  73. pager_stats $::p1
  74. } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
  75. do_test pager-2.3.6 {
  76. expr $::g1==$::gx
  77. } {1}
  78. do_test pager-2.3.7 {
  79. page_unref $::gx
  80. pager_stats $::p1
  81. } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
  82. do_test pager-2.4 {
  83. pager_stats $::p1
  84. } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
  85. do_test pager-2.5 {
  86. pager_pagecount $::p1
  87. } {0}
  88. do_test pager-2.6 {
  89. pager_stats $::p1
  90. } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 1 ovfl 0}
  91. do_test pager-2.7 {
  92. page_number $::g1
  93. } {1}
  94. do_test pager-2.8 {
  95. page_read $::g1
  96. } {}
  97. do_test pager-2.9 {
  98. page_unref $::g1
  99. } {}
  100. do_test pager-2.10 {
  101. pager_stats $::p1
  102. } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 1 ovfl 0}
  103. do_test pager-2.11 {
  104. set ::g1 [page_get $::p1 1]
  105. expr {$::g1!=0}
  106. } {1}
  107. do_test pager-2.12 {
  108. page_number $::g1
  109. } {1}
  110. do_test pager-2.13 {
  111. pager_stats $::p1
  112. } {ref 1 page 1 max 10 size 0 state 1 err 0 hit 0 miss 2 ovfl 0}
  113. do_test pager-2.14 {
  114. set v [catch {
  115. page_write $::g1 "Page-One"
  116. } msg]
  117. lappend v $msg
  118. } {0 {}}
  119. do_test pager-2.15 {
  120. pager_stats $::p1
  121. } {ref 1 page 1 max 10 size 1 state 2 err 0 hit 0 miss 2 ovfl 0}
  122. do_test pager-2.16 {
  123. page_read $::g1
  124. } {Page-One}
  125. do_test pager-2.17 {
  126. set v [catch {
  127. pager_commit $::p1
  128. } msg]
  129. lappend v $msg
  130. } {0 {}}
  131. do_test pager-2.20 {
  132. pager_stats $::p1
  133. } {ref 1 page 1 max 10 size -1 state 1 err 0 hit 0 miss 2 ovfl 0}
  134. do_test pager-2.19 {
  135. pager_pagecount $::p1
  136. } {1}
  137. do_test pager-2.21 {
  138. pager_stats $::p1
  139. } {ref 1 page 1 max 10 size 1 state 1 err 0 hit 0 miss 2 ovfl 0}
  140. do_test pager-2.22 {
  141. page_unref $::g1
  142. } {}
  143. do_test pager-2.23 {
  144. pager_stats $::p1
  145. } {ref 0 page 0 max 10 size -1 state 0 err 0 hit 0 miss 2 ovfl 0}
  146. do_test pager-2.24 {
  147. set v [catch {
  148. page_get $::p1 1
  149. } ::g1]
  150. if {$v} {lappend v $::g1}
  151. set v
  152. } {0}
  153. do_test pager-2.25 {
  154. page_read $::g1
  155. } {Page-One}
  156. do_test pager-2.26 {
  157. set v [catch {
  158. page_write $::g1 {page-one}
  159. } msg]
  160. lappend v $msg
  161. } {0 {}}
  162. do_test pager-2.27 {
  163. page_read $::g1
  164. } {page-one}
  165. do_test pager-2.28 {
  166. set v [catch {
  167. pager_rollback $::p1
  168. } msg]
  169. lappend v $msg
  170. } {0 {}}
  171. do_test pager-2.29 {
  172. page_read $::g1
  173. } {Page-One}
  174. do_test pager-2.99 {
  175. pager_close $::p1
  176. } {}
  177. do_test pager-3.1 {
  178. set v [catch {
  179. set ::p1 [pager_open ptf1.db 15]
  180. } msg]
  181. if {$v} {lappend v $msg}
  182. set v
  183. } {0}
  184. do_test pager-3.2 {
  185. pager_pagecount $::p1
  186. } {1}
  187. do_test pager-3.3 {
  188. set v [catch {
  189. set ::g(1) [page_get $::p1 1]
  190. } msg]
  191. if {$v} {lappend v $msg}
  192. set v
  193. } {0}
  194. do_test pager-3.4 {
  195. page_read $::g(1)
  196. } {Page-One}
  197. do_test pager-3.5 {
  198. for {set i 2} {$i<=20} {incr i} {
  199. set gx [page_get $::p1 $i]
  200. page_write $gx "Page-$i"
  201. page_unref $gx
  202. }
  203. pager_commit $::p1
  204. } {}
  205. for {set i 2} {$i<=20} {incr i} {
  206. do_test pager-3.6.[expr {$i-1}] [subst {
  207. set gx \[page_get $::p1 $i\]
  208. set v \[page_read \$gx\]
  209. page_unref \$gx
  210. set v
  211. }] "Page-$i"
  212. }
  213. for {set i 1} {$i<=20} {incr i} {
  214. regsub -all CNT {
  215. set ::g1 [page_get $::p1 CNT]
  216. set ::g2 [page_get $::p1 CNT]
  217. set ::vx [page_read $::g2]
  218. expr {$::g1==$::g2}
  219. } $i body;
  220. do_test pager-3.7.$i.1 $body {1}
  221. regsub -all CNT {
  222. page_unref $::g2
  223. set vy [page_read $::g1]
  224. expr {$vy==$::vx}
  225. } $i body;
  226. do_test pager-3.7.$i.2 $body {1}
  227. regsub -all CNT {
  228. page_unref $::g1
  229. set gx [page_get $::p1 CNT]
  230. set vy [page_read $gx]
  231. page_unref $gx
  232. expr {$vy==$::vx}
  233. } $i body;
  234. do_test pager-3.7.$i.3 $body {1}
  235. }
  236. do_test pager-3.99 {
  237. pager_close $::p1
  238. } {}
  239. # tests of the checkpoint mechanism and api
  240. #
  241. do_test pager-4.0 {
  242. set v [catch {
  243. file delete -force ptf1.db
  244. set ::p1 [pager_open ptf1.db 15]
  245. } msg]
  246. if {$v} {lappend v $msg}
  247. set v
  248. } {0}
  249. do_test pager-4.1 {
  250. set g1 [page_get $::p1 1]
  251. page_write $g1 "Page-1 v0"
  252. for {set i 2} {$i<=20} {incr i} {
  253. set gx [page_get $::p1 $i]
  254. page_write $gx "Page-$i v0"
  255. page_unref $gx
  256. }
  257. pager_commit $::p1
  258. } {}
  259. for {set i 1} {$i<=20} {incr i} {
  260. do_test pager-4.2.$i {
  261. set gx [page_get $p1 $i]
  262. set v [page_read $gx]
  263. page_unref $gx
  264. set v
  265. } "Page-$i v0"
  266. }
  267. do_test pager-4.3 {
  268. lrange [pager_stats $::p1] 0 1
  269. } {ref 1}
  270. do_test pager-4.4 {
  271. lrange [pager_stats $::p1] 8 9
  272. } {state 1}
  273. for {set i 1} {$i<20} {incr i} {
  274. do_test pager-4.5.$i.0 {
  275. set res {}
  276. for {set j 2} {$j<=20} {incr j} {
  277. set gx [page_get $p1 $j]
  278. set value [page_read $gx]
  279. page_unref $gx
  280. set shouldbe "Page-$j v[expr {$i-1}]"
  281. if {$value!=$shouldbe} {
  282. lappend res $value $shouldbe
  283. }
  284. }
  285. set res
  286. } {}
  287. do_test pager-4.5.$i.1 {
  288. page_write $g1 "Page-1 v$i"
  289. lrange [pager_stats $p1] 8 9
  290. } {state 2}
  291. do_test pager-4.5.$i.2 {
  292. for {set j 2} {$j<=20} {incr j} {
  293. set gx [page_get $p1 $j]
  294. page_write $gx "Page-$j v$i"
  295. page_unref $gx
  296. if {$j==$i} {
  297. pager_ckpt_begin $p1
  298. }
  299. }
  300. } {}
  301. do_test pager-4.5.$i.3 {
  302. set res {}
  303. for {set j 2} {$j<=20} {incr j} {
  304. set gx [page_get $p1 $j]
  305. set value [page_read $gx]
  306. page_unref $gx
  307. set shouldbe "Page-$j v$i"
  308. if {$value!=$shouldbe} {
  309. lappend res $value $shouldbe
  310. }
  311. }
  312. set res
  313. } {}
  314. do_test pager-4.5.$i.4 {
  315. pager_rollback $p1
  316. set res {}
  317. for {set j 2} {$j<=20} {incr j} {
  318. set gx [page_get $p1 $j]
  319. set value [page_read $gx]
  320. page_unref $gx
  321. set shouldbe "Page-$j v[expr {$i-1}]"
  322. if {$value!=$shouldbe} {
  323. lappend res $value $shouldbe
  324. }
  325. }
  326. set res
  327. } {}
  328. do_test pager-4.5.$i.5 {
  329. page_write $g1 "Page-1 v$i"
  330. lrange [pager_stats $p1] 8 9
  331. } {state 2}
  332. do_test pager-4.5.$i.6 {
  333. for {set j 2} {$j<=20} {incr j} {
  334. set gx [page_get $p1 $j]
  335. page_write $gx "Page-$j v$i"
  336. page_unref $gx
  337. if {$j==$i} {
  338. pager_ckpt_begin $p1
  339. }
  340. }
  341. } {}
  342. do_test pager-4.5.$i.7 {
  343. pager_ckpt_rollback $p1
  344. for {set j 2} {$j<=20} {incr j} {
  345. set gx [page_get $p1 $j]
  346. set value [page_read $gx]
  347. page_unref $gx
  348. if {$j<=$i || $i==1} {
  349. set shouldbe "Page-$j v$i"
  350. } else {
  351. set shouldbe "Page-$j v[expr {$i-1}]"
  352. }
  353. if {$value!=$shouldbe} {
  354. lappend res $value $shouldbe
  355. }
  356. }
  357. set res
  358. } {}
  359. do_test pager-4.5.$i.8 {
  360. for {set j 2} {$j<=20} {incr j} {
  361. set gx [page_get $p1 $j]
  362. page_write $gx "Page-$j v$i"
  363. page_unref $gx
  364. if {$j==$i} {
  365. pager_ckpt_begin $p1
  366. }
  367. }
  368. } {}
  369. do_test pager-4.5.$i.9 {
  370. pager_ckpt_commit $p1
  371. for {set j 2} {$j<=20} {incr j} {
  372. set gx [page_get $p1 $j]
  373. set value [page_read $gx]
  374. page_unref $gx
  375. set shouldbe "Page-$j v$i"
  376. if {$value!=$shouldbe} {
  377. lappend res $value $shouldbe
  378. }
  379. }
  380. set res
  381. } {}
  382. do_test pager-4.5.$i.10 {
  383. pager_commit $p1
  384. lrange [pager_stats $p1] 8 9
  385. } {state 1}
  386. }
  387. do_test pager-4.99 {
  388. pager_close $::p1
  389. } {}
  390. file delete -force ptf1.db
  391. } ;# end if( not mem: and has pager_open command );
  392. finish_test