forked from filecoin-project/go-fil-markets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sectoraccessor.go
109 lines (89 loc) · 3.48 KB
/
sectoraccessor.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
package testnodes
import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"sync"
"testing"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-state-types/abi"
"github.com/brossetti1/go-fil-markets/retrievalmarket"
)
type sectorKey struct {
sectorID abi.SectorNumber
offset abi.UnpaddedPieceSize
length abi.UnpaddedPieceSize
}
// TestSectorAccessor is a mock implementation of the SectorAccessor
type TestSectorAccessor struct {
lk sync.Mutex
sectorStubs map[sectorKey][]byte
expectations map[sectorKey]struct{}
received map[sectorKey]struct{}
unsealed map[sectorKey]struct{}
unsealPaused chan struct{}
}
var _ retrievalmarket.SectorAccessor = &TestSectorAccessor{}
// NewTestSectorAccessor instantiates a new TestSectorAccessor
func NewTestSectorAccessor() *TestSectorAccessor {
return &TestSectorAccessor{
sectorStubs: make(map[sectorKey][]byte),
expectations: make(map[sectorKey]struct{}),
received: make(map[sectorKey]struct{}),
unsealed: make(map[sectorKey]struct{}),
}
}
func (trpn *TestSectorAccessor) IsUnsealed(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (bool, error) {
_, ok := trpn.unsealed[sectorKey{sectorID, offset, length}]
return ok, nil
}
func (trpn *TestSectorAccessor) MarkUnsealed(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) {
trpn.unsealed[sectorKey{sectorID, offset, length}] = struct{}{}
}
// StubUnseal stubs a response to attempting to unseal a sector with the given paramters
func (trpn *TestSectorAccessor) StubUnseal(sectorID abi.SectorNumber, offset, length abi.UnpaddedPieceSize, data []byte) {
trpn.sectorStubs[sectorKey{sectorID, offset, length}] = data
}
// ExpectFailedUnseal indicates an expectation that a call will be made to unseal
// a sector with the given params and should fail
func (trpn *TestSectorAccessor) ExpectFailedUnseal(sectorID abi.SectorNumber, offset, length abi.UnpaddedPieceSize) {
trpn.expectations[sectorKey{sectorID, offset, length}] = struct{}{}
}
// ExpectUnseal indicates an expectation that a call will be made to unseal
// a sector with the given params and should return the given data
func (trpn *TestSectorAccessor) ExpectUnseal(sectorID abi.SectorNumber, offset, length abi.UnpaddedPieceSize, data []byte) {
trpn.expectations[sectorKey{sectorID, offset, length}] = struct{}{}
trpn.StubUnseal(sectorID, offset, length, data)
}
func (trpn *TestSectorAccessor) PauseUnseal() {
trpn.unsealPaused = make(chan struct{})
}
func (trpn *TestSectorAccessor) FinishUnseal() {
close(trpn.unsealPaused)
}
// UnsealSector simulates unsealing a sector by returning a stubbed response
// or erroring
func (trpn *TestSectorAccessor) UnsealSector(ctx context.Context, sectorID abi.SectorNumber, offset, length abi.UnpaddedPieceSize) (io.ReadCloser, error) {
trpn.lk.Lock()
defer trpn.lk.Unlock()
if trpn.unsealPaused != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-trpn.unsealPaused:
}
}
trpn.received[sectorKey{sectorID, offset, length}] = struct{}{}
data, ok := trpn.sectorStubs[sectorKey{sectorID, offset, length}]
if !ok {
return nil, errors.New("Could not unseal")
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
}
// VerifyExpectations verifies that all expected calls were made and no other calls
// were made
func (trpn *TestSectorAccessor) VerifyExpectations(t *testing.T) {
require.Equal(t, trpn.expectations, trpn.received)
}