From 71ed95dda8ed4029c770a0050714cd4a4be813b5 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Wed, 1 Sep 2021 17:25:23 +0200 Subject: [PATCH] Add simple test for Balance Monitor The test checks calls to the balance source function. In the future we should check logged messages. --- pkg/chain/ethlike/balance_monitor_test.go | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 pkg/chain/ethlike/balance_monitor_test.go diff --git a/pkg/chain/ethlike/balance_monitor_test.go b/pkg/chain/ethlike/balance_monitor_test.go new file mode 100644 index 0000000..6438cd3 --- /dev/null +++ b/pkg/chain/ethlike/balance_monitor_test.go @@ -0,0 +1,59 @@ +package ethlike + +import ( + "context" + "fmt" + "math/big" + "sync" + "time" + + "testing" + + "github.com/ipfs/go-log" +) + +func TestBalanceMonitor_Retries(t *testing.T) { + log.SetDebugLogging() + + attemptsCount := 0 + expectedAttempts := 3 + + wg := &sync.WaitGroup{} + wg.Add(expectedAttempts) + + balanceSource := func(address Address) (*Token, error) { + attemptsCount++ + wg.Done() + + if attemptsCount < expectedAttempts { + return nil, fmt.Errorf("not this time") + } + + return &Token{big.NewInt(10)}, nil + } + + balanceMonitor := NewBalanceMonitor(balanceSource) + + address := Address{1, 2} + alertThreshold := &Token{big.NewInt(15)} + tick := 1 * time.Minute + retryTimeout := 5 * time.Second + + balanceMonitor.Observe( + context.Background(), + address, + alertThreshold, + tick, + retryTimeout, + ) + + wg.Wait() + + if expectedAttempts != attemptsCount { + t.Errorf( + "unexpected retries count\nexpected: %d\nactual: %d", + expectedAttempts, + attemptsCount, + ) + } +}