-
Notifications
You must be signed in to change notification settings - Fork 670
/
test_vm.go
294 lines (268 loc) · 9.19 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/database/manager"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/version"
)
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")
errCrossChainAppRequest = errors.New("unexpectedly called CrossChainAppRequest")
errCrossChainAppResponse = errors.New("unexpectedly called CrossChainAppResponse")
errCrossChainAppRequestFailed = errors.New("unexpectedly called CrossChainAppRequestFailed")
_ VM = (*TestVM)(nil)
)
// TestVM is a test vm
type TestVM struct {
T *testing.T
CantInitialize, CantSetState,
CantShutdown, CantCreateHandlers, CantCreateStaticHandlers,
CantHealthCheck, CantConnected, CantDisconnected, CantVersion,
CantAppRequest, CantAppResponse, CantAppGossip, CantAppRequestFailed,
CantCrossChainAppRequest, CantCrossChainAppResponse, CantCrossChainAppRequestFailed bool
InitializeF func(ctx context.Context, chainCtx *snow.Context, db manager.Manager, genesisBytes []byte, upgradeBytes []byte, configBytes []byte, msgChan chan<- Message, fxs []*Fx, appSender AppSender) error
SetStateF func(ctx context.Context, state snow.State) error
ShutdownF func(context.Context) error
CreateHandlersF func(context.Context) (map[string]http.Handler, error)
CreateStaticHandlersF func(context.Context) (map[string]http.Handler, error)
ConnectedF func(ctx context.Context, nodeID ids.NodeID, nodeVersion *version.Application) error
DisconnectedF func(ctx context.Context, nodeID ids.NodeID) error
HealthCheckF func(context.Context) (interface{}, error)
AppRequestF func(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, msg []byte) error
AppResponseF func(ctx context.Context, nodeID ids.NodeID, requestID uint32, msg []byte) error
AppGossipF func(ctx context.Context, nodeID ids.NodeID, msg []byte) error
AppRequestFailedF func(ctx context.Context, nodeID ids.NodeID, requestID uint32) error
VersionF func(context.Context) (string, error)
CrossChainAppRequestF func(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, msg []byte) error
CrossChainAppResponseF func(ctx context.Context, chainID ids.ID, requestID uint32, msg []byte) error
CrossChainAppRequestFailedF func(ctx context.Context, chainID ids.ID, requestID uint32) 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
vm.CantCrossChainAppRequest = cant
vm.CantCrossChainAppRequestFailed = cant
vm.CantCrossChainAppResponse = cant
}
func (vm *TestVM) Initialize(
ctx context.Context,
chainCtx *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,
chainCtx,
db,
genesisBytes,
upgradeBytes,
configBytes,
msgChan,
fxs,
appSender,
)
}
if vm.CantInitialize && vm.T != nil {
require.FailNow(vm.T, errInitialize.Error())
}
return errInitialize
}
func (vm *TestVM) SetState(ctx context.Context, state snow.State) error {
if vm.SetStateF != nil {
return vm.SetStateF(ctx, state)
}
if vm.CantSetState {
if vm.T != nil {
require.FailNow(vm.T, errSetState.Error())
}
return errSetState
}
return nil
}
func (vm *TestVM) Shutdown(ctx context.Context) error {
if vm.ShutdownF != nil {
return vm.ShutdownF(ctx)
}
if vm.CantShutdown {
if vm.T != nil {
require.FailNow(vm.T, errShutdown.Error())
}
return errShutdown
}
return nil
}
func (vm *TestVM) CreateHandlers(ctx context.Context) (map[string]http.Handler, error) {
if vm.CreateHandlersF != nil {
return vm.CreateHandlersF(ctx)
}
if vm.CantCreateHandlers && vm.T != nil {
require.FailNow(vm.T, errCreateHandlers.Error())
}
return nil, nil
}
func (vm *TestVM) CreateStaticHandlers(ctx context.Context) (map[string]http.Handler, error) {
if vm.CreateStaticHandlersF != nil {
return vm.CreateStaticHandlersF(ctx)
}
if vm.CantCreateStaticHandlers && vm.T != nil {
require.FailNow(vm.T, errCreateStaticHandlers.Error())
}
return nil, nil
}
func (vm *TestVM) HealthCheck(ctx context.Context) (interface{}, error) {
if vm.HealthCheckF != nil {
return vm.HealthCheckF(ctx)
}
if vm.CantHealthCheck && vm.T != nil {
require.FailNow(vm.T, errHealthCheck.Error())
}
return nil, errHealthCheck
}
func (vm *TestVM) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID uint32, deadline time.Time, request []byte) error {
if vm.AppRequestF != nil {
return vm.AppRequestF(ctx, nodeID, requestID, deadline, request)
}
if !vm.CantAppRequest {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errAppRequest.Error())
}
return errAppRequest
}
func (vm *TestVM) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
if vm.AppRequestFailedF != nil {
return vm.AppRequestFailedF(ctx, nodeID, requestID)
}
if !vm.CantAppRequestFailed {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errAppRequestFailed.Error())
}
return errAppRequestFailed
}
func (vm *TestVM) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, response []byte) error {
if vm.AppResponseF != nil {
return vm.AppResponseF(ctx, nodeID, requestID, response)
}
if !vm.CantAppResponse {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errAppResponse.Error())
}
return errAppResponse
}
func (vm *TestVM) AppGossip(ctx context.Context, nodeID ids.NodeID, msg []byte) error {
if vm.AppGossipF != nil {
return vm.AppGossipF(ctx, nodeID, msg)
}
if !vm.CantAppGossip {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errAppGossip.Error())
}
return errAppGossip
}
func (vm *TestVM) CrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, deadline time.Time, request []byte) error {
if vm.CrossChainAppRequestF != nil {
return vm.CrossChainAppRequestF(ctx, chainID, requestID, deadline, request)
}
if !vm.CantCrossChainAppRequest {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errCrossChainAppRequest.Error())
}
return errCrossChainAppRequest
}
func (vm *TestVM) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID, requestID uint32) error {
if vm.CrossChainAppRequestFailedF != nil {
return vm.CrossChainAppRequestFailedF(ctx, chainID, requestID)
}
if !vm.CantCrossChainAppRequestFailed {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errCrossChainAppRequestFailed.Error())
}
return errCrossChainAppRequestFailed
}
func (vm *TestVM) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, response []byte) error {
if vm.CrossChainAppResponseF != nil {
return vm.CrossChainAppResponseF(ctx, chainID, requestID, response)
}
if !vm.CantCrossChainAppResponse {
return nil
}
if vm.T != nil {
require.FailNow(vm.T, errCrossChainAppResponse.Error())
}
return errCrossChainAppResponse
}
func (vm *TestVM) Connected(ctx context.Context, id ids.NodeID, nodeVersion *version.Application) error {
if vm.ConnectedF != nil {
return vm.ConnectedF(ctx, id, nodeVersion)
}
if vm.CantConnected && vm.T != nil {
require.FailNow(vm.T, errConnected.Error())
}
return nil
}
func (vm *TestVM) Disconnected(ctx context.Context, id ids.NodeID) error {
if vm.DisconnectedF != nil {
return vm.DisconnectedF(ctx, id)
}
if vm.CantDisconnected && vm.T != nil {
require.FailNow(vm.T, errDisconnected.Error())
}
return nil
}
func (vm *TestVM) Version(ctx context.Context) (string, error) {
if vm.VersionF != nil {
return vm.VersionF(ctx)
}
if vm.CantVersion && vm.T != nil {
require.FailNow(vm.T, errVersion.Error())
}
return "", nil
}