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            wait_for_condition 50 100 {
6                [s -1 master_link_status] eq {up}
7            } else {
8                fail "Replication not started."
9            }
10        }
11
12        if {$::accurate} {set numops 50000} else {set numops 5000}
13
14        test {MASTER and SLAVE consistency with expire} {
15            createComplexDataset r $numops useexpire
16            after 4000 ;# Make sure everything expired before taking the digest
17            r keys *   ;# Force DEL syntesizing to slave
18            after 1000 ;# Wait another second. Now everything should be fine.
19            if {[r debug digest] ne [r -1 debug digest]} {
20                set csv1 [csvdump r]
21                set csv2 [csvdump {r -1}]
22                set fd [open /tmp/repldump1.txt w]
23                puts -nonewline $fd $csv1
24                close $fd
25                set fd [open /tmp/repldump2.txt w]
26                puts -nonewline $fd $csv2
27                close $fd
28                puts "Master - Slave inconsistency"
29                puts "Run diff -u against /tmp/repldump*.txt for more info"
30            }
31            assert_equal [r debug digest] [r -1 debug digest]
32        }
33    }
34}
35
36start_server {tags {"repl"}} {
37    start_server {} {
38        test {First server should have role slave after SLAVEOF} {
39            r -1 slaveof [srv 0 host] [srv 0 port]
40            wait_for_condition 50 100 {
41                [s -1 master_link_status] eq {up}
42            } else {
43                fail "Replication not started."
44            }
45        }
46
47        set numops 20000 ;# Enough to trigger the Script Cache LRU eviction.
48
49        # While we are at it, enable AOF to test it will be consistent as well
50        # after the test.
51        r config set appendonly yes
52
53        test {MASTER and SLAVE consistency with EVALSHA replication} {
54            array set oldsha {}
55            for {set j 0} {$j < $numops} {incr j} {
56                set key "key:$j"
57                # Make sure to create scripts that have different SHA1s
58                set script "return redis.call('incr','$key')"
59                set sha1 [r eval "return redis.sha1hex(\"$script\")" 0]
60                set oldsha($j) $sha1
61                r eval $script 0
62                set res [r evalsha $sha1 0]
63                assert {$res == 2}
64                # Additionally call one of the old scripts as well, at random.
65                set res [r evalsha $oldsha([randomInt $j]) 0]
66                assert {$res > 2}
67
68                # Trigger an AOF rewrite while we are half-way, this also
69                # forces the flush of the script cache, and we will cover
70                # more code as a result.
71                if {$j == $numops / 2} {
72                    catch {r bgrewriteaof}
73                }
74            }
75
76            wait_for_condition 50 100 {
77                [r dbsize] == $numops &&
78                [r -1 dbsize] == $numops &&
79                [r debug digest] eq [r -1 debug digest]
80            } else {
81                set csv1 [csvdump r]
82                set csv2 [csvdump {r -1}]
83                set fd [open /tmp/repldump1.txt w]
84                puts -nonewline $fd $csv1
85                close $fd
86                set fd [open /tmp/repldump2.txt w]
87                puts -nonewline $fd $csv2
88                close $fd
89                puts "Master - Slave inconsistency"
90                puts "Run diff -u against /tmp/repldump*.txt for more info"
91
92            }
93
94            set old_digest [r debug digest]
95            r config set appendonly no
96            r debug loadaof
97            set new_digest [r debug digest]
98            assert {$old_digest eq $new_digest}
99        }
100    }
101}
102