]> git.saurik.com Git - redis.git/blob - test/support/util.tcl
wait for redis-server to be settled and ready for connections
[redis.git] / test / support / util.tcl
1 proc randstring {min max {type binary}} {
2 set len [expr {$min+int(rand()*($max-$min+1))}]
3 set output {}
4 if {$type eq {binary}} {
5 set minval 0
6 set maxval 255
7 } elseif {$type eq {alpha}} {
8 set minval 48
9 set maxval 122
10 } elseif {$type eq {compr}} {
11 set minval 48
12 set maxval 52
13 }
14 while {$len} {
15 append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
16 incr len -1
17 }
18 return $output
19 }
20
21 # Useful for some test
22 proc zlistAlikeSort {a b} {
23 if {[lindex $a 0] > [lindex $b 0]} {return 1}
24 if {[lindex $a 0] < [lindex $b 0]} {return -1}
25 string compare [lindex $a 1] [lindex $b 1]
26 }
27
28 proc waitForBgsave r {
29 while 1 {
30 set i [$r info]
31 if {[string match {*bgsave_in_progress:1*} $i]} {
32 puts -nonewline "\nWaiting for background save to finish... "
33 flush stdout
34 after 1000
35 } else {
36 break
37 }
38 }
39 }
40
41 proc waitForBgrewriteaof r {
42 while 1 {
43 set i [$r info]
44 if {[string match {*bgrewriteaof_in_progress:1*} $i]} {
45 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
46 flush stdout
47 after 1000
48 } else {
49 break
50 }
51 }
52 }
53
54 proc randomInt {max} {
55 expr {int(rand()*$max)}
56 }
57
58 proc randpath args {
59 set path [expr {int(rand()*[llength $args])}]
60 uplevel 1 [lindex $args $path]
61 }
62
63 proc randomValue {} {
64 randpath {
65 # Small enough to likely collide
66 randomInt 1000
67 } {
68 # 32 bit compressible signed/unsigned
69 randpath {randomInt 2000000000} {randomInt 4000000000}
70 } {
71 # 64 bit
72 randpath {randomInt 1000000000000}
73 } {
74 # Random string
75 randpath {randstring 0 256 alpha} \
76 {randstring 0 256 compr} \
77 {randstring 0 256 binary}
78 }
79 }
80
81 proc randomKey {} {
82 randpath {
83 # Small enough to likely collide
84 randomInt 1000
85 } {
86 # 32 bit compressible signed/unsigned
87 randpath {randomInt 2000000000} {randomInt 4000000000}
88 } {
89 # 64 bit
90 randpath {randomInt 1000000000000}
91 } {
92 # Random string
93 randpath {randstring 1 256 alpha} \
94 {randstring 1 256 compr}
95 }
96 }
97
98 proc createComplexDataset {r ops} {
99 for {set j 0} {$j < $ops} {incr j} {
100 set k [randomKey]
101 set f [randomValue]
102 set v [randomValue]
103 randpath {
104 set d [expr {rand()}]
105 } {
106 set d [expr {rand()}]
107 } {
108 set d [expr {rand()}]
109 } {
110 set d [expr {rand()}]
111 } {
112 set d [expr {rand()}]
113 } {
114 randpath {set d +inf} {set d -inf}
115 }
116 set t [$r type $k]
117
118 if {$t eq {none}} {
119 randpath {
120 $r set $k $v
121 } {
122 $r lpush $k $v
123 } {
124 $r sadd $k $v
125 } {
126 $r zadd $k $d $v
127 } {
128 $r hset $k $f $v
129 }
130 set t [$r type $k]
131 }
132
133 switch $t {
134 {string} {
135 # Nothing to do
136 }
137 {list} {
138 randpath {$r lpush $k $v} \
139 {$r rpush $k $v} \
140 {$r lrem $k 0 $v} \
141 {$r rpop $k} \
142 {$r lpop $k}
143 }
144 {set} {
145 randpath {$r sadd $k $v} \
146 {$r srem $k $v}
147 }
148 {zset} {
149 randpath {$r zadd $k $d $v} \
150 {$r zrem $k $v}
151 }
152 {hash} {
153 randpath {$r hset $k $f $v} \
154 {$r hdel $k $f}
155 }
156 }
157 }
158 }