Skip to content

Commit

Permalink
test: test psync builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeDP committed Feb 25, 2024
1 parent 7506ca2 commit e0e29ab
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 38 deletions.
2 changes: 2 additions & 0 deletions app/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type IMutex interface {
type Builtin interface {
Response([]string)
Request([]string)
GetConn() net.Conn
SetConn(net.Conn)
GetTimeNow() time.Time
SetTimeNow(time.Time)
}

Expand Down
41 changes: 39 additions & 2 deletions app/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ type SetupFilesRDWR struct {
Env map[string]EnvData
Mutex IMutex
TimeNow time.Time
Infos map[string]map[string]string
}

func (s *SetupFilesRDWR) config(data map[string]EnvData) {
func (s *SetupFilesRDWR) config(data map[string]EnvData, infos map[string]map[string]string) {
s.In = make([]byte, define.BUFFERSIZE)
s.Out = make([]byte, define.BUFFERSIZE)
s.Expected = make([]byte, define.BUFFERSIZE)
s.Conn = TConn{In: s.In, Out: s.Out}
s.Env = data
s.Mutex = TMutex{}
s.TimeNow = time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
s.Infos = infos
}

func (s *SetupFilesRDWR) reset() {
Expand Down Expand Up @@ -137,4 +139,39 @@ Mutex struct Mock
type TMutex struct{}
func (m TMutex) Lock() {}
func (m TMutex) Unlock() {}
func (m TMutex) TryLock() bool { return false }
func (m TMutex) TryLock() bool { return false }

func TestBultinsSets(t *testing.T) {
s := SetupFilesRDWR{}
s.config(nil, nil)

builtins := []Builtin{
&Set{},
&Get{},
&Echo{},
&Info{},
&Ping{},
&PSync{},
&ReplConf{},
}

t.Run("Test Set TimeNow", func(t *testing.T) {
for _, builtin := range builtins {
builtin.SetTimeNow(s.TimeNow)

if builtin.GetTimeNow() != s.TimeNow {
t.Errorf("Expected %v, but has %v\n", s.TimeNow, builtin.GetTimeNow())
}
}
})

t.Run("Test Set Conn", func(t *testing.T) {
for _, builtin := range builtins {
builtin.SetConn(s.Conn)

if !reflect.DeepEqual(builtin.GetConn(), s.Conn) {
t.Errorf("Expected %v, but has %v\n", s.Conn, builtin.GetConn())
}
}
})
}
8 changes: 8 additions & 0 deletions app/builtin/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func (e *Echo) SetConn(conn net.Conn) {
e.Conn = conn
}

func (e *Echo) GetConn() net.Conn {
return e.Conn
}

func (e *Echo) SetTimeNow(now time.Time) {
e.Now = now
}

func (e *Echo) GetTimeNow() time.Time {
return e.Now
}
4 changes: 2 additions & 2 deletions app/builtin/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func TestEchoBuiltin(t *testing.T) {
s := SetupFilesRDWR{}
s.config(nil)
s.config(nil, nil)

t.Run("Test pass a \"hey\" string", func(t *testing.T) {
echo := Echo{Conn: s.Conn}
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestEchoBuiltin(t *testing.T) {

func BenchmarkEchoBuiltin(b *testing.B) {
s := SetupFilesRDWR{}
s.config(nil)
s.config(nil, nil)
params := []string{"hey", "ho", "lets", "go"}
echo := Echo{Conn: s.Conn}

Expand Down
8 changes: 8 additions & 0 deletions app/builtin/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func (g *Get) SetConn(conn net.Conn) {
g.Conn = conn
}

func (g *Get) GetConn() net.Conn {
return g.Conn
}

func (g *Get) SetTimeNow(now time.Time) {
g.Now = now
}

func (g *Get) GetTimeNow() time.Time {
return g.Now
}
4 changes: 2 additions & 2 deletions app/builtin/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestGetBuiltin(t *testing.T) {
s.config(map[string]EnvData{
"Percy": {Value: "Jackson", Expiry: s.TimeNow, MustExpire: false},
"Key": {Value: "Value", Expiry: s.TimeNow.Add(-10 * time.Millisecond), MustExpire: true},
})
}, nil)

t.Run("Test get key Percy and response Jackson", func(t *testing.T) {
get := Get{Conn: s.Conn, Env: s.Env, Mutex: s.Mutex}
Expand Down Expand Up @@ -50,7 +50,7 @@ func BenchmarkGetBuiltin(b *testing.B) {
s.config(map[string]EnvData{
"Percy": {Value: "Jackson", Expiry: s.TimeNow, MustExpire: false},
"Key": {Value: "Value", Expiry: s.TimeNow.Add(-10 * time.Millisecond), MustExpire: true},
})
}, nil)
get := Get{Conn: s.Conn, Env: s.Env, Mutex: s.Mutex}
params := []string{"Percy"}

Expand Down
8 changes: 8 additions & 0 deletions app/builtin/infos.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func (i *Info) SetConn(conn net.Conn) {
i.Conn = conn
}

func (i *Info) GetConn() net.Conn {
return i.Conn
}

func (i *Info) SetTimeNow(now time.Time) {
i.Now = now
}

func (i *Info) GetTimeNow() time.Time {
return i.Now
}
2 changes: 1 addition & 1 deletion app/builtin/infos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func TestInfoBuiltin(t *testing.T) {
s := SetupFilesRDWR{}
s.config(nil)
s.config(nil, nil)

t.Run("Test Info only command", func(t *testing.T) {
i := map[string]map[string]string{
Expand Down
8 changes: 8 additions & 0 deletions app/builtin/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func (p *Ping) SetConn(conn net.Conn) {
p.Conn = conn
}

func (p *Ping) GetConn() net.Conn {
return p.Conn
}

func (p *Ping) SetTimeNow(now time.Time) {
p.Now = now
}

func (p *Ping) GetTimeNow() time.Time {
return p.Now
}
28 changes: 2 additions & 26 deletions app/builtin/ping_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package builtin

import (
"reflect"
"testing"
)

func TestPingBuiltinRequest(t *testing.T) {
s := SetupFilesRDWR{}
s.config(nil)
s.config(nil, nil)

t.Run("Test ping with string 'ping'", func(t *testing.T) {
ping := Ping{Conn: s.Conn}
Expand All @@ -21,32 +20,9 @@ func TestPingBuiltinRequest(t *testing.T) {
})
}

func TestPingSets(t *testing.T) {
s := SetupFilesRDWR{}
s.config(nil)

t.Run("Test set Ping time now", func(t *testing.T) {
ping := Ping{Conn: s.Conn}
ping.SetTimeNow(s.TimeNow)

if ping.Now != s.TimeNow {
t.Errorf("Expected %v, but has %v\n", s.TimeNow, ping.Now)
}
})

t.Run("Test set Ping conn", func(t *testing.T) {
ping := Ping{}
ping.SetConn(s.Conn)

if !reflect.DeepEqual(ping.Conn, s.Conn) {
t.Errorf("Expected %v, but has %v\n", s.Conn, ping.Conn)
}
})
}

func BenchmarkPingBuiltin(b *testing.B) {
s := SetupFilesRDWR{}
s.config(nil)
s.config(nil, nil)
params := []string{"ping"}
ping := Ping{Conn: s.Conn}

Expand Down
13 changes: 11 additions & 2 deletions app/builtin/psync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ func (p *PSync) Request(params []string) {
}

func (p *PSync) Response(params []string) {
str := fmt.Sprintf("+FULLRESYNC %s 0\r\n", p.Infos["replication"]["master_replid"])
str := fmt.Sprintf("+FULLRESYNC %s %s\r\n",
p.Infos["replication"]["master_replid"],
p.Infos["replication"]["master_repl_offset"])
p.Conn.Write([]byte(str))
}

func (p *PSync) SetConn(conn net.Conn) {
p.Conn = conn
}

func (p *PSync) GetConn() net.Conn {
return p.Conn
}

func (p *PSync) SetTimeNow(now time.Time) {
p.Now = now
}
//

func (p *PSync) GetTimeNow() time.Time {
return p.Now
}
61 changes: 61 additions & 0 deletions app/builtin/psync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package builtin

import (
"testing"
)

func TestPSyncBuiltin(t *testing.T) {
s := SetupFilesRDWR{}
getTime := TTime{}
s.config(nil, map[string]map[string]string{
"replication": {
"master_replid": "8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb",
"master_repl_offset": "0",
}})

t.Run("Test Request ['?', '-1'] command", func(t *testing.T) {
sp := PSync{Conn: s.Conn, Infos: s.Infos, Now: getTime.Now()}
params := []string{"?", "-1"}
copy(s.Expected, "+FULLRESYNC 8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb 0\r\n")

sp.Response(params)
compareStrings(t, s.Expected, s.Out)
s.reset()
})

t.Run("Test Response PSYNC ? -1 test", func(t *testing.T) {
sp := PSync{Conn: s.Conn, Infos: s.Infos, Now: getTime.Now()}
params := []string{"PSYNC", "?", "-1"}
copy(s.Expected, "*3\r\n$5\r\nPSYNC\r\n$1\r\n?\r\n$2\r\n-1\r\n")

sp.Request(params)
compareStrings(t, s.Expected, s.Out)
s.reset()
})
}

func BenchmarkPSyncBuiltin(b *testing.B) {
s := SetupFilesRDWR{}
getTime := TTime{}
s.config(nil, map[string]map[string]string{
"replication": {
"master_replid": "8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb",
"master_repl_offset": "0",
},
})
rc := PSync{Conn: s.Conn, Infos: s.Infos, Now: getTime.Now()}

b.Run("Benchmark handler Request psync ? -1", func(b *testing.B) {
params := []string{"?", "-1"}
for i := 0; i < b.N; i++ {
rc.Response(params)
}
})

b.Run("Benchmark handler Response psync ? -1", func(b *testing.B) {
params := []string{"REPLCONF", "?", "-1"}
for i := 0; i < b.N; i++ {
rc.Request(params)
}
})
}
8 changes: 8 additions & 0 deletions app/builtin/replconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func (rc *ReplConf) SetConn(conn net.Conn) {
rc.Conn = conn
}

func (rc * ReplConf) GetConn() net.Conn {
return rc.Conn
}

func (rc *ReplConf) SetTimeNow(now time.Time) {
rc.Now = now
}

func (rc *ReplConf) GetTimeNow() time.Time {
return rc.Now
}
23 changes: 22 additions & 1 deletion app/builtin/replconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func TestReplConfBuiltin(t *testing.T) {
s := SetupFilesRDWR{}
getTime := TTime{}
s.config(nil)
s.config(nil, nil)

t.Run("Test handler Request reply conf listening-port 6380", func(t *testing.T) {
rc := ReplConf{Conn: s.Conn, Env: s.Env, Now: getTime.Now()}
Expand Down Expand Up @@ -42,3 +42,24 @@ func TestReplConfBuiltin(t *testing.T) {
s.reset()
})
}

func BenchmarkReplConfBuiltin(b *testing.B) {
s := SetupFilesRDWR{}
getTime := TTime{}
s.config(nil, nil)
rc := ReplConf{Conn: s.Conn, Env: s.Env, Now: getTime.Now()}

b.Run("Benchmark handler Request reply conf listening-port 6380", func(b *testing.B) {
params := []string{"listening-port", "6380"}
for i := 0; i < b.N; i++ {
rc.Response(params)
}
})

b.Run("Benchmark handler Response reply conf listening-port 6380", func(b *testing.B) {
params := []string{"REPLCONF", "listening-port", "6380"}
for i := 0; i < b.N; i++ {
rc.Request(params)
}
})
}
8 changes: 8 additions & 0 deletions app/builtin/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (s *Set) SetConn(conn net.Conn) {
s.Conn = conn
}

func (s *Set) GetConn() net.Conn {
return s.Conn
}

func (s *Set) SetTimeNow(now time.Time) {
s.Now = now
}

func (s *Set) GetTimeNow() time.Time {
return s.Now
}
Loading

0 comments on commit e0e29ab

Please sign in to comment.