Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing ServerInUseHostPort test on MacOS #2477

Merged
merged 5 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/query/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewServer(logger *zap.Logger, querySvc *querysvc.QueryService, options *Que
tracer: tracer,
grpcServer: grpcServer,
httpServer: createHTTPServer(querySvc, options, tracer, logger),
separatePorts: (grpcPort != httpPort),
separatePorts: grpcPort != httpPort,
unavailableChannel: make(chan healthcheck.Status),
}, nil
}
Expand Down
59 changes: 38 additions & 21 deletions cmd/query/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"

Expand Down Expand Up @@ -73,28 +74,44 @@ func TestServerBadHostPort(t *testing.T) {
}

func TestServerInUseHostPort(t *testing.T) {

for _, hostPort := range [2]string{":8080", ":8081"} {
conn, err := net.Listen("tcp", hostPort)
assert.NoError(t, err)

server, err := NewServer(zap.NewNop(), &querysvc.QueryService{},
&QueryOptions{HTTPHostPort: "127.0.0.1:8080", GRPCHostPort: "127.0.0.1:8081", BearerTokenPropagation: true},
opentracing.NoopTracer{})
assert.NoError(t, err)

err = server.Start()
assert.NotNil(t, err)
conn.Close()
if server.grpcConn != nil {
server.grpcConn.Close()
}
if server.httpConn != nil {
server.httpConn.Close()
}

const availableHostPort = "127.0.0.1:0"
conn, err := net.Listen("tcp", availableHostPort)
require.NoError(t, err)
defer func() { require.NoError(t, conn.Close()) }()

testCases := []struct {
name string
httpHostPort string
grpcHostPort string
}{
{"HTTP host port clash", conn.Addr().String(), availableHostPort},
{"GRPC host port clash", availableHostPort, conn.Addr().String()},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
server, err := NewServer(
zap.NewNop(),
&querysvc.QueryService{},
&QueryOptions{
HTTPHostPort: tc.httpHostPort,
GRPCHostPort: tc.grpcHostPort,
BearerTokenPropagation: true,
},
opentracing.NoopTracer{},
)
assert.NoError(t, err)

err = server.Start()
assert.Error(t, err)

if server.grpcConn != nil {
server.grpcConn.Close()
}
if server.httpConn != nil {
server.httpConn.Close()
}
})
}

}

func TestServer(t *testing.T) {
Expand Down