/
faketransport.go
100 lines (84 loc) · 3.35 KB
/
faketransport.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
package testutil
import (
"context"
"github.com/ipld/go-ipld-prime"
"github.com/libp2p/go-libp2p-core/peer"
datatransfer "github.com/filecoin-project/go-data-transfer"
)
// OpenedChannel records a call to open a channel
type OpenedChannel struct {
DataSender peer.ID
ChannelID datatransfer.ChannelID
Root ipld.Link
Selector ipld.Node
Channel datatransfer.ChannelState
Message datatransfer.Message
}
// ResumedChannel records a call to resume a channel
type ResumedChannel struct {
ChannelID datatransfer.ChannelID
Message datatransfer.Message
}
// CustomizedTransfer is just a way to record calls made to transport configurer
type CustomizedTransfer struct {
ChannelID datatransfer.ChannelID
Voucher datatransfer.Voucher
}
// FakeTransport is a fake transport with mocked results
type FakeTransport struct {
OpenedChannels []OpenedChannel
OpenChannelErr error
ClosedChannels []datatransfer.ChannelID
CloseChannelErr error
PausedChannels []datatransfer.ChannelID
PauseChannelErr error
ResumedChannels []ResumedChannel
ResumeChannelErr error
CleanedUpChannels []datatransfer.ChannelID
CustomizedTransfers []CustomizedTransfer
EventHandler datatransfer.EventsHandler
SetEventHandlerErr error
}
// NewFakeTransport returns a new instance of FakeTransport
func NewFakeTransport() *FakeTransport {
return &FakeTransport{}
}
// OpenChannel initiates an outgoing request for the other peer to send data
// to us on this channel
// Note: from a data transfer symantic standpoint, it doesn't matter if the
// request is push or pull -- OpenChannel is called by the party that is
// intending to receive data
func (ft *FakeTransport) OpenChannel(ctx context.Context, dataSender peer.ID, channelID datatransfer.ChannelID, root ipld.Link, stor ipld.Node, channel datatransfer.ChannelState, msg datatransfer.Message) error {
ft.OpenedChannels = append(ft.OpenedChannels, OpenedChannel{dataSender, channelID, root, stor, channel, msg})
return ft.OpenChannelErr
}
// CloseChannel closes the given channel
func (ft *FakeTransport) CloseChannel(ctx context.Context, chid datatransfer.ChannelID) error {
ft.ClosedChannels = append(ft.ClosedChannels, chid)
return ft.CloseChannelErr
}
// SetEventHandler sets the handler for events on channels
func (ft *FakeTransport) SetEventHandler(events datatransfer.EventsHandler) error {
ft.EventHandler = events
return ft.SetEventHandlerErr
}
func (ft *FakeTransport) Shutdown(ctx context.Context) error {
return nil
}
// PauseChannel paused the given channel ID
func (ft *FakeTransport) PauseChannel(ctx context.Context, chid datatransfer.ChannelID) error {
ft.PausedChannels = append(ft.PausedChannels, chid)
return ft.PauseChannelErr
}
// ResumeChannel resumes the given channel
func (ft *FakeTransport) ResumeChannel(ctx context.Context, msg datatransfer.Message, chid datatransfer.ChannelID) error {
ft.ResumedChannels = append(ft.ResumedChannels, ResumedChannel{chid, msg})
return ft.ResumeChannelErr
}
// CleanupChannel cleans up the given channel
func (ft *FakeTransport) CleanupChannel(chid datatransfer.ChannelID) {
ft.CleanedUpChannels = append(ft.CleanedUpChannels, chid)
}
func (ft *FakeTransport) RecordCustomizedTransfer(chid datatransfer.ChannelID, voucher datatransfer.Voucher) {
ft.CustomizedTransfers = append(ft.CustomizedTransfers, CustomizedTransfer{chid, voucher})
}