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