Skip to content

Commit fed41d5

Browse files
committed
[baseserver] Allow zero port to automatically assign port
1 parent 2261745 commit fed41d5

File tree

5 files changed

+40
-15
lines changed

5 files changed

+40
-15
lines changed

components/common-go/baseserver/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func WithHostname(hostname string) Option {
4646

4747
func WithHTTPPort(port int) Option {
4848
return func(cfg *config) error {
49-
if port <= 0 {
50-
return fmt.Errorf("http port must be greater than 0, got: %d", port)
49+
if port < 0 {
50+
return fmt.Errorf("http must not be negative, got: %d", port)
5151
}
5252

5353
cfg.httpPort = port
@@ -58,7 +58,7 @@ func WithHTTPPort(port int) Option {
5858
func WithGRPCPort(port int) Option {
5959
return func(cfg *config) error {
6060
if port < 0 {
61-
return fmt.Errorf("grpc port must be greater than 0, got: %d", port)
61+
return fmt.Errorf("grpc port must not be negative, got: %d", port)
6262
}
6363

6464
cfg.grpcPort = port

components/common-go/baseserver/options_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,28 @@ func TestOptions(t *testing.T) {
3737
}, cfg)
3838
}
3939

40-
func TestWithTTPPort_ErrorsWithNegativePort(t *testing.T) {
41-
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(-1))
42-
require.Error(t, err)
40+
func TestWithTTPPort(t *testing.T) {
41+
t.Run("negative", func(t *testing.T) {
42+
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(-1))
43+
require.Error(t, err)
44+
})
45+
46+
t.Run("zero", func(t *testing.T) {
47+
_, err := evaluateOptions(defaultConfig(), WithHTTPPort(0))
48+
require.Error(t, err)
49+
})
4350
}
4451

45-
func TestWithGRPCPort_ErrorsWithNegativePort(t *testing.T) {
46-
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(-1))
47-
require.Error(t, err)
52+
func TestWithGRPCPort(t *testing.T) {
53+
t.Run("negative", func(t *testing.T) {
54+
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(-1))
55+
require.Error(t, err)
56+
})
57+
58+
t.Run("zero", func(t *testing.T) {
59+
_, err := evaluateOptions(defaultConfig(), WithGRPCPort(0))
60+
require.Error(t, err)
61+
})
4862
}
4963

5064
func TestLogger_ErrorsWithNilLogger(t *testing.T) {

components/common-go/baseserver/server.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,23 @@ func (s *Server) Logger() *logrus.Entry {
132132
return s.cfg.logger
133133
}
134134

135+
// HTTPAddress returns address of the HTTP Server
136+
// HTTPAddress() is only available once the server has been started.
135137
func (s *Server) HTTPAddress() string {
138+
if s.httpListener == nil {
139+
return ""
140+
}
136141
protocol := "http"
137-
return fmt.Sprintf("%s://%s:%d", protocol, s.cfg.hostname, s.cfg.httpPort)
142+
addr := s.httpListener.Addr().(*net.TCPAddr)
143+
return fmt.Sprintf("%s://%s:%d", protocol, addr.IP, addr.Port)
138144
}
139145

146+
// GRPCAddress returns address of the gRPC Server
147+
// GRPCAddress() is only available once the server has been started.
140148
func (s *Server) GRPCAddress() string {
141149
protocol := "http"
142-
return fmt.Sprintf("%s://%s:%d", protocol, s.cfg.hostname, s.cfg.grpcPort)
150+
addr := s.grpcListener.Addr().(*net.TCPAddr)
151+
return fmt.Sprintf("%s://%s:%d", protocol, addr.IP, addr.Port)
143152
}
144153

145154
func (s *Server) HTTPMux() *http.ServeMux {

components/common-go/baseserver/server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func TestServer_ServesReady(t *testing.T) {
3535

3636
baseserver.WaitForServerToBeReachable(t, srv, 3*time.Second)
3737

38-
readyUR := fmt.Sprintf("%s/ready", srv.HTTPAddress())
39-
resp, err := http.Get(readyUR)
38+
readyURL := fmt.Sprintf("%s/ready", srv.HTTPAddress())
39+
resp, err := http.Get(readyURL)
4040
require.NoError(t, err)
4141
require.Equal(t, http.StatusOK, resp.StatusCode)
4242
}

components/common-go/baseserver/testing.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ func NewForTests(t *testing.T, opts ...Option) *Server {
1818
t.Helper()
1919

2020
defaultTestOpts := []Option{
21+
WithGRPCPort(0),
22+
WithHTTPPort(0),
2123
WithCloseTimeout(1 * time.Second),
2224
}
2325

@@ -44,13 +46,13 @@ func WaitForServerToBeReachable(t *testing.T, srv *Server, timeout time.Duration
4446
Timeout: tick,
4547
}
4648

47-
healthURL := fmt.Sprintf("%s/ready", srv.HTTPAddress())
48-
4949
for {
5050
select {
5151
case <-ctx.Done():
5252
require.Failf(t, "server did not become reachable in %s", timeout.String())
5353
case <-ticker.C:
54+
// We retrieve the URL on each tick, because the HTTPAddress is only available once the server is listening.
55+
healthURL := fmt.Sprintf("%s/ready", srv.HTTPAddress())
5456
_, err := client.Get(healthURL)
5557
if err != nil {
5658
continue

0 commit comments

Comments
 (0)