forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l1_rollup_info_consumer_test.go
76 lines (68 loc) · 2.49 KB
/
l1_rollup_info_consumer_test.go
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
package synchronizer
import (
"context"
"errors"
"testing"
"time"
"github.com/0xPolygonHermez/zkevm-node/etherman"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type consumerTestData struct {
sut *l1RollupInfoConsumer
syncMock *synchronizerProcessBlockRangeMock
ch chan l1SyncMessage
}
func TestGivenConsumerWhenReceiveAFullSyncAndChannelIsEmptyThenStopOk(t *testing.T) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
data := setupConsumerTest(t)
defer cancel()
data.ch <- *newL1SyncMessageControl(eventProducerIsFullySynced)
err := data.sut.Start(ctxTimeout, nil)
require.NoError(t, err)
}
func TestGivenConsumerWhenReceiveAFullSyncAndChannelIsNotEmptyThenDontStop(t *testing.T) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
data := setupConsumerTest(t)
defer cancel()
data.ch <- *newL1SyncMessageControl(eventProducerIsFullySynced)
data.ch <- *newL1SyncMessageControl(eventNone)
err := data.sut.Start(ctxTimeout, nil)
require.Error(t, err)
require.Equal(t, errContextCanceled, err)
}
func TestGivenConsumerWhenFailsToProcessRollupThenDontKnownLastEthBlock(t *testing.T) {
ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
data := setupConsumerTest(t)
defer cancel()
responseRollupInfoByBlockRange := rollupInfoByBlockRangeResult{
blockRange: blockRange{
fromBlock: 100,
toBlock: 200,
},
blocks: []etherman.Block{},
order: map[common.Hash][]etherman.Order{},
lastBlockOfRange: nil,
}
data.syncMock.
On("processBlockRange", mock.Anything, mock.Anything).
Return(errors.New("error")).
Once()
data.ch <- *newL1SyncMessageData(&responseRollupInfoByBlockRange)
data.ch <- *newL1SyncMessageControl(eventProducerIsFullySynced)
err := data.sut.Start(ctxTimeout, nil)
require.Error(t, err)
_, ok := data.sut.GetLastEthBlockSynced()
require.False(t, ok)
}
func setupConsumerTest(t *testing.T) consumerTestData {
syncMock := newSynchronizerProcessBlockRangeMock(t)
ch := make(chan l1SyncMessage, 10)
cfg := configConsumer{
numIterationsBeforeStartCheckingTimeWaitingForNewRollupInfoData: minNumIterationsBeforeStartCheckingTimeWaitingForNewRollupInfoData,
acceptableTimeWaitingForNewRollupInfoData: minAcceptableTimeWaitingForNewRollupInfoData,
}
sut := newL1RollupInfoConsumer(cfg, syncMock, ch)
return consumerTestData{sut, syncMock, ch}
}