github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/test/mockStream.go (about)

     1  package p2ptest
     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  }