Skip to content

Commit 7670c6e

Browse files
Fix windows tests (#1235)
* Fix windows tests Just ignore /../ tests on windows until we have proper suppor. * Remove useless test code This code was basically just testing if tcp works. To test if SO_REUSEPORT works we only have to try to listen on the same addr:port twice. * Fix test
1 parent f54ffa1 commit 7670c6e

File tree

3 files changed

+29
-89
lines changed

3 files changed

+29
-89
lines changed

reuseport/reuseport_test.go

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
package reuseport
22

33
import (
4-
"fmt"
5-
"io/ioutil"
64
"net"
75
"testing"
8-
"time"
96
)
107

118
func TestTCP4(t *testing.T) {
129
t.Parallel()
1310

14-
testNewListener(t, "tcp4", "localhost:10081", 20, 1000)
11+
testNewListener(t, "tcp4", "localhost:10081")
1512
}
1613

1714
func TestTCP6(t *testing.T) {
1815
t.Parallel()
1916

2017
// Run this test only if tcp6 interface exists.
2118
if hasLocalIPv6(t) {
22-
testNewListener(t, "tcp6", "[::1]:10082", 20, 1000)
19+
testNewListener(t, "tcp6", "[::1]:10082")
2320
}
2421
}
2522

@@ -36,87 +33,17 @@ func hasLocalIPv6(t *testing.T) bool {
3633
return false
3734
}
3835

39-
func testNewListener(t *testing.T, network, addr string, serversCount, requestsCount int) {
40-
var lns []net.Listener
41-
doneCh := make(chan struct{}, serversCount)
42-
43-
for i := 0; i < serversCount; i++ {
44-
ln, err := Listen(network, addr)
45-
if err != nil {
46-
t.Fatalf("cannot create listener %d: %s", i, err)
47-
}
48-
go func() {
49-
serveEcho(t, ln)
50-
doneCh <- struct{}{}
51-
}()
52-
lns = append(lns, ln)
53-
}
54-
55-
for i := 0; i < requestsCount; i++ {
56-
c, err := net.Dial(network, addr)
57-
if err != nil {
58-
t.Fatalf("%d. unexpected error when dialing: %s", i, err)
59-
}
60-
req := fmt.Sprintf("request number %d", i)
61-
if _, err = c.Write([]byte(req)); err != nil {
62-
t.Fatalf("%d. unexpected error when writing request: %s", i, err)
63-
}
64-
if err = c.(*net.TCPConn).CloseWrite(); err != nil {
65-
t.Fatalf("%d. unexpected error when closing write end of the connection: %s", i, err)
66-
}
67-
68-
var resp []byte
69-
ch := make(chan struct{})
70-
go func() {
71-
if resp, err = ioutil.ReadAll(c); err != nil {
72-
t.Errorf("%d. unexpected error when reading response: %s", i, err)
73-
}
74-
close(ch)
75-
}()
76-
select {
77-
case <-ch:
78-
case <-time.After(250 * time.Millisecond):
79-
t.Fatalf("%d. timeout when waiting for response", i)
80-
}
81-
82-
if string(resp) != req {
83-
t.Fatalf("%d. unexpected response %q. Expecting %q", i, resp, req)
84-
}
85-
if err = c.Close(); err != nil {
86-
t.Fatalf("%d. unexpected error when closing connection: %s", i, err)
87-
}
88-
}
89-
90-
for _, ln := range lns {
91-
if err := ln.Close(); err != nil {
92-
t.Fatalf("unexpected error when closing listener: %s", err)
93-
}
36+
func testNewListener(t *testing.T, network, addr string) {
37+
ln1, err := Listen(network, addr)
38+
if err != nil {
39+
t.Fatalf("cannot create listener %v", err)
9440
}
9541

96-
for i := 0; i < serversCount; i++ {
97-
select {
98-
case <-doneCh:
99-
case <-time.After(200 * time.Millisecond):
100-
t.Fatalf("timeout when waiting for servers to be closed")
101-
}
42+
ln2, err := Listen(network, addr)
43+
if err != nil {
44+
t.Fatalf("cannot create listener %v", err)
10245
}
103-
}
10446

105-
func serveEcho(t *testing.T, ln net.Listener) {
106-
for {
107-
c, err := ln.Accept()
108-
if err != nil {
109-
break
110-
}
111-
req, err := ioutil.ReadAll(c)
112-
if err != nil {
113-
t.Fatalf("unexpected error when reading request: %s", err)
114-
}
115-
if _, err = c.Write(req); err != nil {
116-
t.Fatalf("unexpected error when writing response: %s", err)
117-
}
118-
if err = c.Close(); err != nil {
119-
t.Fatalf("unexpected error when closing connection: %s", err)
120-
}
121-
}
47+
_ = ln1.Close()
48+
_ = ln2.Close()
12249
}

server_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os"
1717
"reflect"
1818
"regexp"
19+
"runtime"
1920
"strings"
2021
"sync"
2122
"testing"
@@ -173,7 +174,7 @@ func TestServerPipelineFlush(t *testing.T) {
173174

174175
// Since the second request takes 200ms to finish we expect the first one to be flushed earlier.
175176
d := time.Since(start)
176-
if d > time.Millisecond*100 {
177+
if d >= time.Millisecond*200 {
177178
t.Fatalf("had to wait for %v", d)
178179
}
179180

@@ -573,12 +574,15 @@ func TestRequestCtxRedirect(t *testing.T) {
573574
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "x.html?b=1#aaa=bbb&cc=ddd", "http://qqq/foo/x.html?b=1#aaa=bbb&cc=ddd")
574575
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "/x.html", "http://qqq/x.html")
575576
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "/x.html#aaa=bbb&cc=ddd", "http://qqq/x.html#aaa=bbb&cc=ddd")
576-
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../x.html", "http://qqq/x.html")
577-
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../../x.html", "http://qqq/x.html")
578-
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "./.././../x.html", "http://qqq/x.html")
579577
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "http://foo.bar/baz", "http://foo.bar/baz")
580578
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "https://foo.bar/baz", "https://foo.bar/baz")
581579
testRequestCtxRedirect(t, "https://foo.com/bar?aaa", "//google.com/aaa?bb", "https://google.com/aaa?bb")
580+
581+
if runtime.GOOS != "windows" {
582+
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../x.html", "http://qqq/x.html")
583+
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../../x.html", "http://qqq/x.html")
584+
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "./.././../x.html", "http://qqq/x.html")
585+
}
582586
}
583587

