Skip to content

Commit

Permalink
Add simple test for Balance Monitor
Browse files Browse the repository at this point in the history
The test checks calls to the balance source function. In the future we
should check logged messages.
  • Loading branch information
nkuba committed Sep 1, 2021
1 parent f17cc48 commit 71ed95d
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions pkg/chain/ethlike/balance_monitor_test.go
Original file line number Diff line number Diff line change
@@ -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,
)
}
}

0 comments on commit 71ed95d

Please sign in to comment.