forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sender.go
142 lines (126 loc) · 5 KB
/
test_sender.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"testing"
"github.com/ava-labs/gecko/ids"
)
// SenderTest is a test sender
type SenderTest struct {
T *testing.T
CantGetAcceptedFrontier, CantAcceptedFrontier,
CantGetAccepted, CantAccepted,
CantGet, CantPut,
CantPullQuery, CantPushQuery, CantChits bool
GetAcceptedFrontierF func(ids.ShortSet, uint32)
AcceptedFrontierF func(ids.ShortID, uint32, ids.Set)
GetAcceptedF func(ids.ShortSet, uint32, ids.Set)
AcceptedF func(ids.ShortID, uint32, ids.Set)
GetF func(ids.ShortID, uint32, ids.ID)
PutF func(ids.ShortID, uint32, ids.ID, []byte)
PushQueryF func(ids.ShortSet, uint32, ids.ID, []byte)
PullQueryF func(ids.ShortSet, uint32, ids.ID)
ChitsF func(ids.ShortID, uint32, ids.Set)
}
// Default set the default callable value to [cant]
func (s *SenderTest) Default(cant bool) {
s.CantGetAcceptedFrontier = cant
s.CantAcceptedFrontier = cant
s.CantGetAccepted = cant
s.CantAccepted = cant
s.CantGet = cant
s.CantPut = cant
s.CantPullQuery = cant
s.CantPushQuery = cant
s.CantChits = cant
}
// GetAcceptedFrontier calls GetAcceptedFrontierF if it was initialized. If it
// wasn't initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) GetAcceptedFrontier(validatorIDs ids.ShortSet, requestID uint32) {
if s.GetAcceptedFrontierF != nil {
s.GetAcceptedFrontierF(validatorIDs, requestID)
} else if s.CantGetAcceptedFrontier && s.T != nil {
s.T.Fatalf("Unexpectedly called GetAcceptedFrontier")
}
}
// AcceptedFrontier calls AcceptedFrontierF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) AcceptedFrontier(validatorID ids.ShortID, requestID uint32, containerIDs ids.Set) {
if s.AcceptedFrontierF != nil {
s.AcceptedFrontierF(validatorID, requestID, containerIDs)
} else if s.CantAcceptedFrontier && s.T != nil {
s.T.Fatalf("Unexpectedly called AcceptedFrontier")
}
}
// GetAccepted calls GetAcceptedF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) GetAccepted(validatorIDs ids.ShortSet, requestID uint32, containerIDs ids.Set) {
if s.GetAcceptedF != nil {
s.GetAcceptedF(validatorIDs, requestID, containerIDs)
} else if s.CantGetAccepted && s.T != nil {
s.T.Fatalf("Unexpectedly called GetAccepted")
}
}
// Accepted calls AcceptedF if it was initialized. If it wasn't initialized and
// this function shouldn't be called and testing was initialized, then testing
// will fail.
func (s *SenderTest) Accepted(validatorID ids.ShortID, requestID uint32, containerIDs ids.Set) {
if s.AcceptedF != nil {
s.AcceptedF(validatorID, requestID, containerIDs)
} else if s.CantAccepted && s.T != nil {
s.T.Fatalf("Unexpectedly called Accepted")
}
}
// Get calls GetF if it was initialized. If it wasn't initialized and this
// function shouldn't be called and testing was initialized, then testing will
// fail.
func (s *SenderTest) Get(vdr ids.ShortID, requestID uint32, vtxID ids.ID) {
if s.GetF != nil {
s.GetF(vdr, requestID, vtxID)
} else if s.CantGet && s.T != nil {
s.T.Fatalf("Unexpectedly called Get")
}
}
// Put calls PutF if it was initialized. If it wasn't initialized and this
// function shouldn't be called and testing was initialized, then testing will
// fail.
func (s *SenderTest) Put(vdr ids.ShortID, requestID uint32, vtxID ids.ID, vtx []byte) {
if s.PutF != nil {
s.PutF(vdr, requestID, vtxID, vtx)
} else if s.CantPut && s.T != nil {
s.T.Fatalf("Unexpectedly called Put")
}
}
// PushQuery calls PushQueryF if it was initialized. If it wasn't initialized
// and this function shouldn't be called and testing was initialized, then
// testing will fail.
func (s *SenderTest) PushQuery(vdrs ids.ShortSet, requestID uint32, vtxID ids.ID, vtx []byte) {
if s.PushQueryF != nil {
s.PushQueryF(vdrs, requestID, vtxID, vtx)
} else if s.CantPushQuery && s.T != nil {
s.T.Fatalf("Unexpectedly called PushQuery")
}
}
// PullQuery calls PullQueryF if it was initialized. If it wasn't initialized
// and this function shouldn't be called and testing was initialized, then
// testing will fail.
func (s *SenderTest) PullQuery(vdrs ids.ShortSet, requestID uint32, vtxID ids.ID) {
if s.PullQueryF != nil {
s.PullQueryF(vdrs, requestID, vtxID)
} else if s.CantPullQuery && s.T != nil {
s.T.Fatalf("Unexpectedly called PullQuery")
}
}
// Chits calls ChitsF if it was initialized. If it wasn't initialized and this
// function shouldn't be called and testing was initialized, then testing will
// fail.
func (s *SenderTest) Chits(vdr ids.ShortID, requestID uint32, votes ids.Set) {
if s.ChitsF != nil {
s.ChitsF(vdr, requestID, votes)
} else if s.CantChits && s.T != nil {
s.T.Fatalf("Unexpectedly called Chits")
}
}