-
Notifications
You must be signed in to change notification settings - Fork 641
/
test_vm.go
166 lines (146 loc) · 4.37 KB
/
test_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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"errors"
"testing"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/snow"
)
var (
errInitialize = errors.New("unexpectedly called Initialize")
errBootstrapping = errors.New("unexpectedly called Bootstrapping")
errBootstrapped = errors.New("unexpectedly called Bootstrapped")
errShutdown = errors.New("unexpectedly called Shutdown")
errCreateHandlers = errors.New("unexpectedly called CreateHandlers")
errCreateStaticHandlers = errors.New("unexpectedly called CreateStaticHandlers")
errHealthCheck = errors.New("unexpectedly called HealthCheck")
errConnected = errors.New("unexpectedly called Connected")
errDisconnected = errors.New("unexpectedly called Disconnected")
errVersion = errors.New("unexpectedly called Version")
_ VM = &TestVM{}
)
// TestVM is a test vm
type TestVM struct {
T *testing.T
CantInitialize, CantBootstrapping, CantBootstrapped,
CantShutdown, CantCreateHandlers, CantCreateStaticHandlers,
CantHealthCheck, CantConnected, CantDisconnected, CantVersion bool
InitializeF func(*snow.Context, manager.Manager, []byte, []byte, []byte, chan<- Message, []*Fx) error
BootstrappingF, BootstrappedF, ShutdownF func() error
CreateHandlersF func() (map[string]*HTTPHandler, error)
CreateStaticHandlersF func() (map[string]*HTTPHandler, error)
ConnectedF func(ids.ShortID) error
DisconnectedF func(ids.ShortID) error
HealthCheckF func() (interface{}, error)
VersionF func() (string, error)
}
func (vm *TestVM) Default(cant bool) {
vm.CantInitialize = cant
vm.CantBootstrapping = cant
vm.CantBootstrapped = cant
vm.CantShutdown = cant
vm.CantCreateHandlers = cant
vm.CantCreateStaticHandlers = cant
vm.CantHealthCheck = cant
}
func (vm *TestVM) Initialize(ctx *snow.Context, db manager.Manager, genesisBytes, upgradeBytes, configBytes []byte, msgChan chan<- Message, fxs []*Fx) error {
if vm.InitializeF != nil {
return vm.InitializeF(ctx, db, genesisBytes, upgradeBytes, configBytes, msgChan, fxs)
}
if vm.CantInitialize && vm.T != nil {
vm.T.Fatal(errInitialize)
}
return errInitialize
}
func (vm *TestVM) Bootstrapping() error {
if vm.BootstrappingF != nil {
return vm.BootstrappingF()
}
if vm.CantBootstrapping {
if vm.T != nil {
vm.T.Fatal(errBootstrapping)
}
return errBootstrapping
}
return nil
}
func (vm *TestVM) Bootstrapped() error {
if vm.BootstrappedF != nil {
return vm.BootstrappedF()
}
if vm.CantBootstrapped {
if vm.T != nil {
vm.T.Fatal(errBootstrapped)
}
return errBootstrapped
}
return nil
}
func (vm *TestVM) Shutdown() error {
if vm.ShutdownF != nil {
return vm.ShutdownF()
}
if vm.CantShutdown {
if vm.T != nil {
vm.T.Fatal(errShutdown)
}
return errShutdown
}
return nil
}
func (vm *TestVM) CreateHandlers() (map[string]*HTTPHandler, error) {
if vm.CreateHandlersF != nil {
return vm.CreateHandlersF()
}
if vm.CantCreateHandlers && vm.T != nil {
vm.T.Fatal(errCreateHandlers)
}
return nil, nil
}
func (vm *TestVM) CreateStaticHandlers() (map[string]*HTTPHandler, error) {
if vm.CreateStaticHandlersF != nil {
return vm.CreateStaticHandlersF()
}
if vm.CantCreateStaticHandlers && vm.T != nil {
vm.T.Fatal(errCreateStaticHandlers)
}
return nil, nil
}
func (vm *TestVM) HealthCheck() (interface{}, error) {
if vm.HealthCheckF != nil {
return vm.HealthCheckF()
}
if vm.CantHealthCheck && vm.T != nil {
vm.T.Fatal(errHealthCheck)
}
return nil, errHealthCheck
}
func (vm *TestVM) Connected(id ids.ShortID) error {
if vm.ConnectedF != nil {
return vm.ConnectedF(id)
}
if vm.CantConnected && vm.T != nil {
vm.T.Fatal(errConnected)
}
return nil
}
func (vm *TestVM) Disconnected(id ids.ShortID) error {
if vm.DisconnectedF != nil {
return vm.DisconnectedF(id)
}
if vm.CantDisconnected && vm.T != nil {
vm.T.Fatal(errDisconnected)
}
return nil
}
func (vm *TestVM) Version() (string, error) {
if vm.VersionF != nil {
return vm.VersionF()
}
if vm.CantVersion && vm.T != nil {
vm.T.Fatal(errVersion)
}
return "", nil
}