github.com/anycable/anycable-go@v1.5.1/mocks/MockConnection.go (about)

     1  package mocks
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"time"
     7  )
     8  
     9  type MockConnection struct {
    10  	send   chan []byte
    11  	closed bool
    12  }
    13  
    14  func (conn MockConnection) Write(msg []byte, deadline time.Time) error {
    15  	conn.send <- msg
    16  	return nil
    17  }
    18  
    19  func (conn MockConnection) WriteBinary(msg []byte, deadline time.Time) error {
    20  	conn.send <- msg
    21  	return nil
    22  }
    23  
    24  func (conn MockConnection) Read() ([]byte, error) {
    25  	timer := time.After(100 * time.Millisecond)
    26  
    27  	select {
    28  	case <-timer:
    29  		return nil, errors.New("connection hasn't received any messages")
    30  	case msg := <-conn.send:
    31  		return msg, nil
    32  	}
    33  }
    34  
    35  func (conn MockConnection) ReadIndifinitely() []byte {
    36  	msg := <-conn.send
    37  	return msg
    38  }
    39  
    40  func (conn MockConnection) Close(_code int, _reason string) {
    41  	conn.send <- []byte("")
    42  }
    43  
    44  func (conn MockConnection) Descriptor() net.Conn {
    45  	return nil
    46  }
    47  
    48  func NewMockConnection() MockConnection {
    49  	return MockConnection{closed: false, send: make(chan []byte, 10)}
    50  }