-
Notifications
You must be signed in to change notification settings - Fork 22
/
libp2ptransfermgr_test.go
155 lines (130 loc) · 4.2 KB
/
libp2ptransfermgr_test.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
package filclient
import (
"context"
"fmt"
"testing"
"time"
"github.com/filecoin-project/boost/transport/httptransport"
boosttypes "github.com/filecoin-project/boost/transport/types"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/require"
)
func TestLibp2pTransferManager(t *testing.T) {
runTest := func(t *testing.T, completeBeforeExpiry bool) {
opts := libp2pTransferManagerOpts{
xferTimeout: 100 * time.Millisecond,
authCheckPeriod: 200 * time.Millisecond,
}
ctx, cancel := context.WithTimeout(context.Background(), opts.authCheckPeriod*2)
defer cancel()
ds := sync.MutexWrap(datastore.NewMapDatastore())
server := &mockLibp2pCarServer{}
authDB := httptransport.NewAuthTokenDB(ds)
tm := newLibp2pTransferManager(server, ds, authDB, opts)
type evt struct {
dbid uint
st ChannelState
}
evts := make(chan evt, 1024)
unsub, err := tm.Subscribe(func(dbid uint, st ChannelState) {
evts <- evt{dbid: dbid, st: st}
})
require.NoError(t, err)
defer unsub()
err = tm.Start(ctx)
require.NoError(t, err)
authToken, err := httptransport.GenerateAuthToken()
require.NoError(t, err)
proposalCid, err := cid.Parse("bafkqaaa")
require.NoError(t, err)
payloadCid, err := cid.Parse("bafkqaab")
require.NoError(t, err)
dbid := uint(1)
size := uint64(1024)
err = tm.PrepareForDataRequest(ctx, dbid, authToken, proposalCid, payloadCid, size)
require.NoError(t, err)
// Expect an "Ongoing" event to be passed through to the subscriber
dbidstr := fmt.Sprintf("%d", dbid)
st := boosttypes.TransferState{
Status: boosttypes.TransferStatusOngoing,
}
server.simulateEvent(dbidstr, st)
require.Len(t, evts, 1)
e := <-evts
require.Equal(t, datatransfer.Ongoing, e.st.Status)
if completeBeforeExpiry {
// Simulate a "Completed" event
st := boosttypes.TransferState{
Status: boosttypes.TransferStatusCompleted,
}
server.simulateEvent(dbidstr, st)
require.Len(t, evts, 1)
e := <-evts
require.Equal(t, datatransfer.Completed, e.st.Status)
}
// Simulate a Failed event
st = boosttypes.TransferState{
Status: boosttypes.TransferStatusFailed,
}
server.simulateEvent(dbidstr, st)
require.Len(t, evts, 0)
// Wait until the auth token expires, and the auth token check runs
select {
case <-ctx.Done():
// The server fired a Complete event so we expected the manager not
// to fire a Failed event
if completeBeforeExpiry {
return
}
// The server did not fire a Complete event, so we were expecting
// the manager to fire a Failed event
require.Fail(t, "timed out waiting for Failed event")
case e = <-evts:
// The server fired a Complete event, so we did not expect the
// manager to fire a Failed event
if completeBeforeExpiry {
require.Fail(t, "unexpected event %s", e.st.StatusStr)
return
}
// The server did not fire a Complete event, so we expect the
// manager to fire a Failed event
require.Equal(t, datatransfer.Failed, e.st.Status)
}
}
t.Run("pass if complete event before token expiry", func(t *testing.T) {
runTest(t, true)
})
t.Run("fail if no complete event", func(t *testing.T) {
runTest(t, false)
})
}
type mockLibp2pCarServer struct {
listener httptransport.EventListenerFn
}
func (m *mockLibp2pCarServer) simulateEvent(dbidstr string, st boosttypes.TransferState) {
m.listener(dbidstr, st)
}
func (m *mockLibp2pCarServer) Subscribe(listener httptransport.EventListenerFn) httptransport.UnsubFn {
m.listener = listener
return func() {}
}
func (m *mockLibp2pCarServer) ID() peer.ID {
return "pid"
}
func (m *mockLibp2pCarServer) Start(ctx context.Context) error {
return nil
}
func (m *mockLibp2pCarServer) Get(id string) (*httptransport.Libp2pTransfer, error) {
return nil, nil
}
func (m *mockLibp2pCarServer) CancelTransfer(ctx context.Context, id string) (*boosttypes.TransferState, error) {
return nil, nil
}
func (m *mockLibp2pCarServer) Matching(f httptransport.MatchFn) ([]*httptransport.Libp2pTransfer, error) {
return nil, nil
}
var _ libp2pCarServer = (*mockLibp2pCarServer)(nil)