Skip to content

Commit 5302f3e

Browse files
committed
1 parent 9c8a7a3 commit 5302f3e

File tree

1 file changed

+31
-38
lines changed

1 file changed

+31
-38
lines changed

proxy_test.go

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ package proxy
2323
import (
2424
"bufio"
2525
"bytes"
26-
"fmt"
2726
"github.com/stretchr/testify/require"
2827
fh "github.com/valyala/fasthttp"
2928
"net"
3029
h "net/http"
3130
ht "net/http/httptest"
32-
"sync"
3331
"testing"
3432
"time"
3533
)
@@ -41,7 +39,7 @@ func TestFastProxyRoundTrip(t *testing.T) {
4139
resp.SetBodyString(blabla)
4240
buff := new(bytes.Buffer)
4341
resp.WriteTo(buff)
44-
server := newMockConn(buff.String())
42+
server := newMockConn(buff.String(), false)
4543
dial := func(iface string) func(string,
4644
string) (net.Conn, error) {
4745
return func(network, addr string) (n net.Conn, e error) {
@@ -55,7 +53,7 @@ func TestFastProxyRoundTrip(t *testing.T) {
5553
req.SetBodyString(bla)
5654
buff0 := new(bytes.Buffer)
5755
req.WriteTo(buff0)
58-
client := newMockConn(buff0.String())
56+
client := newMockConn(buff0.String(), false)
5957

6058
ctl := func(o *Operation) *Result { return new(Result) }
6159
p := NewFastProxy(ctl, dial, time.Now)
@@ -77,7 +75,7 @@ func TestFastProxyRoundTrip(t *testing.T) {
7775

7876
func TestFastProxyConnect(t *testing.T) {
7977
bla, blabla := "bla", "blabla"
80-
server := newMockConn(blabla)
78+
server := newMockConn(blabla, false)
8179
dial := func(iface string) func(string, string) (net.Conn,
8280
error) {
8381
return func(network, addr string) (n net.Conn, e error) {
@@ -97,7 +95,7 @@ func TestFastProxyConnect(t *testing.T) {
9795
_, e := r.WriteTo(buff)
9896
require.NoError(t, e)
9997
s := buff.String()
100-
client := newMockConn(s + bla)
98+
client := newMockConn(s+bla, false)
10199
// client connection has the content of a valid request followed
102100
// by raw data
103101
e0 := srv.ServeConn(client)
@@ -124,7 +122,7 @@ func TestStdProxyRoundTrip(t *testing.T) {
124122
r := ht.NewRequest(h.MethodGet, "http://example.com", nil)
125123
resp.Request = r
126124
s := buff.String()
127-
server := newMockConn(s)
125+
server := newMockConn(s, true)
128126
dial := func(iface string) func(string, string) (net.Conn,
129127
error) {
130128
return func(network, addr string) (n net.Conn, e error) {
@@ -141,7 +139,9 @@ func TestStdProxyRoundTrip(t *testing.T) {
141139
func TestStdProxyConnect(t *testing.T) {
142140
ctl := func(o *Operation) *Result { return new(Result) }
143141
bla, blabla := "bla", "blabla"
144-
client, server := newMockConn(bla), newMockConn(blabla)
142+
client, server :=
143+
newMockConn(bla, false),
144+
newMockConn(blabla, false)
145145
dial := func(iface string) func(string, string) (net.Conn,
146146
error) {
147147
return func(network, addr string) (n net.Conn, e error) {
@@ -177,7 +177,9 @@ func (j *hijacker) Hijack() (c net.Conn,
177177

178178
func TestCopyConns(t *testing.T) {
179179
bla, blabla := "bla", "blabla"
180-
client, server := newMockConn(bla), newMockConn(blabla)
180+
client, server :=
181+
newMockConn(bla, false),
182+
newMockConn(blabla, false)
181183
copyConns(server, client)
182184
<-client.clöse
183185
<-server.clöse
@@ -186,53 +188,44 @@ func TestCopyConns(t *testing.T) {
186188
}
187189

188190
type mockConn struct {
189-
name string
190-
read *bytes.Buffer
191-
write *bytes.Buffer
192-
clöse chan bool
193-
closed bool
194-
closedAcc *sync.Mutex
191+
name string
192+
read *bytes.Buffer
193+
write *bytes.Buffer
194+
clöse chan bool
195+
closed bool
196+
writeFirst bool
197+
writeOk chan bool
195198
}
196199

197-
func newMockConn(content string) (m *mockConn) {
200+
func newMockConn(content string, writeFirst bool) (m *mockConn) {
198201
m = &mockConn{
199-
read: bytes.NewBufferString(content),
200-
write: new(bytes.Buffer),
201-
clöse: make(chan bool, 1),
202-
closedAcc: new(sync.Mutex),
202+
read: bytes.NewBufferString(content),
203+
write: new(bytes.Buffer),
204+
clöse: make(chan bool, 1),
205+
writeOk: make(chan bool, 1),
206+
writeFirst: writeFirst,
203207
}
204208
return
205209
}
206210

207211
func (m *mockConn) Read(p []byte) (n int, e error) {
208-
m.closedAcc.Lock()
209-
closed := m.closed
210-
m.closedAcc.Unlock()
211-
if closed {
212-
e = fmt.Errorf("closed")
213-
} else {
214-
n, e = m.read.Read(p)
212+
if m.writeFirst && m.read.Len() != 0 {
213+
<-m.writeOk
215214
}
215+
n, e = m.read.Read(p)
216216
return
217217
}
218218

219219
func (m *mockConn) Write(p []byte) (n int, e error) {
220-
m.closedAcc.Lock()
221-
closed := m.closed
222-
m.closedAcc.Unlock()
223-
if closed {
224-
e = fmt.Errorf("closed")
225-
} else {
226-
n, e = m.write.Write(p)
220+
n, e = m.write.Write(p)
221+
if m.writeFirst {
222+
m.writeOk <- true
227223
}
228224
return
229225
}
230226

231227
func (m *mockConn) Close() (e error) {
232-
m.closedAcc.Lock()
233-
m.closed = true
234-
m.closedAcc.Unlock()
235-
m.clöse <- m.closed
228+
m.clöse <- true
236229
return
237230
}
238231

0 commit comments

Comments
 (0)