Skip to content

Commit

Permalink
Fix windows tests (#1235)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
erikdubbelboer committed Mar 4, 2022
1 parent f54ffa1 commit 7670c6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 89 deletions.
95 changes: 11 additions & 84 deletions reuseport/reuseport_test.go
@@ -1,25 +1,22 @@
package reuseport

import (
"fmt"
"io/ioutil"
"net"
"testing"
"time"
)

func TestTCP4(t *testing.T) {
t.Parallel()

testNewListener(t, "tcp4", "localhost:10081", 20, 1000)
testNewListener(t, "tcp4", "localhost:10081")
}

func TestTCP6(t *testing.T) {
t.Parallel()

// Run this test only if tcp6 interface exists.
if hasLocalIPv6(t) {
testNewListener(t, "tcp6", "[::1]:10082", 20, 1000)
testNewListener(t, "tcp6", "[::1]:10082")
}
}

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

func testNewListener(t *testing.T, network, addr string, serversCount, requestsCount int) {
var lns []net.Listener
doneCh := make(chan struct{}, serversCount)

for i := 0; i < serversCount; i++ {
ln, err := Listen(network, addr)
if err != nil {
t.Fatalf("cannot create listener %d: %s", i, err)
}
go func() {
serveEcho(t, ln)
doneCh <- struct{}{}
}()
lns = append(lns, ln)
}

for i := 0; i < requestsCount; i++ {
c, err := net.Dial(network, addr)
if err != nil {
t.Fatalf("%d. unexpected error when dialing: %s", i, err)
}
req := fmt.Sprintf("request number %d", i)
if _, err = c.Write([]byte(req)); err != nil {
t.Fatalf("%d. unexpected error when writing request: %s", i, err)
}
if err = c.(*net.TCPConn).CloseWrite(); err != nil {
t.Fatalf("%d. unexpected error when closing write end of the connection: %s", i, err)
}

var resp []byte
ch := make(chan struct{})
go func() {
if resp, err = ioutil.ReadAll(c); err != nil {
t.Errorf("%d. unexpected error when reading response: %s", i, err)
}
close(ch)
}()
select {
case <-ch:
case <-time.After(250 * time.Millisecond):
t.Fatalf("%d. timeout when waiting for response", i)
}

if string(resp) != req {
t.Fatalf("%d. unexpected response %q. Expecting %q", i, resp, req)
}
if err = c.Close(); err != nil {
t.Fatalf("%d. unexpected error when closing connection: %s", i, err)
}
}

for _, ln := range lns {
if err := ln.Close(); err != nil {
t.Fatalf("unexpected error when closing listener: %s", err)
}
func testNewListener(t *testing.T, network, addr string) {
ln1, err := Listen(network, addr)
if err != nil {
t.Fatalf("cannot create listener %v", err)
}

for i := 0; i < serversCount; i++ {
select {
case <-doneCh:
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout when waiting for servers to be closed")
}
ln2, err := Listen(network, addr)
if err != nil {
t.Fatalf("cannot create listener %v", err)
}
}

func serveEcho(t *testing.T, ln net.Listener) {
for {
c, err := ln.Accept()
if err != nil {
break
}
req, err := ioutil.ReadAll(c)
if err != nil {
t.Fatalf("unexpected error when reading request: %s", err)
}
if _, err = c.Write(req); err != nil {
t.Fatalf("unexpected error when writing response: %s", err)
}
if err = c.Close(); err != nil {
t.Fatalf("unexpected error when closing connection: %s", err)
}
}
_ = ln1.Close()
_ = ln2.Close()
}
12 changes: 8 additions & 4 deletions server_test.go
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -173,7 +174,7 @@ func TestServerPipelineFlush(t *testing.T) {

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

Expand Down Expand Up @@ -573,12 +574,15 @@ func TestRequestCtxRedirect(t *testing.T) {
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")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "/x.html", "http://qqq/x.html")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "/x.html#aaa=bbb&cc=ddd", "http://qqq/x.html#aaa=bbb&cc=ddd")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../x.html", "http://qqq/x.html")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../../x.html", "http://qqq/x.html")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "./.././../x.html", "http://qqq/x.html")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "http://foo.bar/baz", "http://foo.bar/baz")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "https://foo.bar/baz", "https://foo.bar/baz")
testRequestCtxRedirect(t, "https://foo.com/bar?aaa", "//google.com/aaa?bb", "https://google.com/aaa?bb")

if runtime.GOOS != "windows" {
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../x.html", "http://qqq/x.html")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "../../x.html", "http://qqq/x.html")
testRequestCtxRedirect(t, "http://qqq/foo/bar?baz=111", "./.././../x.html", "http://qqq/x.html")
}
}

func testRequestCtxRedirect(t *testing.T, origURL, redirectURL, expectedURL string) {
Expand Down
11 changes: 10 additions & 1 deletion uri_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"reflect"
"runtime"
"testing"
"time"
)
Expand Down Expand Up @@ -120,12 +121,16 @@ func TestURIUpdate(t *testing.T) {

// relative uri
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")
testURIUpdate(t, "http://example.com/a/b/c/d", "../qwe/p?zx=34", "http://example.com/a/b/qwe/p?zx=34")

testURIUpdate(t, "http://example.com/aaa.html?foo=bar", "?baz=434&aaa#xcv", "http://example.com/aaa.html?baz=434&aaa#xcv")
testURIUpdate(t, "http://example.com/baz", "~a/%20b=c,тест?йцу=ке", "http://example.com/~a/%20b=c,%D1%82%D0%B5%D1%81%D1%82?йцу=ке")
testURIUpdate(t, "http://example.com/baz", "/qwe#fragment", "http://example.com/qwe#fragment")
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")

if runtime.GOOS != "windows" {
testURIUpdate(t, "http://example.com/a/b/c/d", "../qwe/p?zx=34", "http://example.com/a/b/qwe/p?zx=34")
}

// hash
testURIUpdate(t, "http://example.com/#fragment1", "#fragment2", "http://example.com/#fragment2")

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

func TestURIPathNormalize(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}

t.Parallel()

var u URI
Expand Down

0 comments on commit 7670c6e

Please sign in to comment.