-
Notifications
You must be signed in to change notification settings - Fork 641
/
test_bootstrapable.go
56 lines (44 loc) · 1.3 KB
/
test_bootstrapable.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
)
var (
_ Bootstrapable = (*BootstrapableTest)(nil)
errForceAccepted = errors.New("unexpectedly called ForceAccepted")
errClear = errors.New("unexpectedly called Clear")
)
// BootstrapableTest is a test engine that supports bootstrapping
type BootstrapableTest struct {
T *testing.T
CantForceAccepted, CantClear bool
ClearF func(ctx context.Context) error
ForceAcceptedF func(ctx context.Context, acceptedContainerIDs []ids.ID) error
}
// Default sets the default on call handling
func (b *BootstrapableTest) Default(cant bool) {
b.CantForceAccepted = cant
}
func (b *BootstrapableTest) Clear(ctx context.Context) error {
if b.ClearF != nil {
return b.ClearF(ctx)
}
if b.CantClear && b.T != nil {
require.FailNow(b.T, errClear.Error())
}
return errClear
}
func (b *BootstrapableTest) ForceAccepted(ctx context.Context, containerIDs []ids.ID) error {
if b.ForceAcceptedF != nil {
return b.ForceAcceptedF(ctx, containerIDs)
}
if b.CantForceAccepted && b.T != nil {
require.FailNow(b.T, errForceAccepted.Error())
}
return errForceAccepted
}