Skip to content

[baseserver] Helper for starting server in tests #9436

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

Merged
merged 1 commit into from
Apr 21, 2022
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
26 changes: 4 additions & 22 deletions components/common-go/baseserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,22 @@ import (
"github.com/stretchr/testify/require"
"net/http"
"testing"
"time"
)

func TestServer_StartStop(t *testing.T) {
// We don't use the helper NewForTests, because we want to control stopping ourselves.
srv, err := baseserver.New("server_test", baseserver.WithHTTPPort(8765), baseserver.WithGRPCPort(8766))
require.NoError(t, err)
baseserver.StartServerForTests(t, srv)

go func() {
require.NoError(t, srv.ListenAndServe())
}()

baseserver.WaitForServerToBeReachable(t, srv, 3*time.Second)
require.Equal(t, "http://localhost:8765", srv.HTTPAddress())
require.Equal(t, "localhost:8766", srv.GRPCAddress())
require.NoError(t, srv.Close())
}

func TestServer_ServesReady(t *testing.T) {
srv := baseserver.NewForTests(t)

go func(t *testing.T) {
require.NoError(t, srv.ListenAndServe())
}(t)

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

readyURL := fmt.Sprintf("%s/ready", srv.HTTPAddress())
resp, err := http.Get(readyURL)
Expand All @@ -47,11 +37,7 @@ func TestServer_ServesReady(t *testing.T) {
func TestServer_ServesMetricsEndpointWithDefaultConfig(t *testing.T) {
srv := baseserver.NewForTests(t)

go func(t *testing.T) {
require.NoError(t, srv.ListenAndServe())
}(t)

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

readyUR := fmt.Sprintf("%s/metrics", srv.HTTPAddress())
resp, err := http.Get(readyUR)
Expand All @@ -65,11 +51,7 @@ func TestServer_ServesMetricsEndpointWithCustomMetricsConfig(t *testing.T) {
baseserver.WithMetricsRegistry(registry),
)

go func(t *testing.T) {
require.NoError(t, srv.ListenAndServe())
}(t)

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

readyUR := fmt.Sprintf("%s/metrics", srv.HTTPAddress())
resp, err := http.Get(readyUR)
Expand Down
15 changes: 14 additions & 1 deletion components/common-go/baseserver/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ func NewForTests(t *testing.T, opts ...Option) *Server {
return srv
}

func WaitForServerToBeReachable(t *testing.T, srv *Server, timeout time.Duration) {
// StartServerForTests starts the server for test purposes.
// This is a helper which also ensures the server is reachable before returning.
func StartServerForTests(t *testing.T, srv *Server) {
t.Helper()

go func() {
err := srv.ListenAndServe()
require.NoError(t, err)
}()

waitForServerToBeReachable(t, srv, 3*time.Second)
}

func waitForServerToBeReachable(t *testing.T, srv *Server, timeout time.Duration) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand Down
9 changes: 2 additions & 7 deletions components/public-api-server/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"testing"
"time"
)

func TestPublicAPIServer(t *testing.T) {
ctx := context.Background()
srv := baseserver.NewForTests(t)
require.NoError(t, register(srv))

go func() {
require.NoError(t, srv.ListenAndServe())
}()

baseserver.WaitForServerToBeReachable(t, srv, 1*time.Second)
require.NoError(t, register(srv))
baseserver.StartServerForTests(t, srv)

conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.NoError(t, err)
Expand Down