-
Notifications
You must be signed in to change notification settings - Fork 178
/
unittest.go
258 lines (212 loc) · 6.69 KB
/
unittest.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package unittest
import (
"context"
"fmt"
"sync"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/stretchr/testify/mock"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/blobs"
"github.com/onflow/flow-go/module/executiondatasync/execution_data"
"github.com/onflow/flow-go/network/mocknetwork"
statemock "github.com/onflow/flow-go/state/protocol/mock"
"github.com/onflow/flow-go/storage"
storagemock "github.com/onflow/flow-go/storage/mock"
)
func ExecutionDataFixture(blockID flow.Identifier) *execution_data.BlockExecutionData {
return &execution_data.BlockExecutionData{
BlockID: blockID,
ChunkExecutionDatas: []*execution_data.ChunkExecutionData{},
}
}
func MockBlobService(bs blockstore.Blockstore) *mocknetwork.BlobService {
bex := new(mocknetwork.BlobService)
bex.On("GetBlobs", mock.Anything, mock.AnythingOfType("[]cid.Cid")).
Return(func(ctx context.Context, cids []cid.Cid) <-chan blobs.Blob {
ch := make(chan blobs.Blob)
var wg sync.WaitGroup
wg.Add(len(cids))
for _, c := range cids {
c := c
go func() {
defer wg.Done()
blob, err := bs.Get(ctx, c)
if err != nil {
// In the real implementation, Bitswap would keep trying to get the blob from
// the network indefinitely, sending requests to more and more peers until it
// eventually finds the blob, or the context is canceled. Here, we know that
// if the blob is not already in the blobstore, then we will never appear, so
// we just wait for the context to be canceled.
<-ctx.Done()
return
}
ch <- blob
}()
}
go func() {
wg.Wait()
close(ch)
}()
return ch
}).Maybe()
bex.On("AddBlobs", mock.Anything, mock.AnythingOfType("[]blocks.Block")).Return(bs.PutMany).Maybe()
bex.On("DeleteBlob", mock.Anything, mock.AnythingOfType("cid.Cid")).Return(bs.DeleteBlock).Maybe()
noop := module.NoopReadyDoneAware{}
bex.On("Ready").Return(func() <-chan struct{} { return noop.Ready() }).Maybe()
return bex
}
type SnapshotMockOptions func(*statemock.Snapshot)
func WithHead(head *flow.Header) SnapshotMockOptions {
return func(snapshot *statemock.Snapshot) {
snapshot.On("Head").Return(head, nil)
}
}
func WithSeal(seal *flow.Seal) SnapshotMockOptions {
return func(snapshot *statemock.Snapshot) {
snapshot.On("Seal").Return(seal, nil)
}
}
func MockProtocolStateSnapshot(opts ...SnapshotMockOptions) *statemock.Snapshot {
snapshot := new(statemock.Snapshot)
for _, opt := range opts {
opt(snapshot)
}
return snapshot
}
type StateMockOptions func(*statemock.State)
func WithSealedSnapshot(snapshot *statemock.Snapshot) StateMockOptions {
return func(state *statemock.State) {
state.On("Sealed").Return(snapshot)
}
}
func MockProtocolState(opts ...StateMockOptions) *statemock.State {
state := new(statemock.State)
for _, opt := range opts {
opt(state)
}
return state
}
type BlockHeaderMockOptions func(*storagemock.Headers)
func WithByHeight(blocksByHeight map[uint64]*flow.Block) BlockHeaderMockOptions {
return func(blocks *storagemock.Headers) {
blocks.On("ByHeight", mock.AnythingOfType("uint64")).Return(
func(height uint64) *flow.Header {
if _, has := blocksByHeight[height]; !has {
return nil
}
return blocksByHeight[height].Header
},
func(height uint64) error {
if _, has := blocksByHeight[height]; !has {
return fmt.Errorf("block %d not found: %w", height, storage.ErrNotFound)
}
return nil
},
)
}
}
func WithByID(blocksByID map[flow.Identifier]*flow.Block) BlockHeaderMockOptions {
return func(blocks *storagemock.Headers) {
blocks.On("ByBlockID", mock.AnythingOfType("flow.Identifier")).Return(
func(blockID flow.Identifier) *flow.Header {
if _, has := blocksByID[blockID]; !has {
return nil
}
return blocksByID[blockID].Header
},
func(blockID flow.Identifier) error {
if _, has := blocksByID[blockID]; !has {
return fmt.Errorf("block %s not found: %w", blockID, storage.ErrNotFound)
}
return nil
},
)
}
}
func MockBlockHeaderStorage(opts ...BlockHeaderMockOptions) *storagemock.Headers {
headers := new(storagemock.Headers)
for _, opt := range opts {
opt(headers)
}
return headers
}
type ResultsMockOptions func(*storagemock.ExecutionResults)
func WithByBlockID(resultsByID map[flow.Identifier]*flow.ExecutionResult) ResultsMockOptions {
return func(results *storagemock.ExecutionResults) {
results.On("ByBlockID", mock.AnythingOfType("flow.Identifier")).Return(
func(blockID flow.Identifier) *flow.ExecutionResult {
if _, has := resultsByID[blockID]; !has {
return nil
}
return resultsByID[blockID]
},
func(blockID flow.Identifier) error {
if _, has := resultsByID[blockID]; !has {
return fmt.Errorf("result %s not found: %w", blockID, storage.ErrNotFound)
}
return nil
},
)
}
}
func WithResultByID(resultsByID map[flow.Identifier]*flow.ExecutionResult) ResultsMockOptions {
return func(results *storagemock.ExecutionResults) {
results.On("ByID", mock.AnythingOfType("flow.Identifier")).Return(
func(resultID flow.Identifier) *flow.ExecutionResult {
if _, has := resultsByID[resultID]; !has {
return nil
}
return resultsByID[resultID]
},
func(resultID flow.Identifier) error {
if _, has := resultsByID[resultID]; !has {
return fmt.Errorf("result %s not found: %w", resultID, storage.ErrNotFound)
}
return nil
},
)
}
}
func MockResultsStorage(opts ...ResultsMockOptions) *storagemock.ExecutionResults {
results := new(storagemock.ExecutionResults)
for _, opt := range opts {
opt(results)
}
return results
}
type SealsMockOptions func(*storagemock.Seals)
func WithSealsByBlockID(sealsByBlockID map[flow.Identifier]*flow.Seal) SealsMockOptions {
return func(seals *storagemock.Seals) {
seals.On("FinalizedSealForBlock", mock.AnythingOfType("flow.Identifier")).Return(
func(blockID flow.Identifier) *flow.Seal {
if _, has := sealsByBlockID[blockID]; !has {
return nil
}
return sealsByBlockID[blockID]
},
func(blockID flow.Identifier) error {
if _, has := sealsByBlockID[blockID]; !has {
return fmt.Errorf("seal for block %s not found: %w", blockID, storage.ErrNotFound)
}
return nil
},
)
}
}
func MockSealsStorage(opts ...SealsMockOptions) *storagemock.Seals {
seals := new(storagemock.Seals)
for _, opt := range opts {
opt(seals)
}
return seals
}
func RemoveExpectedCall(method string, expectedCalls []*mock.Call) []*mock.Call {
for i, call := range expectedCalls {
if call.Method == method {
expectedCalls = append(expectedCalls[:i], expectedCalls[i+1:]...)
}
}
return expectedCalls
}