Skip to content

Commit

Permalink
Add app server tests, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Oct 28, 2019
1 parent f4127c2 commit 96b6b70
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkg/app2/appcommon/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package appcommon
3 changes: 3 additions & 0 deletions pkg/app2/appserver/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package appserver contains facilities
// for implementing the skywire app server.
package appserver
4 changes: 2 additions & 2 deletions pkg/app2/appserver/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os/exec"
"path/filepath"

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

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

"github.com/skycoin/skywire/pkg/app2/appcommon"
)

// Proc is a wrapper for a skywire app. Encapsulates
Expand Down
3 changes: 1 addition & 2 deletions pkg/app2/appserver/proc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os/exec"
"sync"

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

"github.com/pkg/errors"

"github.com/skycoin/skycoin/src/util/logging"
"github.com/skycoin/skywire/pkg/app2/appcommon"
)

var (
Expand Down
5 changes: 2 additions & 3 deletions pkg/app2/appserver/rpc_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"strings"
"testing"

"github.com/skycoin/skywire/pkg/app2/appcommon"
"github.com/skycoin/skywire/pkg/app2/idmanager"

"github.com/pkg/errors"
"github.com/skycoin/dmsg"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/app2/appcommon"
"github.com/skycoin/skywire/pkg/app2/appnet"
"github.com/skycoin/skywire/pkg/app2/idmanager"
"github.com/skycoin/skywire/pkg/routing"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/app2/appserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"net/rpc"
"sync"

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

"github.com/pkg/errors"
"github.com/skycoin/skycoin/src/util/logging"

"github.com/skycoin/skywire/pkg/app2/appcommon"
)

// Server is a server for app/visor communication.
Expand Down
101 changes: 101 additions & 0 deletions pkg/app2/appserver/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package appserver_test

import (
"fmt"
"strings"
"testing"
"time"

"github.com/skycoin/dmsg"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/skycoin/skywire/pkg/app2"
"github.com/skycoin/skywire/pkg/app2/appcommon"
"github.com/skycoin/skywire/pkg/app2/appnet"
"github.com/skycoin/skywire/pkg/app2/appserver"
"github.com/skycoin/skywire/pkg/routing"
)

func TestServer_ListenAndServe(t *testing.T) {
l := logging.MustGetLogger("app_server")
sockFile := "app.sock"
appKey := appcommon.GenerateAppKey()

s, err := appserver.New(l, sockFile, appKey)
require.NoError(t, err)

visorPK, _ := cipher.GenerateKeyPair()
clientConfig := app2.ClientConfig{
VisorPK: visorPK,
SockFile: sockFile,
AppKey: appKey,
}

errCh := make(chan error)
go func() {
err := s.ListenAndServe()
if err != nil {
fmt.Printf("ListenAndServe error: %v\n", err)
}
errCh <- err
}()

time.Sleep(500 * time.Millisecond)

dmsgLocal, dmsgRemote, remote := prepAddrs()

var noErr error

conn := &appcommon.MockConn{}
conn.On("LocalAddr").Return(dmsgLocal)
conn.On("RemoteAddr").Return(dmsgRemote)
conn.On("Close").Return(noErr)

appnet.ClearNetworkers()
n := &appnet.MockNetworker{}
n.On("DialContext", mock.Anything, remote).Return(conn, noErr)

err = appnet.AddNetworker(appnet.TypeDMSG, n)
require.NoError(t, err)

cl, err := app2.NewClient(logging.MustGetLogger("app_client"), clientConfig)
require.NoError(t, err)

gotConn, err := cl.Dial(remote)
require.NoError(t, err)
require.NotNil(t, gotConn)
require.Equal(t, remote, gotConn.RemoteAddr())

err = s.Close()
require.NoError(t, err)

err = <-errCh
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "use of closed network connection"))
}

func prepAddrs() (dmsgLocal, dmsgRemote dmsg.Addr, remote appnet.Addr) {
localPK, _ := cipher.GenerateKeyPair()
localPort := uint16(10)
dmsgLocal = dmsg.Addr{
PK: localPK,
Port: localPort,
}

remotePK, _ := cipher.GenerateKeyPair()
remotePort := uint16(11)
dmsgRemote = dmsg.Addr{
PK: remotePK,
Port: remotePort,
}
remote = appnet.Addr{
Net: appnet.TypeDMSG,
PubKey: remotePK,
Port: routing.Port(remotePort),
}

return
}

0 comments on commit 96b6b70

Please sign in to comment.