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