-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit tests for ping handler
- Loading branch information
Showing
4 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{})) | ||
} |