-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The test checks calls to the balance source function. In the future we should check logged messages.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} |