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

Refactor tests for GRPC check #88

Merged
merged 2 commits into from
Feb 21, 2023
Merged
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
128 changes: 56 additions & 72 deletions checks/grpc/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package grpc

import (
"context"
"log"
"net"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,77 +11,63 @@ import (
"google.golang.org/grpc/health/grpc_health_v1"
)

const (
addr = ":8080"
service = "HealthTest"
)

var healthServer *health.Server

func TestMain(m *testing.M) {
healthServer = health.NewServer()
healthServer.SetServingStatus(service, grpc_health_v1.HealthCheckResponse_SERVING)

lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("error setting up tcp listener: %v", err)
}

server := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(server, healthServer)

go func() {
if err := server.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()

defer server.Stop()

os.Exit(m.Run())
}

func TestNew_WithServingStatusServing(t *testing.T) {
healthServer.SetServingStatus(service, grpc_health_v1.HealthCheckResponse_SERVING)

check := New(Config{
Target: addr,
Service: service,
DialOptions: []grpc.DialOption{
grpc.WithInsecure(),
func TestNew(t *testing.T) {
for name, tc := range map[string]struct {
servingStatus grpc_health_v1.HealthCheckResponse_ServingStatus
requireError bool
}{
"serving": {
servingStatus: grpc_health_v1.HealthCheckResponse_SERVING,
requireError: false,
},
})

err := check(context.Background())
require.NoError(t, err)
}

func TestNew_WithServingStatusUnknown(t *testing.T) {
healthServer.SetServingStatus(service, grpc_health_v1.HealthCheckResponse_UNKNOWN)

check := New(Config{
Target: addr,
Service: service,
DialOptions: []grpc.DialOption{
grpc.WithInsecure(),
"unknown": {
servingStatus: grpc_health_v1.HealthCheckResponse_UNKNOWN,
requireError: true,
},
})

err := check(context.Background())
require.Error(t, err)
}

func TestNew_WithServingStatusNotServing(t *testing.T) {
healthServer.SetServingStatus(service, grpc_health_v1.HealthCheckResponse_NOT_SERVING)

check := New(Config{
Target: addr,
Service: service,
DialOptions: []grpc.DialOption{
grpc.WithInsecure(),
"not serving": {
servingStatus: grpc_health_v1.HealthCheckResponse_NOT_SERVING,
requireError: true,
},
})

err := check(context.Background())
require.Error(t, err)
} {
servingStatus := tc.servingStatus
requireError := tc.requireError

t.Run(name, func(t *testing.T) {
t.Parallel()

const service = "HealthTest"

healthServer := health.NewServer()
healthServer.SetServingStatus(service, servingStatus)

lis, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)

server := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(server, healthServer)

go func() {
if err := server.Serve(lis); err != nil {
t.Log("Failed to serve GRPC", err)
}
}()
defer server.Stop()

check := New(Config{
Target: lis.Addr().String(),
Service: service,
DialOptions: []grpc.DialOption{
grpc.WithInsecure(),
},
})

err = check(context.Background())

if requireError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}