Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test coverage and code cleanup #26

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/tezosconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ var rootCmd = &cobra.Command{
Short: "Hyperledger FireFly Connector for Tezos blockchain",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
return run()
ctx, cancelCtx := context.WithCancel(context.Background())
return run(ctx, cancelCtx)
},
}

Expand All @@ -52,11 +53,10 @@ func InitConfig() {
txhandlerfactory.RegisterHandler(&simple.TransactionHandlerFactory{})
}

func run() error {
func run(ctx context.Context, cancelCtx context.CancelFunc) error {
err := config.ReadConfig("tezosconnect", cfgFile)

// Setup logging after reading config (even if failed), to output header correctly
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
ctx = log.WithLogger(ctx, logrus.WithField("pid", fmt.Sprintf("%d", os.Getpid())))
ctx = log.WithLogger(ctx, logrus.WithField("prefix", "tezosconnect"))
Expand Down
79 changes: 79 additions & 0 deletions cmd/tezosconnect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"context"
"os"
"testing"
"time"

"github.com/hyperledger/firefly-common/pkg/config"
"github.com/hyperledger/firefly-tezosconnect/internal/tezos"
"github.com/stretchr/testify/assert"
)

func TestRun(t *testing.T) {
testCases := []struct {
name string
errMsg string
initFunc func()
cleanupFunc func()
}{
{
name: "success",
initFunc: func() {
f, err := os.Create("firefly.tezosconnect")
assert.NoError(t, err)
err = f.Close()
assert.NoError(t, err)

dir, err := os.MkdirTemp("", "ldb_*")
assert.NoError(t, err)
config.Set("persistence.leveldb.path", dir)
},
cleanupFunc: func() {
err := os.Remove("firefly.tezosconnect")
assert.NoError(t, err)
},
},
{
name: "error on config not found",
initFunc: func() {},
cleanupFunc: func() {},
errMsg: "FF00101: Failed to read config: Config File \"firefly.tezosconnect\" Not Found",
},
{
name: "error on NewTezosConnector",
initFunc: func() {
f, err := os.Create("firefly.tezosconnect")
assert.NoError(t, err)
err = f.Close()
assert.NoError(t, err)

connectorConfig.Set(tezos.TxCacheSize, "-1")
},
cleanupFunc: func() {
err := os.Remove("firefly.tezosconnect")
assert.NoError(t, err)
},
errMsg: "FF23040: Failed to initialize transaction cache: Must provide a positive size",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tc.initFunc()
ctx, cancelCtx := context.WithTimeout(context.Background(), time.Second)

err := run(ctx, cancelCtx)

tc.cleanupFunc()

if tc.errMsg != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.errMsg)
} else {
assert.NoError(t, err)
}
})
}
}
9 changes: 0 additions & 9 deletions internal/tezos/blocklistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,3 @@ func (bl *blockListener) getHighestBlock(ctx context.Context) int64 {
log.L(ctx).Debugf("ChainHead=%d", highestBlock)
return highestBlock
}

func (bl *blockListener) waitClosed() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is used only from test and can be omitted there, so let's remove it

bl.mux.Lock()
listenLoopDone := bl.listenLoopDone
bl.mux.Unlock()
if listenLoopDone != nil {
<-listenLoopDone
}
}
10 changes: 0 additions & 10 deletions internal/tezos/tezos.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,3 @@ func NewTezosConnector(ctx context.Context, conf config.Section) (cc ffcapi.API,

return c, nil
}

// WaitClosed can be called after cancelling all the contexts, to wait for everything to close down
func (c *tezosConnector) WaitClosed() {
if c.blockListener != nil {
c.blockListener.waitClosed()
}
for _, s := range c.eventStreams {
<-s.streamLoopDone
}
}
3 changes: 0 additions & 3 deletions internal/tezos/tezos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/stretchr/testify/assert"
)

func strPtr(s string) *string { return &s }

func newTestConnector(t *testing.T) (context.Context, *tezosConnector, *tzrpcbackendmocks.RpcClient, func()) {
mRPC := &tzrpcbackendmocks.RpcClient{}
config.RootConfigReset()
Expand All @@ -27,7 +25,6 @@ func newTestConnector(t *testing.T) (context.Context, *tezosConnector, *tzrpcbac
return ctx, c, mRPC, func() {
done()
mRPC.AssertExpectations(t)
c.WaitClosed()
}
}

Expand Down