Skip to content

Commit

Permalink
Add unit tests, improve integ tests in session pkg (#142)
Browse files Browse the repository at this point in the history
- Add unit tests for all methods of Session and Channel types.
  For make it feasible to tests, add a few interfaces.

- Improve the integration tests used for client initialization
  to increase coverage.

Closes #131.

Signed-off-by: Manoranjith <ponraj.manoranjitha@in.bosch.com>
  • Loading branch information
manoranjith committed Dec 18, 2020
1 parent 27efa4d commit c10154d
Show file tree
Hide file tree
Showing 19 changed files with 1,897 additions and 205 deletions.
35 changes: 30 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,43 @@ type client struct {

// pClient represents the methods on client.Client that are used by client.
type pClient interface {
ProposeChannel(context.Context, pclient.ChannelProposal) (*pclient.Channel, error)
ProposeChannel(context.Context, pclient.ChannelProposal) (perun.Channel, error)
Handle(pclient.ProposalHandler, pclient.UpdateHandler)
Channel(pchannel.ID) (*pclient.Channel, error)
Channel(pchannel.ID) (perun.Channel, error)
Close() error

EnablePersistence(ppersistence.PersistRestorer)
OnNewChannel(handler func(*pclient.Channel))
OnNewChannel(handler func(perun.Channel))
Restore(context.Context) error

Log() plog.Logger
}

// pclientWrapped is a wrapper around pclient.Client that returns a channel of interface type
// instead of struct type. This enables easier mocking of the returned value in tests.
type pclientWrapped struct {
*pclient.Client
}

// ProposeChannel is a wrapper around the original function, that returns a channel of interface type instead of
// struct type.
func (c *pclientWrapped) ProposeChannel(ctx context.Context, proposal pclient.ChannelProposal) (perun.Channel, error) {
return c.Client.ProposeChannel(ctx, proposal)
}

// Channel is a wrapper around the original function, that returns a channel of interface type instead of struct type.
func (c *pclientWrapped) Channel(id pchannel.ID) (perun.Channel, error) {
return c.Client.Channel(id)
}

// OnNewChannel is a wrapper around the original function, that takes a handler that takes channel of interface type as
// argument instead of the handler in original function that takes channel of struct type as argument.
func (c *pclientWrapped) OnNewChannel(handler func(perun.Channel)) {
c.Client.OnNewChannel(func(ch *pclient.Channel) {
handler(ch)
})
}

// NewEthereumPaymentClient initializes a two party, ethereum payment channel client for the given user.
// It establishes a connection to the blockchain and verifies the integrity of contracts at the given address.
// It uses the comm backend to initialize adapters for off-chain communication network.
Expand All @@ -93,7 +118,7 @@ func NewEthereumPaymentClient(cfg Config, user perun.User, comm perun.CommBacken
}

c := &client{
pClient: pcClient,
pClient: &pclientWrapped{pcClient},
msgBus: msgBus,
msgBusRegistry: dialer,
dbPath: cfg.DatabaseDir,
Expand Down Expand Up @@ -123,7 +148,7 @@ func (c *client) Handle(ph pclient.ProposalHandler, ch pclient.UpdateHandler) {

// RestoreChs will restore the persisted channels. Register OnNewChannel Callback
// before calling this function.
func (c *client) RestoreChs(handler func(*pclient.Channel)) error {
func (c *client) RestoreChs(handler func(perun.Channel)) error {
c.OnNewChannel(handler)
db, err := pleveldb.LoadDatabase(c.dbPath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions client/client_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
pclient "perun.network/go-perun/client"

"github.com/hyperledger-labs/perun-node"
"github.com/hyperledger-labs/perun-node/blockchain/ethereum/ethereumtest"
"github.com/hyperledger-labs/perun-node/client"
"github.com/hyperledger-labs/perun-node/client/clienttest"
Expand Down Expand Up @@ -73,7 +73,7 @@ func Test_Integ_NewEthereumPaymentClient(t *testing.T) {
cfg.DatabaseDir = newDatabaseDir(t) // start with empty persistence dir each time.
client, err := client.NewEthereumPaymentClient(cfg, user, tcp.NewTCPBackend(tcptest.DialerTimeout))
require.NoError(t, err)
err = client.RestoreChs(func(*pclient.Channel) {})
err = client.RestoreChs(func(perun.Channel) {})
assert.NoError(t, err)
assert.NoError(t, client.Close())
})
Expand Down
6 changes: 3 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func Test_Client_Register(t *testing.T) {
// happy path test is covered in integration test, as internal components of
// the client should be initialized.
t.Run("happy", func(t *testing.T) {
registere := &mocks.Registerer{}
registerer := &mocks.Registerer{}
dbConnCloser := &mocks.Closer{}
Client := client.NewClientForTest(nil, nil, registere, dbConnCloser)
registere.On("Register", nil, "").Return()
Client := client.NewClientForTest(nil, nil, registerer, dbConnCloser)
registerer.On("Register", nil, "").Return()
Client.Register(nil, "")
})
}
24 changes: 13 additions & 11 deletions internal/mocks/ChClient.go

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

55 changes: 55 additions & 0 deletions internal/mocks/ChProposalResponder.go

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

42 changes: 42 additions & 0 deletions internal/mocks/ChUpdateResponder.go

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

Loading

0 comments on commit c10154d

Please sign in to comment.