forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_vm.go
77 lines (65 loc) · 1.84 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
// (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/gecko/database"
"github.com/ava-labs/gecko/snow"
)
var (
errInitialize = errors.New("unexpectedly called Initialize")
)
// VMTest is a test vm
type VMTest struct {
T *testing.T
CantInitialize, CantShutdown, CantCreateHandlers, CantCreateStaticHandlers bool
InitializeF func(*snow.Context, database.Database, []byte, chan<- Message, []*Fx) error
ShutdownF func()
CreateHandlersF func() map[string]*HTTPHandler
CreateStaticHandlersF func() map[string]*HTTPHandler
}
// Default ...
func (vm *VMTest) Default(cant bool) {
vm.CantInitialize = cant
vm.CantShutdown = cant
vm.CantCreateHandlers = cant
}
// Initialize ...
func (vm *VMTest) 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
}
// Shutdown ...
func (vm *VMTest) Shutdown() {
if vm.ShutdownF != nil {
vm.ShutdownF()
} else if vm.CantShutdown && vm.T != nil {
vm.T.Fatalf("Unexpectedly called Shutdown")
}
}
// CreateHandlers ...
func (vm *VMTest) 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 *VMTest) 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
}