Skip to content

Commit

Permalink
Merge pull request #73 from dotslashbit/issue-55
Browse files Browse the repository at this point in the history
feat: added ECHO command
  • Loading branch information
kelvinmwinuka authored Jun 18, 2024
2 parents b87c836 + 852c5ca commit 75baaa5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
26 changes: 25 additions & 1 deletion internal/modules/connection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package connection
import (
"errors"
"fmt"

"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/internal/constants"
)
Expand All @@ -32,13 +33,20 @@ func handlePing(params internal.HandlerFuncParams) ([]byte, error) {
}
}

func handleEcho(params internal.HandlerFuncParams) ([]byte, error) {
if len(params.Command) != 2 {
return nil, errors.New(constants.WrongArgsResponse)
}
return []byte(fmt.Sprintf("$%d\r\n%s\r\n", len(params.Command[1]), params.Command[1])), nil
}

func Commands() []internal.Command {
return []internal.Command{
{
Command: "ping",
Module: constants.ConnectionModule,
Categories: []string{constants.ConnectionCategory, constants.FastCategory},
Description: `(PING [message])
Description: `(PING [message])
Ping the echovault server. If a message is provided, the message will be echoed back to the client.
Otherwise, the server will return "PONG".`,
Sync: false,
Expand All @@ -51,5 +59,21 @@ Otherwise, the server will return "PONG".`,
},
HandlerFunc: handlePing,
},
{
Command: "echo",
Module: constants.ConnectionModule,
Categories: []string{constants.ConnectionCategory, constants.FastCategory},
Description: `(ECHO message)
Echo the message back to the client.`,
Sync: false,
KeyExtractionFunc: func(cmd []string) (internal.KeyExtractionFuncResult, error) {
return internal.KeyExtractionFuncResult{
Channels: make([]string, 0),
ReadKeys: make([]string, 0),
WriteKeys: make([]string, 0),
}, nil
},
HandlerFunc: handleEcho,
},
}
}
67 changes: 65 additions & 2 deletions internal/modules/connection/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package connection_test

import (
"errors"
"strings"
"testing"

"github.com/echovault/echovault/echovault"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/internal/config"
"github.com/echovault/echovault/internal/constants"
"github.com/tidwall/resp"
"strings"
"testing"
)

func Test_Connection(t *testing.T) {
Expand Down Expand Up @@ -114,4 +115,66 @@ func Test_Connection(t *testing.T) {
}
})


t.Run("Test_HandleEcho", func(t *testing.T) {
conn, err := internal.GetConnection("localhost", port)
if err != nil {
t.Error(err)
return
}
defer func() {
_ = conn.Close()
}()
client := resp.NewConn(conn)

tests := []struct {
command []resp.Value
expected string
expectedErr error
}{
{
command: []resp.Value{resp.StringValue("ECHO"), resp.StringValue("Hello, EchoVault!")},
expected: "Hello, EchoVault!",
expectedErr: nil,
},
{
command: []resp.Value{resp.StringValue("ECHO")},
expected: "",
expectedErr: errors.New(constants.WrongArgsResponse),
},
{
command: []resp.Value{
resp.StringValue("ECHO"),
resp.StringValue("Hello, EchoVault!"),
resp.StringValue("Once more"),
},
expected: "",
expectedErr: errors.New(constants.WrongArgsResponse),
},
}

for _, test := range tests {
if err = client.WriteArray(test.command); err != nil {
t.Error(err)
return
}

res, _, err := client.ReadValue()
if err != nil {
t.Error(err)
}

if test.expectedErr != nil {
if !strings.Contains(res.Error().Error(), test.expectedErr.Error()) {
t.Errorf("expected error \"%s\", got \"%s\"", test.expectedErr.Error(), res.Error().Error())
}
continue
}

if res.String() != test.expected {
t.Errorf("expected response \"%s\", got \"%s\"", test.expected, res.String())
}
}
})

}

0 comments on commit 75baaa5

Please sign in to comment.