Skip to content

Commit 62f3a5e

Browse files
committed
add test and gofumpt linting
1 parent 956f8fe commit 62f3a5e

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

protocol_test.go

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"io"
1414
"io/ioutil"
1515
"net"
16+
"net/http"
1617
"testing"
1718
"time"
1819
)
@@ -83,7 +84,6 @@ func TestRequiredWithReadHeaderTimeout(t *testing.T) {
8384
start := time.Now()
8485

8586
l, err := net.Listen("tcp", "127.0.0.1:0")
86-
8787
if err != nil {
8888
t.Fatalf("err: %v", err)
8989
}
@@ -138,7 +138,6 @@ func TestUseWithReadHeaderTimeout(t *testing.T) {
138138
start := time.Now()
139139

140140
l, err := net.Listen("tcp", "127.0.0.1:0")
141-
142141
if err != nil {
143142
t.Fatalf("err: %v", err)
144143
}
@@ -848,6 +847,7 @@ func TestReadingIsRefusedWhenProxyHeaderPresentButNotAllowed(t *testing.T) {
848847
t.Fatalf("client error: %v", err)
849848
}
850849
}
850+
851851
func TestIgnorePolicyIgnoresIpFromProxyHeader(t *testing.T) {
852852
l, err := net.Listen("tcp", "127.0.0.1:0")
853853
if err != nil {
@@ -1275,6 +1275,48 @@ func Test_ConnectionErrorsWhenHeaderValidationFails(t *testing.T) {
12751275
}
12761276
}
12771277

1278+
func Test_ConnectionHandlesInvalidUpstreamError(t *testing.T) {
1279+
l, err := net.Listen("tcp", "localhost:8080")
1280+
if err != nil {
1281+
t.Fatalf("error creating listener: %v", err)
1282+
}
1283+
1284+
times := 0
1285+
1286+
newLn := &Listener{
1287+
Listener: l,
1288+
ConnPolicy: func(_ ConnPolicyOptions) (Policy, error) {
1289+
// Return the invalid upstream error on the first call, the listener
1290+
// should remain open and accepting.
1291+
if times == 0 {
1292+
times++
1293+
return REJECT, ErrInvalidUpstream
1294+
}
1295+
1296+
return REJECT, ErrNoProxyProtocol
1297+
},
1298+
}
1299+
1300+
// Kick off the listener and capture any error.
1301+
var listenerErr error
1302+
go func(t *testing.T) {
1303+
_, listenerErr = newLn.Accept()
1304+
}(t)
1305+
1306+
// Make two calls to trigger the listener's accept, the first should experience
1307+
// the ErrInvalidUpstream and keep the listener open, the second should experience
1308+
// a different error which will cause the listener to close.
1309+
_, _ = http.Get("http://localhost:8080")
1310+
if listenerErr != nil {
1311+
t.Fatalf("invalid upstream shouldn't return an error: %v", listenerErr)
1312+
}
1313+
1314+
_, _ = http.Get("http://localhost:8080")
1315+
if listenerErr == nil {
1316+
t.Fatalf("errors other than invalid upstream should error")
1317+
}
1318+
}
1319+
12781320
type TestTLSServer struct {
12791321
Listener net.Listener
12801322

@@ -1483,9 +1525,11 @@ func (c *testConn) ReadFrom(r io.Reader) (int64, error) {
14831525
b, err := ioutil.ReadAll(r)
14841526
return int64(len(b)), err
14851527
}
1528+
14861529
func (c *testConn) Write(p []byte) (int, error) {
14871530
return len(p), nil
14881531
}
1532+
14891533
func (c *testConn) Read(p []byte) (int, error) {
14901534
if c.reads == 0 {
14911535
return 0, io.EOF
@@ -1534,7 +1578,7 @@ func TestCopyFromWrappedConnectionToWrappedConnection(t *testing.T) {
15341578
}
15351579

15361580
func benchmarkTCPProxy(size int, b *testing.B) {
1537-
//create and start the echo backend
1581+
// create and start the echo backend
15381582
backend, err := net.Listen("tcp", "127.0.0.1:0")
15391583
if err != nil {
15401584
b.Fatalf("err: %v", err)
@@ -1555,7 +1599,7 @@ func benchmarkTCPProxy(size int, b *testing.B) {
15551599
}
15561600
}()
15571601

1558-
//start the proxyprotocol enabled tcp proxy
1602+
// start the proxyprotocol enabled tcp proxy
15591603
l, err := net.Listen("tcp", "127.0.0.1:0")
15601604
if err != nil {
15611605
b.Fatalf("err: %v", err)
@@ -1604,7 +1648,7 @@ func benchmarkTCPProxy(size int, b *testing.B) {
16041648
},
16051649
}
16061650

1607-
//now for the actual benchmark
1651+
// now for the actual benchmark
16081652
b.ResetTimer()
16091653
for n := 0; n < b.N; n++ {
16101654
conn, err := net.Dial("tcp", pl.Addr().String())
@@ -1615,16 +1659,15 @@ func benchmarkTCPProxy(size int, b *testing.B) {
16151659
if _, err := header.WriteTo(conn); err != nil {
16161660
b.Fatalf("err: %v", err)
16171661
}
1618-
//send data
1662+
// send data
16191663
go func() {
16201664
_, err = conn.Write(data)
16211665
_ = conn.(*net.TCPConn).CloseWrite()
16221666
if err != nil {
16231667
panic(fmt.Sprintf("Failed to write data: %v", err))
16241668
}
1625-
16261669
}()
1627-
//receive data
1670+
// receive data
16281671
n, err := io.Copy(ioutil.Discard, conn)
16291672
if n != int64(len(data)) {
16301673
b.Fatalf("Expected to receive %d bytes, got %d", len(data), n)
@@ -1639,24 +1682,31 @@ func benchmarkTCPProxy(size int, b *testing.B) {
16391682
func BenchmarkTCPProxy16KB(b *testing.B) {
16401683
benchmarkTCPProxy(16*1024, b)
16411684
}
1685+
16421686
func BenchmarkTCPProxy32KB(b *testing.B) {
16431687
benchmarkTCPProxy(32*1024, b)
16441688
}
1689+
16451690
func BenchmarkTCPProxy64KB(b *testing.B) {
16461691
benchmarkTCPProxy(64*1024, b)
16471692
}
1693+
16481694
func BenchmarkTCPProxy128KB(b *testing.B) {
16491695
benchmarkTCPProxy(128*1024, b)
16501696
}
1697+
16511698
func BenchmarkTCPProxy256KB(b *testing.B) {
16521699
benchmarkTCPProxy(256*1024, b)
16531700
}
1701+
16541702
func BenchmarkTCPProxy512KB(b *testing.B) {
16551703
benchmarkTCPProxy(512*1024, b)
16561704
}
1705+
16571706
func BenchmarkTCPProxy1024KB(b *testing.B) {
16581707
benchmarkTCPProxy(1024*1024, b)
16591708
}
1709+
16601710
func BenchmarkTCPProxy2048KB(b *testing.B) {
16611711
benchmarkTCPProxy(2048*1024, b)
16621712
}

0 commit comments

Comments
 (0)