Skip to content

Commit

Permalink
feat: add unit tests for ping handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PapePathe committed Apr 13, 2024
1 parent 677cf37 commit b193367
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
69 changes: 69 additions & 0 deletions internal/commands/client_request_mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package commands

import (
"errors"
"pedis/internal/storage"

"go.etcd.io/etcd/raft/v3/raftpb"
)

var (
NotImplementedError error = errors.New("mock caller must provide implementation")
)

type MockClient struct {
r RawRequest
d [][]byte
response []string
errors []string
}

func (mock MockClient) WriteError(e string) error {
mock.errors = append(mock.errors, e)
return nil
}

func (mock *MockClient) WriteString(s string) error {
mock.response = append(mock.response, s)
return nil
}

func (mock MockClient) WriteNumber(s string) error {
mock.response = append(mock.response, s)
return nil
}

func (mock MockClient) WriteArray(s []string) error {
mock.response = s
return nil
}

func (mock MockClient) WriteOK() error {
mock.response = []string{"OK"}
return nil
}

func (mock MockClient) WriteNil() error {
mock.response = []string{"NIL"}
return nil
}

func (mock MockClient) Write([]byte) (int, error) {
return 0, NotImplementedError
}

func (mock MockClient) Data() [][]byte {
return mock.d
}

func (mock MockClient) DataRaw() RawRequest {
return mock.r
}

func (mock MockClient) Store() storage.Storage {
panic("mock caller must provide implementation of Store()")
}

func (mock MockClient) SendClusterConfigChange(raftpb.ConfChange) {
panic("mock caller must provide implementation of SendClusterConfigChange()")
}
2 changes: 2 additions & 0 deletions internal/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"log"
"strings"
"sync"
)
Expand Down Expand Up @@ -35,6 +36,7 @@ func DefaultRequestHandler() *RequestHandler {
}

func (s RequestHandler) Run(request IClientRequest) {
log.Println("RedisCommand", request.DataRaw().String(), "PedisParams", request.Data())
subcommand := strings.ToLower(string(request.Data()[2]))

if h, ok := s.subcommands[subcommand]; ok {
Expand Down
17 changes: 14 additions & 3 deletions internal/commands/handler_ping.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package commands

import "log"

type PingHandler struct{}

func (ch PingHandler) Authorize(IClientRequest) error {
return nil
}

func (ch PingHandler) Permissions(IClientRequest) []string {
return []string{}
return []string{
"fast",
"connection",
}
}

func (ch PingHandler) Persistent(IClientRequest) bool {
Expand All @@ -18,11 +23,17 @@ func (ch PingHandler) Handle(r IClientRequest) {
data := r.DataRaw().ReadArray()

if len(data) == 0 {
r.WriteString("PONG")
err := r.WriteString("PONG")
if err != nil {
log.Println("error writing to client", err)
}
return
}

r.WriteString(data[0])
err := r.WriteString(data[0])
if err != nil {
log.Println("error writing to client", err)
}
}

func init() {
Expand Down
51 changes: 51 additions & 0 deletions internal/commands/handler_ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package commands

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPingHandle(t *testing.T) {
body := []byte("*1\r\n$4\r\nping\r\n")
h := PingHandler{}
r := MockClient{
r: RawRequest(body),
d: bytes.Split(body, []byte{13, 10}),
}

h.Handle(&r)

assert.Equal(t, []string{"PONG"}, r.response)
}

func TestCustomPingHandle(t *testing.T) {
body := []byte("*1\r\n$4\r\nping\r\n$4\r\nnangadef\r\n")
h := PingHandler{}
r := MockClient{
r: RawRequest(body),
d: bytes.Split(body, []byte{13, 10}),
}

h.Handle(&r)

assert.Equal(t, []string{"nangadef"}, r.response)
}

func TestPingPermissions(t *testing.T) {
h := PingHandler{}
perms := h.Permissions(&MockClient{})
assert.Equal(t, []string{"fast", "connection"}, perms)
}

func TestPingAuthorize(t *testing.T) {
h := PingHandler{}
require.NoError(t, h.Authorize(&MockClient{}))
}

func TestPingPersistent(t *testing.T) {
h := PingHandler{}
require.False(t, h.Persistent(&MockClient{}))
}

0 comments on commit b193367

Please sign in to comment.