Skip to content

Commit

Permalink
Merge pull request #314 from thockin/pr-311-plus-thockin
Browse files Browse the repository at this point in the history
Add multi-listener test for HTTP
  • Loading branch information
k8s-ci-robot authored Jul 11, 2024
2 parents d4aae2b + a1afdb7 commit 18e509b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions net/multi_listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package net
import (
"context"
"fmt"
"io"
"net"
"net/http"
"strconv"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -482,6 +484,51 @@ func TestMultiListen_Accept(t *testing.T) {
}
}

func TestMultiListen_HTTP(t *testing.T) {
ctx := context.TODO()
ml, err := MultiListen(ctx, "tcp", ":0", ":0", ":0")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

addrs := ml.(*multiListener).Addrs()
if len(addrs) != 3 {
t.Fatalf("expected 3 listeners, got %v", addrs)
}

// serve http on multi-listener
handler := func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "hello")
}
server := http.Server{
Handler: http.HandlerFunc(handler),
}
go func() { _ = server.Serve(ml) }()
defer server.Close()

// Wait for server
awake := false
for i := 0; i < 5; i++ {
_, err = http.Get("http://" + addrs[0].String())
if err == nil {
awake = true
break
}
time.Sleep(50 * time.Millisecond)
}
if !awake {
t.Fatalf("http server did not respond in time")
}

// HTTP GET on each address.
for _, addr := range addrs {
_, err = http.Get("http://" + addr.String())
if err != nil {
t.Errorf("error connecting to %q: %v", addr.String(), err)
}
}
}

func assertError(t *testing.T, errString string, err error) {
if err == nil {
t.Errorf("Expected error '%s' but got none", errString)
Expand Down

0 comments on commit 18e509b

Please sign in to comment.