Skip to content

Commit

Permalink
Add even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 21, 2019
1 parent 0714e55 commit 3491994
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/app2/network/networker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestAddNetworker(t *testing.T) {
clearNetworkers()

nType := Type(TypeDMSG)
nType := TypeDMSG
var n Networker

err := AddNetworker(nType, n)
Expand All @@ -28,7 +28,7 @@ func TestAddNetworker(t *testing.T) {
func TestResolveNetworker(t *testing.T) {
clearNetworkers()

nType := Type(TypeDMSG)
nType := TypeDMSG
var n Networker

n, err := ResolveNetworker(nType)
Expand Down
2 changes: 1 addition & 1 deletion pkg/app2/network/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Type string

const (
// TypeDMSG is a network type for DMSG communication.
TypeDMSG = "dmsg"
TypeDMSG Type = "dmsg"
)

// IsValid checks whether the network contains valid value for the type.
Expand Down
6 changes: 4 additions & 2 deletions pkg/app2/rpc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newRPCGateway(log *logging.Logger) *RPCGateway {

// Dial dials to the remote.
func (r *RPCGateway) Dial(remote *network.Addr, connID *uint16) error {
connID, err := r.cm.nextKey()
reservedConnID, err := r.cm.nextKey()
if err != nil {
return err
}
Expand All @@ -38,14 +38,16 @@ func (r *RPCGateway) Dial(remote *network.Addr, connID *uint16) error {
return err
}

if err := r.cm.set(*connID, conn); err != nil {
if err := r.cm.set(*reservedConnID, conn); err != nil {
if err := conn.Close(); err != nil {
r.log.WithError(err).Error("error closing conn")
}

return err
}

*connID = *reservedConnID

return nil
}

Expand Down
49 changes: 49 additions & 0 deletions pkg/app2/rpc_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package app2

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"github.com/skycoin/dmsg"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skywire/pkg/routing"

"github.com/skycoin/skywire/pkg/app2/network"

"github.com/skycoin/skycoin/src/util/logging"
)

func TestRPCGateway_Dial(t *testing.T) {
l := logging.MustGetLogger("rpc_gateway")
nType := network.TypeDMSG

dialCtx := context.Background()
dialAddrPK, _ := cipher.GenerateKeyPair()
dialAddrPort := routing.Port(100)
dialAddr := network.Addr{
Net: nType,
PubKey: dialAddrPK,
Port: dialAddrPort,
}
dialConn := &dmsg.Transport{}
var dialErr error

n := &network.MockNetworker{}
n.On("DialContext", dialCtx, dialAddr).Return(dialConn, dialErr)

err := network.AddNetworker(nType, n)
require.NoError(t, err)

rpc := newRPCGateway(l)

t.Run("ok", func(t *testing.T) {
var connID uint16

err := rpc.Dial(&dialAddr, &connID)
require.NoError(t, err)
require.Equal(t, connID, uint16(1))
})
}

0 comments on commit 3491994

Please sign in to comment.