-
Notifications
You must be signed in to change notification settings - Fork 1
/
info_test.go
60 lines (51 loc) · 1.91 KB
/
info_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package sentinel
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_parseInfoSlave(t *testing.T) {
}
// func Test_parseInfoMaster(t *testing.T) {
// s := &Sentinel{
// masterInstances: map[string]*masterInstance{},
// mu: &sync.Mutex{},
// }
// s.masterInstances["fake_master"] = &masterInstance{
// slaves: map[string]*slaveInstance{},
// }
// info := `role:master
// run_id:5a4dfcf77726cfcda754cbdc0a42d1cec6a7348d
// connected_slaves:1
// slave0:ip=127.0.0.1,port=6379,state=online,offset=42,lag=0
// master_failover_state:no-failover
// `
// roleSwitched, err := s.parseInfoMaster("fake_master", info)
// assert.False(t, roleSwitched)
// assert.NoError(t, err)
// m := s.masterInstances["fake_master"]
// assert.Equal(t, "5a4dfcf77726cfcda754cbdc0a42d1cec6a7348d", m.runID)
// assert.Equal(t, 1, len(m.slaves))
// slave, exist := m.slaves["127.0.0.1:6379"]
// assert.True(t, exist)
// assert.Equal(t, "127.0.0.1:6379", slave.addr)
// assert.Equal(t, 42, slave.replOffset)
// }
func Test_slaveInfoRegexp(t *testing.T) {
ok := slaveInfoRegexp.MatchString("ip=127.0.0.1,port=abc,state=online,offset=0,lag=0")
assert.False(t, ok)
ok = slaveInfoRegexp.MatchString("ip=127.0.0.1,port=123,state=something,offset=0,lag=0")
assert.False(t, ok)
ok = slaveInfoRegexp.MatchString("ip=127.0.0.1,port=123,state=offline,offset=abc,lag=0")
assert.False(t, ok)
ok = slaveInfoRegexp.MatchString("ip=127.0.0.1,port=123,state=online,offset=0,lag=abc")
assert.False(t, ok)
ok = slaveInfoRegexp.MatchString("ip=127.0.0.1,port=123,state=online,offset=0,lag=0")
assert.True(t, ok)
matches := slaveInfoRegexp.FindStringSubmatch("ip=127.0.0.1,port=123,state=online,offset=43,lag=74")
assert.Equal(t, 6, len(matches))
assert.Equal(t, "127.0.0.1", matches[1])
assert.Equal(t, "123", matches[2])
assert.Equal(t, "online", matches[3])
assert.Equal(t, "43", matches[4])
assert.Equal(t, "74", matches[5])
}