-
Notifications
You must be signed in to change notification settings - Fork 659
/
test_job.go
95 lines (81 loc) · 2.19 KB
/
test_job.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package queue
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
)
var (
errExecute = errors.New("unexpectedly called Execute")
errHasMissingDependencies = errors.New("unexpectedly called HasMissingDependencies")
)
// TestJob is a test Job
type TestJob struct {
T *testing.T
CantID,
CantMissingDependencies,
CantExecute,
CantBytes,
CantHasMissingDependencies bool
IDF func() ids.ID
MissingDependenciesF func(context.Context) (set.Set[ids.ID], error)
ExecuteF func(context.Context) error
BytesF func() []byte
HasMissingDependenciesF func(context.Context) (bool, error)
}
func (j *TestJob) Default(cant bool) {
j.CantID = cant
j.CantMissingDependencies = cant
j.CantExecute = cant
j.CantBytes = cant
j.CantHasMissingDependencies = cant
}
func (j *TestJob) ID() ids.ID {
if j.IDF != nil {
return j.IDF()
}
if j.CantID && j.T != nil {
require.FailNow(j.T, "Unexpectedly called ID")
}
return ids.ID{}
}
func (j *TestJob) MissingDependencies(ctx context.Context) (set.Set[ids.ID], error) {
if j.MissingDependenciesF != nil {
return j.MissingDependenciesF(ctx)
}
if j.CantMissingDependencies && j.T != nil {
require.FailNow(j.T, "Unexpectedly called MissingDependencies")
}
return set.Set[ids.ID]{}, nil
}
func (j *TestJob) Execute(ctx context.Context) error {
if j.ExecuteF != nil {
return j.ExecuteF(ctx)
}
if j.CantExecute && j.T != nil {
require.FailNow(j.T, errExecute.Error())
}
return errExecute
}
func (j *TestJob) Bytes() []byte {
if j.BytesF != nil {
return j.BytesF()
}
if j.CantBytes && j.T != nil {
require.FailNow(j.T, "Unexpectedly called Bytes")
}
return nil
}
func (j *TestJob) HasMissingDependencies(ctx context.Context) (bool, error) {
if j.HasMissingDependenciesF != nil {
return j.HasMissingDependenciesF(ctx)
}
if j.CantHasMissingDependencies && j.T != nil {
require.FailNow(j.T, errHasMissingDependencies.Error())
}
return false, errHasMissingDependencies
}