-
Notifications
You must be signed in to change notification settings - Fork 669
/
test_vm.go
222 lines (199 loc) · 6.07 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"errors"
"testing"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/snow"
)
var (
errInitialize = errors.New("unexpectedly called Initialize")
errSetState = errors.New("unexpectedly called SetState")
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")
errAppRequest = errors.New("unexpectedly called AppRequest")
errAppResponse = errors.New("unexpectedly called AppResponse")
errAppRequestFailed = errors.New("unexpectedly called AppRequestFailed")
errAppGossip = errors.New("unexpectedly called AppGossip")
_ VM = &TestVM{}
)
// TestVM is a test vm
type TestVM struct {
T *testing.T
CantInitialize, CantSetState,
CantShutdown, CantCreateHandlers, CantCreateStaticHandlers,
CantHealthCheck, CantConnected, CantDisconnected, CantVersion,
CantAppRequest, CantAppResponse, CantAppGossip, CantAppRequestFailed bool
InitializeF func(*snow.Context, manager.Manager, []byte, []byte, []byte, chan<- Message, []*Fx, AppSender) error
SetStateF func(snow.State) error
ShutdownF func() error
CreateHandlersF func() (map[string]*HTTPHandler, error)
CreateStaticHandlersF func() (map[string]*HTTPHandler, error)
ConnectedF func(nodeID ids.NodeID, nodeVersion *version.Application) error
DisconnectedF func(nodeID ids.NodeID) error
HealthCheckF func() (interface{}, error)
AppRequestF func(nodeID ids.NodeID, requestID uint32, deadline time.Time, msg []byte) error
AppResponseF func(nodeID ids.NodeID, requestID uint32, msg []byte) error
AppGossipF func(nodeID ids.NodeID, msg []byte) error
AppRequestFailedF func(nodeID ids.NodeID, requestID uint32) error
VersionF func() (string, error)
}
func (vm *TestVM) Default(cant bool) {
vm.CantInitialize = cant
vm.CantSetState = cant
vm.CantShutdown = cant
vm.CantCreateHandlers = cant
vm.CantCreateStaticHandlers = cant
vm.CantHealthCheck = cant
vm.CantAppRequest = cant
vm.CantAppRequestFailed = cant
vm.CantAppResponse = cant
vm.CantAppGossip = cant
vm.CantVersion = cant
vm.CantConnected = cant
vm.CantDisconnected = cant
}
func (vm *TestVM) Initialize(ctx *snow.Context, db manager.Manager, genesisBytes, upgradeBytes, configBytes []byte, msgChan chan<- Message, fxs []*Fx, appSender AppSender) error {
if vm.InitializeF != nil {
return vm.InitializeF(ctx, db, genesisBytes, upgradeBytes, configBytes, msgChan, fxs, appSender)
}
if vm.CantInitialize && vm.T != nil {
vm.T.Fatal(errInitialize)
}
return errInitialize
}
func (vm *TestVM) SetState(state snow.State) error {
if vm.SetStateF != nil {
return vm.SetStateF(state)
}
if vm.CantSetState {
if vm.T != nil {
vm.T.Fatal(errSetState)
}
return errSetState
}
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) AppRequestFailed(nodeID ids.NodeID, requestID uint32) error {
if vm.AppRequestFailedF != nil {
return vm.AppRequestFailedF(nodeID, requestID)
}
if !vm.CantAppRequestFailed {
return nil
}
if vm.T != nil {
vm.T.Fatal(errAppRequest)
}
return errAppRequest
}
func (vm *TestVM) AppRequest(nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error {
if vm.AppRequestF != nil {
return vm.AppRequestF(nodeID, requestID, deadline, request)
}
if !vm.CantAppRequest {
return nil
}
if vm.T != nil {
vm.T.Fatal(errAppRequest)
}
return errAppRequest
}
func (vm *TestVM) AppResponse(nodeID ids.NodeID, requestID uint32, response []byte) error {
if vm.AppResponseF != nil {
return vm.AppResponseF(nodeID, requestID, response)
}
if !vm.CantAppResponse {
return nil
}
if vm.T != nil {
vm.T.Fatal(errAppResponse)
}
return errAppResponse
}
func (vm *TestVM) AppGossip(nodeID ids.NodeID, msg []byte) error {
if vm.AppGossipF != nil {
return vm.AppGossipF(nodeID, msg)
}
if !vm.CantAppGossip {
return nil
}
if vm.T != nil {
vm.T.Fatal(errAppGossip)
}
return errAppGossip
}
func (vm *TestVM) Connected(id ids.NodeID, nodeVersion *version.Application) error {
if vm.ConnectedF != nil {
return vm.ConnectedF(id, nodeVersion)
}
if vm.CantConnected && vm.T != nil {
vm.T.Fatal(errConnected)
}
return nil
}
func (vm *TestVM) Disconnected(id ids.NodeID) 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
}