-
Notifications
You must be signed in to change notification settings - Fork 669
/
test_vm.go
110 lines (96 loc) · 2.74 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
// (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"
"github.com/ava-labs/avalanchego/snow"
)
var (
errInitialize = errors.New("unexpectedly called Initialize")
)
// TestVM is a test vm
type TestVM struct {
T *testing.T
CantInitialize, CantBootstrapping, CantBootstrapped, CantShutdown, CantCreateHandlers, CantCreateStaticHandlers bool
InitializeF func(*snow.Context, database.Database, []byte, chan<- Message, []*Fx) error
BootstrappingF, BootstrappedF, ShutdownF func() error
CreateHandlersF func() map[string]*HTTPHandler
CreateStaticHandlersF func() map[string]*HTTPHandler
}
// Default ...
func (vm *TestVM) Default(cant bool) {
vm.CantInitialize = cant
vm.CantBootstrapping = cant
vm.CantBootstrapped = cant
vm.CantShutdown = cant
vm.CantCreateHandlers = cant
vm.CantCreateStaticHandlers = cant
}
// Initialize ...
func (vm *TestVM) Initialize(ctx *snow.Context, db database.Database, initState []byte, msgChan chan<- Message, fxs []*Fx) error {
if vm.InitializeF != nil {
return vm.InitializeF(ctx, db, initState, msgChan, fxs)
}
if vm.CantInitialize && vm.T != nil {
vm.T.Fatal(errInitialize)
}
return errInitialize
}
// Bootstrapping ...
func (vm *TestVM) Bootstrapping() error {
if vm.BootstrappingF != nil {
return vm.BootstrappingF()
} else if vm.CantBootstrapping {
if vm.T != nil {
vm.T.Fatalf("Unexpectedly called Bootstrapping")
}
return errors.New("unexpectedly called Bootstrapping")
}
return nil
}
// Bootstrapped ...
func (vm *TestVM) Bootstrapped() error {
if vm.BootstrappedF != nil {
return vm.BootstrappedF()
} else if vm.CantBootstrapped {
if vm.T != nil {
vm.T.Fatalf("Unexpectedly called Bootstrapped")
}
return errors.New("unexpectedly called Bootstrapped")
}
return nil
}
// Shutdown ...
func (vm *TestVM) Shutdown() error {
if vm.ShutdownF != nil {
return vm.ShutdownF()
} else if vm.CantShutdown {
if vm.T != nil {
vm.T.Fatalf("Unexpectedly called Shutdown")
}
return errors.New("unexpectedly called Shutdown")
}
return nil
}
// CreateHandlers ...
func (vm *TestVM) CreateHandlers() map[string]*HTTPHandler {
if vm.CreateHandlersF != nil {
return vm.CreateHandlersF()
}
if vm.CantCreateHandlers && vm.T != nil {
vm.T.Fatalf("Unexpectedly called CreateHandlers")
}
return nil
}
// CreateStaticHandlers ...
func (vm *TestVM) CreateStaticHandlers() map[string]*HTTPHandler {
if vm.CreateStaticHandlersF != nil {
return vm.CreateStaticHandlersF()
}
if vm.CantCreateStaticHandlers && vm.T != nil {
vm.T.Fatalf("Unexpectedly called CreateStaticHandlers")
}
return nil
}