status-go/transactions/conditionalrepeater_test.go
Stefan 524c21834b fix(wallet) propagate multi-transactions IDs to transfers
Mainly refactor API to have control on pending_transactions operations.
Use the new API to migrate the multi-transaction ID from to transfers
in one SQL transaction.
The refactoring was done to better mirror the purpose of pending_transactions

Also:
- Externalize TransactionManager from WalletService to be used by
  other services
- Extract walletEvent as a dependency for all services that need to
  propagate events
- Batch chain requests
- Remove unused APIs
- Add auto delete option for clients that fire and forget transactions

Updates status-desktop #11754
2023-08-22 18:39:42 +02:00

80 lines
1.6 KiB
Go

package transactions
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestConditionalRepeater_RunOnce(t *testing.T) {
var wg sync.WaitGroup
runCount := 0
wg.Add(1)
taskRunner := NewConditionalRepeater(1*time.Nanosecond, func(ctx context.Context) bool {
runCount++
defer wg.Done()
return WorkDone
})
taskRunner.RunUntilDone()
// Wait for task to run
wg.Wait()
taskRunner.Stop()
require.Greater(t, runCount, 0)
}
func TestConditionalRepeater_RunUntilDone_MultipleCalls(t *testing.T) {
var wg sync.WaitGroup
wg.Add(5)
runCount := 0
taskRunner := NewConditionalRepeater(1*time.Nanosecond, func(ctx context.Context) bool {
runCount++
wg.Done()
return runCount == 5
})
for i := 0; i < 10; i++ {
taskRunner.RunUntilDone()
}
// Wait for all tasks to run
wg.Wait()
taskRunner.Stop()
require.Greater(t, runCount, 4)
}
func TestConditionalRepeater_Stop(t *testing.T) {
var taskRunningWG, taskCanceledWG, taskFinishedWG sync.WaitGroup
taskRunningWG.Add(1)
taskCanceledWG.Add(1)
taskFinishedWG.Add(1)
taskRunner := NewConditionalRepeater(1*time.Nanosecond, func(ctx context.Context) bool {
defer taskFinishedWG.Done()
select {
case <-ctx.Done():
require.Fail(t, "task should not be canceled yet")
default:
}
// Wait to caller to stop the task
taskRunningWG.Done()
taskCanceledWG.Wait()
select {
case <-ctx.Done():
require.Error(t, ctx.Err())
default:
require.Fail(t, "task should be canceled")
}
return WorkDone
})
taskRunner.RunUntilDone()
taskRunningWG.Wait()
taskRunner.Stop()
taskCanceledWG.Done()
taskFinishedWG.Wait()
}