[#280] ir: Add balance processor unit tests

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-04-26 11:42:02 +03:00 committed by Evgenii Stratonikov
parent e89fa7f69f
commit 686f01bce5
2 changed files with 96 additions and 2 deletions

View file

@ -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
}

View file

@ -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