/
test_piecestore.go
159 lines (132 loc) · 5.25 KB
/
test_piecestore.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
package shared_testutil
import (
"context"
"errors"
"testing"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-fil-markets/piecestore"
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/filecoin-project/go-fil-markets/shared"
)
// TestPieceStore is piecestore who's query results are mocked
type TestPieceStore struct {
addPieceBlockLocationsError error
addDealForPieceError error
getPieceInfoError error
piecesStubbed map[cid.Cid]piecestore.PieceInfo
piecesExpected map[cid.Cid]struct{}
piecesReceived map[cid.Cid]struct{}
cidInfosStubbed map[cid.Cid]piecestore.CIDInfo
cidInfosExpected map[cid.Cid]struct{}
cidInfosReceived map[cid.Cid]struct{}
}
// TestPieceStoreParams sets parameters for a piece store
type TestPieceStoreParams struct {
AddDealForPieceError error
AddPieceBlockLocationsError error
GetPieceInfoError error
}
var _ piecestore.PieceStore = &TestPieceStore{}
// NewTestPieceStore creates a TestPieceStore
func NewTestPieceStore() *TestPieceStore {
return NewTestPieceStoreWithParams(TestPieceStoreParams{})
}
// NewTestPieceStoreWithParams creates a TestPieceStore with the given parameters
func NewTestPieceStoreWithParams(params TestPieceStoreParams) *TestPieceStore {
return &TestPieceStore{
addDealForPieceError: params.AddDealForPieceError,
addPieceBlockLocationsError: params.AddPieceBlockLocationsError,
getPieceInfoError: params.GetPieceInfoError,
piecesStubbed: make(map[cid.Cid]piecestore.PieceInfo),
piecesExpected: make(map[cid.Cid]struct{}),
piecesReceived: make(map[cid.Cid]struct{}),
cidInfosStubbed: make(map[cid.Cid]piecestore.CIDInfo),
cidInfosExpected: make(map[cid.Cid]struct{}),
cidInfosReceived: make(map[cid.Cid]struct{}),
}
}
// StubPiece creates a return value for the given piece cid without expecting it
// to be called
func (tps *TestPieceStore) StubPiece(pieceCid cid.Cid, pieceInfo piecestore.PieceInfo) {
tps.piecesStubbed[pieceCid] = pieceInfo
}
// ExpectPiece records a piece being expected to be queried and return the given piece info
func (tps *TestPieceStore) ExpectPiece(pieceCid cid.Cid, pieceInfo piecestore.PieceInfo) {
tps.piecesExpected[pieceCid] = struct{}{}
tps.StubPiece(pieceCid, pieceInfo)
}
// ExpectMissingPiece records a piece being expected to be queried and should fail
func (tps *TestPieceStore) ExpectMissingPiece(pieceCid cid.Cid) {
tps.piecesExpected[pieceCid] = struct{}{}
}
// StubCID creates a return value for the given CID without expecting it
// to be called
func (tps *TestPieceStore) StubCID(c cid.Cid, cidInfo piecestore.CIDInfo) {
tps.cidInfosStubbed[c] = cidInfo
}
// ExpectCID records a CID being expected to be queried and return the given CID info
func (tps *TestPieceStore) ExpectCID(c cid.Cid, cidInfo piecestore.CIDInfo) {
tps.cidInfosExpected[c] = struct{}{}
tps.StubCID(c, cidInfo)
}
// ExpectMissingCID records a CID being expected to be queried and should fail
func (tps *TestPieceStore) ExpectMissingCID(c cid.Cid) {
tps.cidInfosExpected[c] = struct{}{}
}
// VerifyExpectations verifies that the piecestore was queried in the expected ways
func (tps *TestPieceStore) VerifyExpectations(t *testing.T) {
require.Equal(t, tps.piecesExpected, tps.piecesReceived)
require.Equal(t, tps.cidInfosExpected, tps.cidInfosReceived)
}
// AddDealForPiece returns a preprogrammed error
func (tps *TestPieceStore) AddDealForPiece(pieceCID cid.Cid, _ cid.Cid, dealInfo piecestore.DealInfo) error {
return tps.addDealForPieceError
}
// AddPieceBlockLocations returns a preprogrammed error
func (tps *TestPieceStore) AddPieceBlockLocations(pieceCID cid.Cid, blockLocations map[cid.Cid]piecestore.BlockLocation) error {
return tps.addPieceBlockLocationsError
}
func (tps *TestPieceStore) ReturnErrorFromGetPieceInfo(err error) {
tps.getPieceInfoError = err
}
// GetPieceInfo returns a piece info if it's been stubbed
func (tps *TestPieceStore) GetPieceInfo(pieceCID cid.Cid) (piecestore.PieceInfo, error) {
if tps.getPieceInfoError != nil {
return piecestore.PieceInfoUndefined, tps.getPieceInfoError
}
tps.piecesReceived[pieceCID] = struct{}{}
pio, ok := tps.piecesStubbed[pieceCID]
if ok {
return pio, nil
}
_, ok = tps.piecesExpected[pieceCID]
if ok {
return piecestore.PieceInfoUndefined, retrievalmarket.ErrNotFound
}
return piecestore.PieceInfoUndefined, errors.New("GetPieceInfo failed")
}
// GetCIDInfo returns cid info if it's been stubbed
func (tps *TestPieceStore) GetCIDInfo(c cid.Cid) (piecestore.CIDInfo, error) {
tps.cidInfosReceived[c] = struct{}{}
cio, ok := tps.cidInfosStubbed[c]
if ok {
return cio, nil
}
_, ok = tps.cidInfosExpected[c]
if ok {
return piecestore.CIDInfoUndefined, retrievalmarket.ErrNotFound
}
return piecestore.CIDInfoUndefined, errors.New("GetCIDInfo failed")
}
func (tps *TestPieceStore) ListCidInfoKeys() ([]cid.Cid, error) {
panic("do not call me")
}
func (tps *TestPieceStore) ListPieceInfoKeys() ([]cid.Cid, error) {
panic("do not call me")
}
func (tps *TestPieceStore) Start(ctx context.Context) error {
return nil
}
func (tps *TestPieceStore) OnReady(ready shared.ReadyFunc) {
}