Skip to content

Commit

Permalink
Add rpcClient tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 26, 2019
1 parent d560351 commit 662c229
Show file tree
Hide file tree
Showing 6 changed files with 520 additions and 49 deletions.
5 changes: 1 addition & 4 deletions pkg/app2/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ type Conn struct {
}

func (c *Conn) Read(b []byte) (int, error) {
n, readBytes, err := c.rpc.Read(c.id, b)
n, err := c.rpc.Read(c.id, b)
if err != nil {
return 0, err
}

// TODO: check for slice border
copy(b[:n], readBytes[:n])

return n, err
}

Expand Down
22 changes: 8 additions & 14 deletions pkg/app2/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,27 @@ func TestConn_Read(t *testing.T) {
connID := uint16(1)

tt := []struct {
name string
readBuff []byte
readN int
readBytes []byte
readErr error
wantBuff []byte
name string
readBuff []byte
readN int
readErr error
}{
{
name: "ok",
readBuff: make([]byte, 10),
readN: 2,
readBytes: []byte{1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
wantBuff: []byte{1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
name: "ok",
readBuff: make([]byte, 10),
readN: 2,
},
{
name: "read error",
readBuff: make([]byte, 10),
readErr: errors.New("read error"),
wantBuff: make([]byte, 10),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rpc := &MockRPCClient{}
rpc.On("Read", connID, tc.readBuff).Return(tc.readN, tc.readBytes, tc.readErr)
rpc.On("Read", connID, tc.readBuff).Return(tc.readN, tc.readErr)

conn := &Conn{
id: connID,
Expand All @@ -46,7 +41,6 @@ func TestConn_Read(t *testing.T) {
n, err := conn.Read(tc.readBuff)
require.Equal(t, tc.readErr, err)
require.Equal(t, tc.readN, n)
require.Equal(t, tc.wantBuff, tc.readBuff)
})
}
}
Expand Down
19 changes: 5 additions & 14 deletions pkg/app2/mock_rpc_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions pkg/app2/network/dmsg_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ func (n *DMSGNetworker) Dial(addr Addr) (net.Conn, error) {

// DialContext dials remote `addr` via dmsg network with context.
func (n *DMSGNetworker) DialContext(ctx context.Context, addr Addr) (net.Conn, error) {
tp, err := n.dmsgC.Dial(ctx, addr.PubKey, uint16(addr.Port))
if err != nil {
return nil, err
}

return WrapConn(tp)
return n.dmsgC.Dial(ctx, addr.PubKey, uint16(addr.Port))
}

// Listen starts listening on local `addr` in the dmsg network.
Expand Down
22 changes: 11 additions & 11 deletions pkg/app2/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type RPCClient interface {
Listen(local network.Addr) (uint16, error)
Accept(lisID uint16) (connID uint16, remote network.Addr, err error)
Write(connID uint16, b []byte) (int, error)
Read(connID uint16, b []byte) (int, []byte, error)
Read(connID uint16, b []byte) (int, error)
CloseConn(id uint16) error
CloseListener(id uint16) error
}
Expand All @@ -35,7 +35,7 @@ func NewRPCClient(rpc *rpc.Client) RPCClient {
// Dial sends `Dial` command to the server.
func (c *rpcCLient) Dial(remote network.Addr) (connID uint16, localPort routing.Port, err error) {
var resp DialResp
if err := c.rpc.Call("Dial", &remote, &resp); err != nil {
if err := c.rpc.Call("RPCGateway.Dial", &remote, &resp); err != nil {
return 0, 0, err
}

Expand All @@ -45,7 +45,7 @@ func (c *rpcCLient) Dial(remote network.Addr) (connID uint16, localPort routing.
// Listen sends `Listen` command to the server.
func (c *rpcCLient) Listen(local network.Addr) (uint16, error) {
var lisID uint16
if err := c.rpc.Call("Listen", &local, &lisID); err != nil {
if err := c.rpc.Call("RPCGateway.Listen", &local, &lisID); err != nil {
return 0, err
}

Expand All @@ -55,7 +55,7 @@ func (c *rpcCLient) Listen(local network.Addr) (uint16, error) {
// Accept sends `Accept` command to the server.
func (c *rpcCLient) Accept(lisID uint16) (connID uint16, remote network.Addr, err error) {
var acceptResp AcceptResp
if err := c.rpc.Call("Accept", &lisID, &acceptResp); err != nil {
if err := c.rpc.Call("RPCGateway.Accept", &lisID, &acceptResp); err != nil {
return 0, network.Addr{}, err
}

Expand All @@ -70,36 +70,36 @@ func (c *rpcCLient) Write(connID uint16, b []byte) (int, error) {
}

var n int
if err := c.rpc.Call("Write", &req, &n); err != nil {
if err := c.rpc.Call("RPCGateway.Write", &req, &n); err != nil {
return n, err
}

return n, nil
}

// Read sends `Read` command to the server.
func (c *rpcCLient) Read(connID uint16, b []byte) (int, []byte, error) {
func (c *rpcCLient) Read(connID uint16, b []byte) (int, error) {
req := ReadReq{
ConnID: connID,
BufLen: len(b),
}

var resp ReadResp
if err := c.rpc.Call("Read", &req, &resp); err != nil {
return 0, nil, err
if err := c.rpc.Call("RPCGateway.Read", &req, &resp); err != nil {
return 0, err
}

copy(b[:resp.N], resp.B[:resp.N])

return resp.N, resp.B, nil
return resp.N, nil
}

// CloseConn sends `CloseConn` command to the server.
func (c *rpcCLient) CloseConn(id uint16) error {
return c.rpc.Call("CloseConn", &id, nil)
return c.rpc.Call("RPCGateway.CloseConn", &id, nil)
}

// CloseListener sends `CloseListener` command to the server.
func (c *rpcCLient) CloseListener(id uint16) error {
return c.rpc.Call("CloseListener", &id, nil)
return c.rpc.Call("RPCGateway.CloseListener", &id, nil)
}
Loading

0 comments on commit 662c229

Please sign in to comment.