2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-04-21 16:56:18 +00:00
|
|
|
package testutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GRPCService interface {
|
|
|
|
Register(*grpc.Server)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunTestServer(t *testing.T, services ...GRPCService) net.Addr {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
for _, svc := range services {
|
|
|
|
svc.Register(grpcServer)
|
|
|
|
}
|
|
|
|
|
|
|
|
go grpcServer.Serve(lis)
|
|
|
|
t.Cleanup(grpcServer.Stop)
|
|
|
|
|
|
|
|
return lis.Addr()
|
|
|
|
}
|