-
Notifications
You must be signed in to change notification settings - Fork 671
/
test_state_syncable_vm.go
88 lines (75 loc) · 2.88 KB
/
test_state_syncable_vm.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
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package block
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
)
var (
_ StateSyncableVM = (*TestStateSyncableVM)(nil)
errStateSyncEnabled = errors.New("unexpectedly called StateSyncEnabled")
errStateSyncGetOngoingSummary = errors.New("unexpectedly called StateSyncGetOngoingSummary")
errGetLastStateSummary = errors.New("unexpectedly called GetLastStateSummary")
errParseStateSummary = errors.New("unexpectedly called ParseStateSummary")
errGetStateSummary = errors.New("unexpectedly called GetStateSummary")
)
type TestStateSyncableVM struct {
T *testing.T
CantStateSyncEnabled,
CantStateSyncGetOngoingSummary,
CantGetLastStateSummary,
CantParseStateSummary,
CantGetStateSummary bool
StateSyncEnabledF func(context.Context) (bool, error)
GetOngoingSyncStateSummaryF func(context.Context) (StateSummary, error)
GetLastStateSummaryF func(context.Context) (StateSummary, error)
ParseStateSummaryF func(ctx context.Context, summaryBytes []byte) (StateSummary, error)
GetStateSummaryF func(ctx context.Context, summaryHeight uint64) (StateSummary, error)
}
func (vm *TestStateSyncableVM) StateSyncEnabled(ctx context.Context) (bool, error) {
if vm.StateSyncEnabledF != nil {
return vm.StateSyncEnabledF(ctx)
}
if vm.CantStateSyncEnabled && vm.T != nil {
require.FailNow(vm.T, errStateSyncEnabled.Error())
}
return false, errStateSyncEnabled
}
func (vm *TestStateSyncableVM) GetOngoingSyncStateSummary(ctx context.Context) (StateSummary, error) {
if vm.GetOngoingSyncStateSummaryF != nil {
return vm.GetOngoingSyncStateSummaryF(ctx)
}
if vm.CantStateSyncGetOngoingSummary && vm.T != nil {
require.FailNow(vm.T, errStateSyncGetOngoingSummary.Error())
}
return nil, errStateSyncGetOngoingSummary
}
func (vm *TestStateSyncableVM) GetLastStateSummary(ctx context.Context) (StateSummary, error) {
if vm.GetLastStateSummaryF != nil {
return vm.GetLastStateSummaryF(ctx)
}
if vm.CantGetLastStateSummary && vm.T != nil {
require.FailNow(vm.T, errGetLastStateSummary.Error())
}
return nil, errGetLastStateSummary
}
func (vm *TestStateSyncableVM) ParseStateSummary(ctx context.Context, summaryBytes []byte) (StateSummary, error) {
if vm.ParseStateSummaryF != nil {
return vm.ParseStateSummaryF(ctx, summaryBytes)
}
if vm.CantParseStateSummary && vm.T != nil {
require.FailNow(vm.T, errParseStateSummary.Error())
}
return nil, errParseStateSummary
}
func (vm *TestStateSyncableVM) GetStateSummary(ctx context.Context, summaryHeight uint64) (StateSummary, error) {
if vm.GetStateSummaryF != nil {
return vm.GetStateSummaryF(ctx, summaryHeight)
}
if vm.CantGetStateSummary && vm.T != nil {
require.FailNow(vm.T, errGetStateSummary.Error())
}
return nil, errGetStateSummary
}