-
Notifications
You must be signed in to change notification settings - Fork 662
/
test_storage.go
66 lines (55 loc) · 1.72 KB
/
test_storage.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vertex
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
)
var (
errGet = errors.New("unexpectedly called Get")
errEdge = errors.New("unexpectedly called Edge")
errStopVertexAccepted = errors.New("unexpectedly called StopVertexAccepted")
_ Storage = (*TestStorage)(nil)
)
type TestStorage struct {
T *testing.T
CantGetVtx, CantEdge, CantStopVertexAccepted bool
GetVtxF func(context.Context, ids.ID) (avalanche.Vertex, error)
EdgeF func(context.Context) []ids.ID
StopVertexAcceptedF func(context.Context) (bool, error)
}
func (s *TestStorage) Default(cant bool) {
s.CantGetVtx = cant
s.CantEdge = cant
}
func (s *TestStorage) GetVtx(ctx context.Context, vtxID ids.ID) (avalanche.Vertex, error) {
if s.GetVtxF != nil {
return s.GetVtxF(ctx, vtxID)
}
if s.CantGetVtx && s.T != nil {
require.FailNow(s.T, errGet.Error())
}
return nil, errGet
}
func (s *TestStorage) Edge(ctx context.Context) []ids.ID {
if s.EdgeF != nil {
return s.EdgeF(ctx)
}
if s.CantEdge && s.T != nil {
require.FailNow(s.T, errEdge.Error())
}
return nil
}
func (s *TestStorage) StopVertexAccepted(ctx context.Context) (bool, error) {
if s.StopVertexAcceptedF != nil {
return s.StopVertexAcceptedF(ctx)
}
if s.CantStopVertexAccepted && s.T != nil {
require.FailNow(s.T, errStopVertexAccepted.Error())
}
return false, nil
}