Skip to content

Commit 71ed95d

Browse files
committed
Add simple test for Balance Monitor
The test checks calls to the balance source function. In the future we should check logged messages.
1 parent f17cc48 commit 71ed95d

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package ethlike
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/big"
7+
"sync"
8+
"time"
9+
10+
"testing"
11+
12+
"github.com/ipfs/go-log"
13+
)
14+
15+
func TestBalanceMonitor_Retries(t *testing.T) {
16+
log.SetDebugLogging()
17+
18+
attemptsCount := 0
19+
expectedAttempts := 3
20+
21+
wg := &sync.WaitGroup{}
22+
wg.Add(expectedAttempts)
23+
24+
balanceSource := func(address Address) (*Token, error) {
25+
attemptsCount++
26+
wg.Done()
27+
28+
if attemptsCount < expectedAttempts {
29+
return nil, fmt.Errorf("not this time")
30+
}
31+
32+
return &Token{big.NewInt(10)}, nil
33+
}
34+
35+
balanceMonitor := NewBalanceMonitor(balanceSource)
36+
37+
address := Address{1, 2}
38+
alertThreshold := &Token{big.NewInt(15)}
39+
tick := 1 * time.Minute
40+
retryTimeout := 5 * time.Second
41+
42+
balanceMonitor.Observe(
43+
context.Background(),
44+
address,
45+
alertThreshold,
46+
tick,
47+
retryTimeout,
48+
)
49+
50+
wg.Wait()
51+
52+
if expectedAttempts != attemptsCount {
53+
t.Errorf(
54+
"unexpected retries count\nexpected: %d\nactual: %d",
55+
expectedAttempts,
56+
attemptsCount,
57+
)
58+
}
59+
}

0 commit comments

Comments
 (0)