-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpegout_bridge_watcher_test.go
More file actions
114 lines (109 loc) · 5.86 KB
/
pegout_bridge_watcher_test.go
File metadata and controls
114 lines (109 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package watcher_test
import (
"context"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/common/math"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/entrypoints/watcher"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
"github.com/rsksmart/liquidity-provider-server/internal/entities"
"github.com/rsksmart/liquidity-provider-server/internal/entities/blockchain"
"github.com/rsksmart/liquidity-provider-server/internal/entities/liquidity_provider"
"github.com/rsksmart/liquidity-provider-server/internal/entities/quote"
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
"github.com/rsksmart/liquidity-provider-server/internal/usecases/pegout"
w "github.com/rsksmart/liquidity-provider-server/internal/usecases/watcher"
"github.com/rsksmart/liquidity-provider-server/test"
"github.com/rsksmart/liquidity-provider-server/test/mocks"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
// nolint:funlen
func TestPegoutBridgeWatcher_Start(t *testing.T) {
ticker := &mocks.TickerMock{}
tickerChannel := make(chan time.Time)
ticker.EXPECT().C().Return(tickerChannel)
pegoutRepository := &mocks.PegoutQuoteRepositoryMock{}
providerMock := &mocks.ProviderMock{}
rskWallet := &mocks.RskWalletMock{}
bridge := &mocks.BridgeMock{}
bridge.On("GetAddress").Return(test.AnyAddress)
mutexes := environment.NewApplicationMutexes()
bridgeUseCase := pegout.NewBridgePegoutUseCase(pegoutRepository, providerMock, rskWallet, blockchain.RskContracts{Bridge: bridge}, mutexes.RskWalletMutex(), pegout.AllAtOnce)
getUseCase := w.NewGetWatchedPegoutQuoteUseCase(pegoutRepository)
bridgeWatcher := watcher.NewPegoutBridgeWatcher(getUseCase, bridgeUseCase, ticker)
resetMocks := func() {
pegoutRepository.Calls = []mock.Call{}
pegoutRepository.ExpectedCalls = []*mock.Call{}
providerMock.Calls = []mock.Call{}
providerMock.ExpectedCalls = []*mock.Call{}
rskWallet.Calls = []mock.Call{}
rskWallet.ExpectedCalls = []*mock.Call{}
}
go bridgeWatcher.Start()
t.Run("should handle error getting quotes", func(t *testing.T) {
resetMocks()
checkFunc := test.AssertLogContains(t, "error getting pegout quotes")
pegoutRepository.EXPECT().GetRetainedQuoteByState(mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
tickerChannel <- time.Now()
assert.Eventually(t, func() bool { return checkFunc() && pegoutRepository.AssertExpectations(t) }, time.Second, 10*time.Millisecond)
})
const quoteHash = "0102"
t.Run("should log error sending tx to the bridge", func(t *testing.T) {
resetMocks()
checkFunc := test.AssertLogContains(t, "error sending pegout to bridge")
pegoutRepository.EXPECT().GetRetainedQuoteByState(mock.Anything, quote.PegoutStateRefundPegOutSucceeded).Return([]quote.RetainedPegoutQuote{
{QuoteHash: quoteHash, State: quote.PegoutStateRefundPegOutSucceeded},
}, nil).Once()
pegoutRepository.EXPECT().GetQuote(mock.Anything, quoteHash).Return("e.PegoutQuote{Value: entities.NewBigWei(math.BigPow(10, 19))}, nil).Once()
pegoutRepository.EXPECT().GetPegoutCreationData(mock.Anything, mock.Anything).Return(quote.PegoutCreationData{GasPrice: entities.NewWei(1)}).Once()
providerMock.On("PegoutConfiguration", mock.Anything).Return(liquidity_provider.DefaultPegoutConfiguration()).Once()
rskWallet.On("GetBalance", mock.Anything).Return((*entities.Wei)(nil), assert.AnError).Once()
tickerChannel <- time.Now()
assert.Eventually(t, func() bool {
return checkFunc() && rskWallet.AssertExpectations(t) && providerMock.AssertExpectations(t) && pegoutRepository.AssertExpectations(t)
}, time.Second, 10*time.Millisecond)
})
t.Run("should send tx to the bridge successfully", func(t *testing.T) {
resetMocks()
log.SetLevel(log.DebugLevel)
checkFunc := test.AssertLogContains(t, "transaction sent to the bridge successfully")
pegoutRepository.EXPECT().GetRetainedQuoteByState(mock.Anything, quote.PegoutStateRefundPegOutSucceeded).Return([]quote.RetainedPegoutQuote{
{QuoteHash: quoteHash, State: quote.PegoutStateRefundPegOutSucceeded},
}, nil).Once()
pegoutRepository.EXPECT().GetQuote(mock.Anything, quoteHash).Return("e.PegoutQuote{Value: entities.NewBigWei(math.BigPow(10, 19))}, nil).Once()
providerMock.On("PegoutConfiguration", mock.Anything).Return(liquidity_provider.DefaultPegoutConfiguration()).Once()
rskWallet.On("GetBalance", mock.Anything).Return(entities.NewBigWei(math.BigPow(10, 20)), nil).Once()
sendRbtcReceipt := blockchain.TransactionReceipt{
TransactionHash: test.AnyHash,
BlockHash: "0xblock123",
BlockNumber: uint64(1000),
From: "0x123",
To: test.AnyAddress,
CumulativeGasUsed: big.NewInt(21000),
GasUsed: big.NewInt(21000),
Value: entities.NewWei(0),
GasPrice: entities.NewWei(1000000000),
}
rskWallet.On("SendRbtc", mock.Anything, mock.Anything, mock.Anything).Return(sendRbtcReceipt, nil).Once()
pegoutRepository.EXPECT().UpdateRetainedQuotes(mock.Anything, mock.Anything).Return(nil).Once()
pegoutRepository.EXPECT().GetPegoutCreationData(mock.Anything, mock.Anything).Return(quote.PegoutCreationData{GasPrice: entities.NewWei(1)}).Once()
tickerChannel <- time.Now()
assert.Eventually(t, func() bool {
return checkFunc() && rskWallet.AssertExpectations(t) && providerMock.AssertExpectations(t) && pegoutRepository.AssertExpectations(t)
}, time.Second, 10*time.Millisecond)
})
}
func TestPegoutBridgeWatcher_Prepare(t *testing.T) {
bridgeWatcher := watcher.NewPegoutBridgeWatcher(nil, nil, nil)
err := bridgeWatcher.Prepare(context.Background())
require.NoError(t, err)
}
func TestPegoutBridgeWatcher_Shutdown(t *testing.T) {
createWatcherShutdownTest(t, func(ticker utils.Ticker) watcher.Watcher {
return watcher.NewPegoutBridgeWatcher(nil, nil, ticker)
})
}