]> git.saurik.com Git - redis.git/blame - tests/integration/replication.tcl
Make sure that SELECT argument is an integer or return an error.
[redis.git] / tests / integration / replication.tcl
CommitLineData
8b654e54 1start_server {tags {"repl"}} {
2 start_server {} {
3 test {First server should have role slave after SLAVEOF} {
4 r -1 slaveof [srv 0 host] [srv 0 port]
d9241b35 5 wait_for_condition 50 100 {
6 [s -1 role] eq {slave} &&
7 [string match {*master_link_status:up*} [r -1 info replication]]
8 } else {
9 fail "Can't turn the instance into a slave"
10 }
11 }
8b654e54 12
c1c9d551 13 test {BRPOPLPUSH replication, when blocking against empty list} {
14 set rd [redis_deferring_client]
15 $rd brpoplpush a b 5
16 r lpush a foo
28ccb530 17 wait_for_condition 50 100 {
18 [r debug digest] eq [r -1 debug digest]
19 } else {
20 fail "Master and slave have different digest: [r debug digest] VS [r -1 debug digest]"
21 }
c1c9d551 22 }
23
24 test {BRPOPLPUSH replication, list exists} {
25 set rd [redis_deferring_client]
26 r lpush c 1
27 r lpush c 2
28 r lpush c 3
29 $rd brpoplpush c d 5
30 after 1000
31 assert_equal [r debug digest] [r -1 debug digest]
32 }
8b654e54 33 }
34}
35
7f7499ee 36start_server {tags {"repl"}} {
85ecc65e
PN
37 r set mykey foo
38
9e5d2e8b 39 start_server {} {
85ecc65e
PN
40 test {Second server should have role master at first} {
41 s role
42 } {master}
43
44 test {SLAVEOF should start with link status "down"} {
45 r slaveof [srv -1 host] [srv -1 port]
46 s master_link_status
47 } {down}
48
49 test {The role should immediately be changed to "slave"} {
50 s role
51 } {slave}
52
53 wait_for_sync r
54 test {Sync should have transferred keys from master} {
55 r get mykey
56 } {foo}
57
58 test {The link status should be up} {
59 s master_link_status
60 } {up}
61
62 test {SET on the master should immediately propagate} {
63 r -1 set mykey bar
4c378d7f 64 if {$::valgrind} {after 2000}
85ecc65e
PN
65 r 0 get mykey
66 } {bar}
c0875a77 67
68 test {FLUSHALL should replicate} {
69 r -1 flushall
70 if {$::valgrind} {after 2000}
71 list [r -1 dbsize] [r 0 dbsize]
72 } {0 0}
85ecc65e
PN
73 }
74}
414c3dea 75
76proc start_write_load {host port seconds} {
77 exec tclsh8.5 tests/helpers/gen_write_load.tcl $host $port $seconds &
78}
79
80proc stop_write_load {handle} {
81 catch {exec /bin/kill -9 $handle}
82}
83
84start_server {tags {"repl"}} {
85 set master [srv 0 client]
86 set master_host [srv 0 host]
87 set master_port [srv 0 port]
88 set slaves {}
2d4b5521 89 set load_handle0 [start_write_load $master_host $master_port 3]
90 set load_handle1 [start_write_load $master_host $master_port 5]
414c3dea 91 set load_handle2 [start_write_load $master_host $master_port 20]
2d4b5521 92 set load_handle3 [start_write_load $master_host $master_port 8]
93 set load_handle4 [start_write_load $master_host $master_port 4]
414c3dea 94 start_server {} {
95 lappend slaves [srv 0 client]
96 start_server {} {
97 lappend slaves [srv 0 client]
98 start_server {} {
99 lappend slaves [srv 0 client]
100 test "Connect multiple slaves at the same time (issue #141)" {
2d4b5521 101 # Send SALVEOF commands to slaves
414c3dea 102 [lindex $slaves 0] slaveof $master_host $master_port
103 [lindex $slaves 1] slaveof $master_host $master_port
104 [lindex $slaves 2] slaveof $master_host $master_port
105
106 # Wait for all the three slaves to reach the "online" state
459e2975 107 set retry 500
414c3dea 108 while {$retry} {
109 set info [r -3 info]
110 if {[string match {*slave0:*,online*slave1:*,online*slave2:*,online*} $info]} {
111 break
112 } else {
113 incr retry -1
114 after 100
115 }
116 }
117 if {$retry == 0} {
118 error "assertion:Slaves not correctly synchronized"
119 }
2d4b5521 120
121 # Stop the write load
414c3dea 122 stop_write_load $load_handle0
123 stop_write_load $load_handle1
124 stop_write_load $load_handle2
125 stop_write_load $load_handle3
126 stop_write_load $load_handle4
2d4b5521 127
128 # Wait that slaves exit the "loading" state
129 wait_for_condition 500 100 {
130 ![string match {*loading:1*} [[lindex $slaves 0] info]] &&
131 ![string match {*loading:1*} [[lindex $slaves 1] info]] &&
132 ![string match {*loading:1*} [[lindex $slaves 2] info]]
133 } else {
134 fail "Slaves still loading data after too much time"
06312eed 135 }
2d4b5521 136
137 # Make sure that slaves and master have same number of keys
138 wait_for_condition 500 100 {
139 [$master dbsize] == [[lindex $slaves 0] dbsize] &&
140 [$master dbsize] == [[lindex $slaves 1] dbsize] &&
141 [$master dbsize] == [[lindex $slaves 2] dbsize]
142 } else {
143 fail "Different number of keys between masted and slave after too long time."
144 }
145
146 # Check digests
414c3dea 147 set digest [$master debug digest]
148 set digest0 [[lindex $slaves 0] debug digest]
149 set digest1 [[lindex $slaves 1] debug digest]
150 set digest2 [[lindex $slaves 2] debug digest]
151 assert {$digest ne 0000000000000000000000000000000000000000}
152 assert {$digest eq $digest0}
153 assert {$digest eq $digest1}
154 assert {$digest eq $digest2}
414c3dea 155 }
156 }
157 }
158 }
159}