Skip to content

Commit

Permalink
Fix failing ServerInUseHostPort test on MacOS (#2477)
Browse files Browse the repository at this point in the history
* Fix failing ServerInUseHostPort test

Signed-off-by: albertteoh <albert.teoh@logz.io>

* Dynamically allocate port. Remove strict error assertion.

Signed-off-by: albertteoh <albert.teoh@logz.io>

* Remove unstable addresses from test names

Signed-off-by: albertteoh <albert.teoh@logz.io>

* Update cmd/query/app/server_test.go

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
Signed-off-by: albertteoh <albert.teoh@logz.io>

* Add require dependency

Signed-off-by: albertteoh <albert.teoh@logz.io>

Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
  • Loading branch information
albertteoh and yurishkuro committed Sep 18, 2020
1 parent 5b9598d commit 899e93a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
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

0 comments on commit 899e93a

Please sign in to comment.