Skip to content

Commit

Permalink
pass repl
Browse files Browse the repository at this point in the history
  • Loading branch information
ThreeDP committed Feb 25, 2024
1 parent 76adb64 commit 44e68a5
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 60 deletions.
50 changes: 50 additions & 0 deletions app/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"strings"
"testing"
"net"
"time"

"github.com/codecrafters-io/redis-starter-go/app/define"
Expand Down Expand Up @@ -88,3 +89,52 @@ func (s *SetupFilesRDWR) reset() {
s.Conn = TConn{In: s.In, Out: s.Out}
}

/*
Test Conn struct Mock
*/
type TConn struct {
In []byte
Out []byte
}

func (c TConn) Close() error { return nil }
func (c TConn) SetDeadline(t time.Time) error { return nil }
func (c TConn) SetReadDeadline(t time.Time) error { return nil }
func (c TConn) SetWriteDeadline(t time.Time) error { return nil }

func (c TConn) Read(b []byte) (n int, err error) {
copy(c.In, b)
return len(b), nil
}

func (c TConn) Write(b []byte) (n int, err error) {
copy(c.Out, b)
return len(b), nil
}

func (c TConn) LocalAddr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8080,
}
}

func (c TConn) RemoteAddr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8080,
}
}

func (c *TConn) NewConn() {
c.In = make([]byte, define.BUFFERSIZE)
c.Out = make([]byte, define.BUFFERSIZE)
}

/*
Mutex struct Mock
*/
type TMutex struct{}
func (m TMutex) Lock() {}
func (m TMutex) Unlock() {}
func (m TMutex) TryLock() bool { return false }
58 changes: 0 additions & 58 deletions app/builtin/connTest_struct.go

This file was deleted.

2 changes: 2 additions & 0 deletions app/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"
)



func TestParserRedisProtocol(t *testing.T) {
t.Run("Test parser the bulk string $4hello\\r\\n", func(t *testing.T) {
rrp := RedisProtocolParser{Idx:0}
Expand Down
54 changes: 52 additions & 2 deletions app/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,56 @@ import (
"github.com/codecrafters-io/redis-starter-go/app/define"
)

/*
Test Conn struct Mock
*/
type TConn struct {
In []byte
Out []byte
}

func (c TConn) Close() error { return nil }
func (c TConn) SetDeadline(t time.Time) error { return nil }
func (c TConn) SetReadDeadline(t time.Time) error { return nil }
func (c TConn) SetWriteDeadline(t time.Time) error { return nil }

func (c TConn) Read(b []byte) (n int, err error) {
copy(c.In, b)
return len(b), nil
}

func (c TConn) Write(b []byte) (n int, err error) {
copy(c.Out, b)
return len(b), nil
}

func (c TConn) LocalAddr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8080,
}
}

func (c TConn) RemoteAddr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 8080,
}
}

func (c *TConn) NewConn() {
c.In = make([]byte, define.BUFFERSIZE)
c.Out = make([]byte, define.BUFFERSIZE)
}

/*
Mutex struct Mock
*/
type TMutex struct{}
func (m TMutex) Lock() {}
func (m TMutex) Unlock() {}
func (m TMutex) TryLock() bool { return false }

type TTime struct {}
func (t TTime) Now() time.Time {
return time.Date(2009, 1, 1, 12, 0, 0, 0, time.UTC)
Expand Down Expand Up @@ -53,7 +103,7 @@ func setupRedisServer(env map[string]builtin.EnvData) RedisServer {
Time: TTime{},
Args: []string{}, //os.Args[1:]
Infos: make(map[string]map[string]string),
Mutex: builtin.TMutex{},
Mutex: TMutex{},
}
}

Expand Down Expand Up @@ -235,7 +285,7 @@ func TestHandleRequest(t *testing.T) {
s := setupRedisServer(map[string]builtin.EnvData{
"Percy": {Value: "Jackson", Expiry: tm.Now(), MustExpire: false},
})
conn := builtin.TConn{}
conn := TConn{}
s.SetCommands()

t.Run("Test *2\\r\\n$4\\r\\necho\\r\\n$2\\r\\nhi\\r\\n command", func(t *testing.T) {
Expand Down

0 comments on commit 44e68a5

Please sign in to comment.