584588
func testRequestCtxRedirect(t *testing.T, origURL, redirectURL, expectedURL string) {

uri_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"reflect"
7+
"runtime"
78
"testing"
89
"time"
910
)
@@ -120,12 +121,16 @@ func TestURIUpdate(t *testing.T) {
120121

121122
// relative uri
122123
testURIUpdate(t, "http://example.com/baz/xxx.html?aaa=22#aaa", "bb.html?xx=12#pp", "http://example.com/baz/bb.html?xx=12#pp")
123-
testURIUpdate(t, "http://example.com/a/b/c/d", "../qwe/p?zx=34", "http://example.com/a/b/qwe/p?zx=34")
124+
124125
testURIUpdate(t, "http://example.com/aaa.html?foo=bar", "?baz=434&aaa#xcv", "http://example.com/aaa.html?baz=434&aaa#xcv")
125126
testURIUpdate(t, "http://example.com/baz", "~a/%20b=c,тест?йцу=ке", "http://example.com/~a/%20b=c,%D1%82%D0%B5%D1%81%D1%82?йцу=ке")
126127
testURIUpdate(t, "http://example.com/baz", "/qwe#fragment", "http://example.com/qwe#fragment")
127128
testURIUpdate(t, "http://example.com/baz/xxx", "aaa.html#bb?cc=dd&ee=dfd", "http://example.com/baz/aaa.html#bb?cc=dd&ee=dfd")
128129

130+
if runtime.GOOS != "windows" {
131+
testURIUpdate(t, "http://example.com/a/b/c/d", "../qwe/p?zx=34", "http://example.com/a/b/qwe/p?zx=34")
132+
}
133+
129134
// hash
130135
testURIUpdate(t, "http://example.com/#fragment1", "#fragment2", "http://example.com/#fragment2")
131136

@@ -147,6 +152,10 @@ func testURIUpdate(t *testing.T, base, update, result string) {
147152
}
148153

149154
func TestURIPathNormalize(t *testing.T) {
155+
if runtime.GOOS == "windows" {
156+
t.SkipNow()
157+
}
158+
150159
t.Parallel()
151160

152161
var u URI

0 commit comments

Comments
 (0)