-
Notifications
You must be signed in to change notification settings - Fork 641
/
test_vm.go
128 lines (112 loc) · 3.32 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
// (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/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")
_ VM = &TestVM{}
)
// TestVM is a test vm
type TestVM struct {
T *testing.T
CantInitialize, CantBootstrapping, CantBootstrapped,
CantShutdown, CantCreateHandlers, CantCreateStaticHandlers,
CantHealthCheck 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)
HealthCheckF func() (interface{}, 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
}