xref: /f-stack/app/redis-5.0.5/tests/unit/other.tcl (revision 572c4311)
1start_server {tags {"other"}} {
2    if {$::force_failure} {
3        # This is used just for test suite development purposes.
4        test {Failing test} {
5            format err
6        } {ok}
7    }
8
9    test {SAVE - make sure there are all the types as values} {
10        # Wait for a background saving in progress to terminate
11        waitForBgsave r
12        r lpush mysavelist hello
13        r lpush mysavelist world
14        r set myemptykey {}
15        r set mynormalkey {blablablba}
16        r zadd mytestzset 10 a
17        r zadd mytestzset 20 b
18        r zadd mytestzset 30 c
19        r save
20    } {OK}
21
22    tags {slow} {
23        if {$::accurate} {set iterations 10000} else {set iterations 1000}
24        foreach fuzztype {binary alpha compr} {
25            test "FUZZ stresser with data model $fuzztype" {
26                set err 0
27                for {set i 0} {$i < $iterations} {incr i} {
28                    set fuzz [randstring 0 512 $fuzztype]
29                    r set foo $fuzz
30                    set got [r get foo]
31                    if {$got ne $fuzz} {
32                        set err [list $fuzz $got]
33                        break
34                    }
35                }
36                set _ $err
37            } {0}
38        }
39    }
40
41    test {BGSAVE} {
42        waitForBgsave r
43        r flushdb
44        r save
45        r set x 10
46        r bgsave
47        waitForBgsave r
48        r debug reload
49        r get x
50    } {10}
51
52    test {SELECT an out of range DB} {
53        catch {r select 1000000} err
54        set _ $err
55    } {*index is out of range*}
56
57    tags {consistency} {
58        if {![catch {package require sha1}]} {
59            if {$::accurate} {set numops 10000} else {set numops 1000}
60            test {Check consistency of different data types after a reload} {
61                r flushdb
62                createComplexDataset r $numops
63                set dump [csvdump r]
64                set sha1 [r debug digest]
65                r debug reload
66                set sha1_after [r debug digest]
67                if {$sha1 eq $sha1_after} {
68                    set _ 1
69                } else {
70                    set newdump [csvdump r]
71                    puts "Consistency test failed!"
72                    puts "You can inspect the two dumps in /tmp/repldump*.txt"
73
74                    set fd [open /tmp/repldump1.txt w]
75                    puts $fd $dump
76                    close $fd
77                    set fd [open /tmp/repldump2.txt w]
78                    puts $fd $newdump
79                    close $fd
80
81                    set _ 0
82                }
83            } {1}
84
85            test {Same dataset digest if saving/reloading as AOF?} {
86                r config set aof-use-rdb-preamble no
87                r bgrewriteaof
88                waitForBgrewriteaof r
89                r debug loadaof
90                set sha1_after [r debug digest]
91                if {$sha1 eq $sha1_after} {
92                    set _ 1
93                } else {
94                    set newdump [csvdump r]
95                    puts "Consistency test failed!"
96                    puts "You can inspect the two dumps in /tmp/aofdump*.txt"
97
98                    set fd [open /tmp/aofdump1.txt w]
99                    puts $fd $dump
100                    close $fd
101                    set fd [open /tmp/aofdump2.txt w]
102                    puts $fd $newdump
103                    close $fd
104
105                    set _ 0
106                }
107            } {1}
108        }
109    }
110
111    test {EXPIRES after a reload (snapshot + append only file rewrite)} {
112        r flushdb
113        r set x 10
114        r expire x 1000
115        r save
116        r debug reload
117        set ttl [r ttl x]
118        set e1 [expr {$ttl > 900 && $ttl <= 1000}]
119        r bgrewriteaof
120        waitForBgrewriteaof r
121        r debug loadaof
122        set ttl [r ttl x]
123        set e2 [expr {$ttl > 900 && $ttl <= 1000}]
124        list $e1 $e2
125    } {1 1}
126
127    test {EXPIRES after AOF reload (without rewrite)} {
128        r flushdb
129        r config set appendonly yes
130        r config set aof-use-rdb-preamble no
131        r set x somevalue
132        r expire x 1000
133        r setex y 2000 somevalue
134        r set z somevalue
135        r expireat z [expr {[clock seconds]+3000}]
136
137        # Milliseconds variants
138        r set px somevalue
139        r pexpire px 1000000
140        r psetex py 2000000 somevalue
141        r set pz somevalue
142        r pexpireat pz [expr {([clock seconds]+3000)*1000}]
143
144        # Reload and check
145        waitForBgrewriteaof r
146        # We need to wait two seconds to avoid false positives here, otherwise
147        # the DEBUG LOADAOF command may read a partial file.
148        # Another solution would be to set the fsync policy to no, since this
149        # prevents write() to be delayed by the completion of fsync().
150        after 2000
151        r debug loadaof
152        set ttl [r ttl x]
153        assert {$ttl > 900 && $ttl <= 1000}
154        set ttl [r ttl y]
155        assert {$ttl > 1900 && $ttl <= 2000}
156        set ttl [r ttl z]
157        assert {$ttl > 2900 && $ttl <= 3000}
158        set ttl [r ttl px]
159        assert {$ttl > 900 && $ttl <= 1000}
160        set ttl [r ttl py]
161        assert {$ttl > 1900 && $ttl <= 2000}
162        set ttl [r ttl pz]
163        assert {$ttl > 2900 && $ttl <= 3000}
164        r config set appendonly no
165    }
166
167    tags {protocol} {
168        test {PIPELINING stresser (also a regression for the old epoll bug)} {
169            set fd2 [socket $::host $::port]
170            fconfigure $fd2 -encoding binary -translation binary
171            puts -nonewline $fd2 "SELECT 9\r\n"
172            flush $fd2
173            gets $fd2
174
175            for {set i 0} {$i < 100000} {incr i} {
176                set q {}
177                set val "0000${i}0000"
178                append q "SET key:$i $val\r\n"
179                puts -nonewline $fd2 $q
180                set q {}
181                append q "GET key:$i\r\n"
182                puts -nonewline $fd2 $q
183            }
184            flush $fd2
185
186            for {set i 0} {$i < 100000} {incr i} {
187                gets $fd2 line
188                gets $fd2 count
189                set count [string range $count 1 end]
190                set val [read $fd2 $count]
191                read $fd2 2
192            }
193            close $fd2
194            set _ 1
195        } {1}
196    }
197
198    test {APPEND basics} {
199        r del foo
200        list [r append foo bar] [r get foo] \
201             [r append foo 100] [r get foo]
202    } {3 bar 6 bar100}
203
204    test {APPEND basics, integer encoded values} {
205        set res {}
206        r del foo
207        r append foo 1
208        r append foo 2
209        lappend res [r get foo]
210        r set foo 1
211        r append foo 2
212        lappend res [r get foo]
213    } {12 12}
214
215    test {APPEND fuzzing} {
216        set err {}
217        foreach type {binary alpha compr} {
218            set buf {}
219            r del x
220            for {set i 0} {$i < 1000} {incr i} {
221                set bin [randstring 0 10 $type]
222                append buf $bin
223                r append x $bin
224            }
225            if {$buf != [r get x]} {
226                set err "Expected '$buf' found '[r get x]'"
227                break
228            }
229        }
230        set _ $err
231    } {}
232
233    # Leave the user with a clean DB before to exit
234    test {FLUSHDB} {
235        set aux {}
236        r select 9
237        r flushdb
238        lappend aux [r dbsize]
239        r select 10
240        r flushdb
241        lappend aux [r dbsize]
242    } {0 0}
243
244    test {Perform a final SAVE to leave a clean DB on disk} {
245        waitForBgsave r
246        r save
247    } {OK}
248}
249