9 var errClosed = errors.New("use of closed network connection")
11 // InmemSocket implements net.Listener using in-memory only connections.
12 type InmemSocket struct {
19 // dummyAddr is used to satisfy net.Addr for the in-mem socket
20 // it is just stored as a string and returns the string for all calls
23 // NewInmemSocket creates an in-memory only net.Listener
24 // The addr argument can be any string, but is used to satisfy the `Addr()` part
25 // of the net.Listener interface
26 func NewInmemSocket(addr string, bufSize int) *InmemSocket {
28 chConn: make(chan net.Conn, bufSize),
29 chClose: make(chan struct{}),
34 // Addr returns the socket's addr string to satisfy net.Listener
35 func (s *InmemSocket) Addr() net.Addr {
36 return dummyAddr(s.addr)
39 // Accept implements the Accept method in the Listener interface; it waits for the next call and returns a generic Conn.
40 func (s *InmemSocket) Accept() (net.Conn, error) {
42 case conn := <-s.chConn:
49 // Close closes the listener. It will be unavailable for use once closed.
50 func (s *InmemSocket) Close() error {
61 // Dial is used to establish a connection with the in-mem server
62 func (s *InmemSocket) Dial(network, addr string) (net.Conn, error) {
63 srvConn, clientConn := net.Pipe()
65 case s.chConn <- srvConn:
70 return clientConn, nil
73 // Network returns the addr string, satisfies net.Addr
74 func (a dummyAddr) Network() string {
78 // String returns the string form
79 func (a dummyAddr) String() string {