diff --git a/cmd/tezosconnect.go b/cmd/tezosconnect.go index 98c7095..e01138d 100644 --- a/cmd/tezosconnect.go +++ b/cmd/tezosconnect.go @@ -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) }, } @@ -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")) diff --git a/cmd/tezosconnect_test.go b/cmd/tezosconnect_test.go new file mode 100644 index 0000000..f0eef3d --- /dev/null +++ b/cmd/tezosconnect_test.go @@ -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) + } + }) + } +} diff --git a/internal/tezos/blocklistener.go b/internal/tezos/blocklistener.go index af63808..9075384 100644 --- a/internal/tezos/blocklistener.go +++ b/internal/tezos/blocklistener.go @@ -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() { - bl.mux.Lock() - listenLoopDone := bl.listenLoopDone - bl.mux.Unlock() - if listenLoopDone != nil { - <-listenLoopDone - } -} diff --git a/internal/tezos/tezos.go b/internal/tezos/tezos.go index cdedfe7..4a4712a 100644 --- a/internal/tezos/tezos.go +++ b/internal/tezos/tezos.go @@ -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 - } -} diff --git a/internal/tezos/tezos_test.go b/internal/tezos/tezos_test.go index acffbab..cec979d 100644 --- a/internal/tezos/tezos_test.go +++ b/internal/tezos/tezos_test.go @@ -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() @@ -27,7 +25,6 @@ func newTestConnector(t *testing.T) (context.Context, *tezosConnector, *tzrpcbac return ctx, c, mRPC, func() { done() mRPC.AssertExpectations(t) - c.WaitClosed() } }