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