github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/actor/main_test.go (about)

     1  package actor
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"net"
     8  	"net/http"
     9  	"sync"
    10  	"time"
    11  
    12  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
    13  )
    14  
    15  func sts2name(sts core.MsgStatus) string {
    16  	switch sts {
    17  	case core.MsgStatusDone:
    18  		return "MsgStatusDone"
    19  	case core.MsgStatusFailed:
    20  		return "MsgStatusFailed"
    21  	case core.MsgStatusTimedOut:
    22  		return "MsgStatusTimedOut"
    23  	case core.MsgStatusUnroutable:
    24  		return "MsgStatusUnroutable"
    25  	case core.MsgStatusThrottled:
    26  		return "MsgStatusThrottled"
    27  	default:
    28  		return "Unknown"
    29  	}
    30  }
    31  
    32  type testAddr struct {
    33  	network string
    34  	address string
    35  }
    36  
    37  var _ net.Addr = (*testAddr)(nil)
    38  
    39  func newTestAddr(network, address string) *testAddr {
    40  	return &testAddr{
    41  		network: network,
    42  		address: address,
    43  	}
    44  }
    45  
    46  func (a *testAddr) Network() string {
    47  	return a.network
    48  }
    49  
    50  func (a *testAddr) String() string {
    51  	return fmt.Sprintf("%s://%s", a.network, a.address)
    52  }
    53  
    54  type testConn struct {
    55  	buf        []byte
    56  	offset     int
    57  	lock       sync.Mutex
    58  	localaddr  net.Addr
    59  	remoteaddr net.Addr
    60  	closed     bool
    61  }
    62  
    63  var _ net.Conn = (*testConn)(nil)
    64  
    65  func min(a, b int) int {
    66  	if a < b {
    67  		return a
    68  	}
    69  	return b
    70  }
    71  
    72  func newTestConn(localaddr, remoteaddr net.Addr) *testConn {
    73  	return &testConn{
    74  		buf:        make([]byte, 0),
    75  		localaddr:  localaddr,
    76  		remoteaddr: remoteaddr,
    77  		closed:     false,
    78  	}
    79  }
    80  
    81  func (c *testConn) Read(b []byte) (int, error) {
    82  	c.lock.Lock()
    83  	defer c.lock.Unlock()
    84  
    85  	var err error
    86  	l := min(len(b), len(c.buf)-c.offset)
    87  	n := copy(b, c.buf[c.offset:c.offset+l+0])
    88  
    89  	c.offset += l
    90  	if c.offset == len(c.buf) {
    91  		err = io.EOF
    92  	}
    93  
    94  	return n, err
    95  }
    96  
    97  func (c *testConn) Write(b []byte) (int, error) {
    98  	c.lock.Lock()
    99  	defer c.lock.Unlock()
   100  
   101  	c.buf = make([]byte, len(b))
   102  	n := copy(c.buf, b)
   103  
   104  	return n, nil
   105  }
   106  
   107  func (c *testConn) Close() error {
   108  	c.lock.Lock()
   109  	defer c.lock.Unlock()
   110  	c.closed = true
   111  
   112  	return nil
   113  }
   114  
   115  func (c *testConn) LocalAddr() net.Addr {
   116  	return c.localaddr
   117  }
   118  
   119  func (c *testConn) RemoteAddr() net.Addr {
   120  	return c.remoteaddr
   121  }
   122  
   123  func (c *testConn) SetDeadline(t time.Time) error {
   124  	return nil
   125  }
   126  
   127  func (c *testConn) SetReadDeadline(t time.Time) error {
   128  	return nil
   129  }
   130  
   131  func (c *testConn) SetWriteDeadline(t time.Time) error {
   132  	return nil
   133  }
   134  
   135  type testResponseWriter struct {
   136  	headers map[string][]string
   137  	status  int
   138  	bytes.Buffer
   139  }
   140  
   141  var _ http.ResponseWriter = (*testResponseWriter)(nil)
   142  
   143  func (rw *testResponseWriter) Header() http.Header {
   144  	return rw.headers
   145  }
   146  
   147  func (rw *testResponseWriter) WriteHeader(status int) {
   148  	rw.status = status
   149  }
   150  
   151  func eqErr(e1, e2 error) bool {
   152  	if e1 == nil || e2 == nil {
   153  		return e1 == e2
   154  	}
   155  	return e1.Error() == e2.Error()
   156  }