From 686f01bce5649c8056d52a824041f759a83f50d8 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Wed, 26 Apr 2023 11:42:02 +0300 Subject: [PATCH] [#280] ir: Add balance processor unit tests Signed-off-by: Dmitrii Stepanov --- .../processors/balance/handlers_test.go | 90 +++++++++++++++++++ pkg/innerring/processors/balance/processor.go | 8 +- 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 pkg/innerring/processors/balance/handlers_test.go diff --git a/pkg/innerring/processors/balance/handlers_test.go b/pkg/innerring/processors/balance/handlers_test.go new file mode 100644 index 000000000..3470fba2d --- /dev/null +++ b/pkg/innerring/processors/balance/handlers_test.go @@ -0,0 +1,90 @@ +package balance + +import ( + "testing" + "time" + + frostfscontract "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/frostfs" + balanceEvent "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event/balance" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger/test" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/stretchr/testify/require" +) + +func TestProcessorCallsFrostFSContractForLockEvent(t *testing.T) { + t.Parallel() + as := &testAlphabetState{ + isAlphabet: true, + } + conv := &testPresicionConverter{} + cl := &testFrostFSContractClient{} + bsc := util.Uint160{100} + + processor, err := New(&Params{ + Log: test.NewLogger(t, true), + PoolSize: 2, + FrostFSClient: cl, + BalanceSC: bsc, + AlphabetState: as, + Converter: conv, + }) + require.NoError(t, err, "failed to create processor") + + processor.handleLock(balanceEvent.Lock{}) + + for processor.pool.Running() > 0 { + time.Sleep(10 * time.Millisecond) + } + + require.Equal(t, 1, cl.chequeCalls, "invalid Cheque calls") +} + +func TestProcessorDoesntCallFrostFSContractIfNotAlphabet(t *testing.T) { + t.Parallel() + as := &testAlphabetState{} + conv := &testPresicionConverter{} + cl := &testFrostFSContractClient{} + bsc := util.Uint160{100} + + processor, err := New(&Params{ + Log: test.NewLogger(t, true), + PoolSize: 2, + FrostFSClient: cl, + BalanceSC: bsc, + AlphabetState: as, + Converter: conv, + }) + require.NoError(t, err, "failed to create processor") + + processor.handleLock(balanceEvent.Lock{}) + + for processor.pool.Running() > 0 { + time.Sleep(10 * time.Millisecond) + } + + require.Equal(t, 0, cl.chequeCalls, "invalid Cheque calls") +} + +type testAlphabetState struct { + isAlphabet bool +} + +func (s *testAlphabetState) IsAlphabet() bool { + return s.isAlphabet +} + +type testPresicionConverter struct { +} + +func (c *testPresicionConverter) ToFixed8(v int64) int64 { + return v +} + +type testFrostFSContractClient struct { + chequeCalls int +} + +func (c *testFrostFSContractClient) Cheque(p frostfscontract.ChequePrm) error { + c.chequeCalls++ + return nil +} diff --git a/pkg/innerring/processors/balance/processor.go b/pkg/innerring/processors/balance/processor.go index 7ae639e89..356754cfb 100644 --- a/pkg/innerring/processors/balance/processor.go +++ b/pkg/innerring/processors/balance/processor.go @@ -25,11 +25,15 @@ type ( ToFixed8(int64) int64 } + FrostFSClient interface { + Cheque(p frostfscontract.ChequePrm) error + } + // Processor of events produced by balance contract in the morphchain. Processor struct { log *logger.Logger pool *ants.Pool - frostfsClient *frostfscontract.Client + frostfsClient FrostFSClient balanceSC util.Uint160 alphabetState AlphabetState converter PrecisionConverter @@ -39,7 +43,7 @@ type ( Params struct { Log *logger.Logger PoolSize int - FrostFSClient *frostfscontract.Client + FrostFSClient FrostFSClient BalanceSC util.Uint160 AlphabetState AlphabetState Converter PrecisionConverter