github.com/koko1123/flow-go-1@v0.29.6/network/p2p/compressed/mockStream.go (about) 1 package compressed 2 3 import ( 4 "io" 5 "time" 6 7 "github.com/hashicorp/go-multierror" 8 "github.com/libp2p/go-libp2p/core/network" 9 "github.com/libp2p/go-libp2p/core/protocol" 10 ) 11 12 // mockStream is a mocked libp2p stream that is implemented as a pipe with a reader and writer. 13 // Whatever is written on the stream is written by the writer on the pipe, which in turn makes 14 // it available for read by the reader. 15 type mockStream struct { 16 pw *io.PipeWriter 17 pr *io.PipeReader 18 } 19 20 func newMockStream(pw *io.PipeWriter, pr *io.PipeReader) *mockStream { 21 return &mockStream{ 22 pw: pw, 23 pr: pr, 24 } 25 } 26 27 func (m *mockStream) Read(p []byte) (int, error) { 28 n, err := m.pr.Read(p) 29 return n, err 30 } 31 32 func (m *mockStream) Write(p []byte) (int, error) { 33 return m.pw.Write(p) 34 } 35 36 func (m *mockStream) Close() error { 37 return multierror.Append(m.CloseRead(), m.CloseWrite()) 38 } 39 40 func (m *mockStream) CloseRead() error { 41 return m.pr.Close() 42 } 43 44 func (m *mockStream) CloseWrite() error { 45 return m.pw.Close() 46 } 47 48 func (m *mockStream) Reset() error { 49 return nil 50 } 51 52 func (m *mockStream) SetDeadline(_ time.Time) error { 53 return nil 54 } 55 56 func (m *mockStream) SetReadDeadline(_ time.Time) error { 57 return nil 58 } 59 60 func (m *mockStream) SetWriteDeadline(_ time.Time) error { 61 return nil 62 } 63 64 func (m *mockStream) ID() string { 65 return "" 66 } 67 68 func (m *mockStream) Protocol() protocol.ID { 69 return "" 70 } 71 72 func (m *mockStream) SetProtocol(_ protocol.ID) error { 73 return nil 74 } 75 76 func (m *mockStream) Stat() network.Stats { 77 return network.Stats{} 78 } 79 80 func (m *mockStream) Conn() network.Conn { 81 return nil 82 } 83 84 func (m *mockStream) Scope() network.StreamScope { 85 return nil 86 }