Skip to content

Commit

Permalink
Only listen on localhost for unit tests (#261)
Browse files Browse the repository at this point in the history
The unit tests currently spin up instances of the server that accept all
incoming connections, which causes a security dialog on Macs. Instead,
for unit tests we only want to listen for incoming connections coming
from localhost.
  • Loading branch information
richardhuaaa authored Oct 24, 2024
1 parent 51cbe4f commit 125db4b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/replication/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func main() {
chainRegistry,
db,
blockchainPublisher,
fmt.Sprintf("0.0.0.0:%d", options.API.Port),
)
if err != nil {
log.Fatal("initializing server", zap.Error(err))
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"context"
"fmt"
"net"
"strings"
"sync"
Expand Down Expand Up @@ -33,11 +32,11 @@ type ApiServer struct {
func NewAPIServer(
ctx context.Context,
log *zap.Logger,
port int,
listenAddress string,
enableReflection bool,
registrationFunc RegistrationFunc,
) (*ApiServer, error) {
grpcListener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
grpcListener, err := net.Listen("tcp", listenAddress)

if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func NewReplicationServer(
nodeRegistry registry.NodeRegistry,
writerDB *sql.DB,
blockchainPublisher blockchain.IBlockchainPublisher,
listenAddress string,
) (*ReplicationServer, error) {
var err error

Expand Down Expand Up @@ -147,7 +148,7 @@ func NewReplicationServer(
s.apiServer, err = api.NewAPIServer(
s.ctx,
log,
options.API.Port,
listenAddress,
options.Reflection.Enable,
serviceRegistrationFunc,
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewTestServer(
API: config.ApiOptions{
Port: port,
},
}, registry, db, messagePublisher)
}, registry, db, messagePublisher, fmt.Sprintf("localhost:%d", port))
require.NoError(t, err)

return server
Expand Down
4 changes: 2 additions & 2 deletions pkg/testutils/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func NewTestAPIServer(t *testing.T) (*api.ApiServer, *sql.DB, func()) {
svr, err := api.NewAPIServer(
ctx,
log,
0, /*port*/
true, /*enableReflection*/
"localhost:0", /*listenAddress*/
true, /*enableReflection*/
serviceRegistrationFunc,
)
require.NoError(t, err)
Expand Down

0 comments on commit 125db4b

Please sign in to comment.