From 7d83b6327c06ef2c5d324e2f0ad4451e35a89b2c Mon Sep 17 00:00:00 2001 From: Wil Simpson Date: Thu, 5 Dec 2024 18:52:33 -0500 Subject: [PATCH] feat: updated testing. changed srv to start listenng in new routine and return server that can be cancelled --- pkg/util/grpc.go | 15 ++++++++------- pkg/util/grpc_test.go | 19 ++++++++++++------- pkg/util/misc_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 pkg/util/misc_test.go diff --git a/pkg/util/grpc.go b/pkg/util/grpc.go index 0d527af..98dd805 100644 --- a/pkg/util/grpc.go +++ b/pkg/util/grpc.go @@ -8,11 +8,9 @@ import ( "strings" "github.com/ShatteredRealms/go-common-service/pkg/log" - "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" - "google.golang.org/grpc" ) func GRPCHandlerFunc(grpcServer http.Handler, otherHandler http.Handler) http.Handler { @@ -34,19 +32,22 @@ func GRPCHandlerFunc(grpcServer http.Handler, otherHandler http.Handler) http.Ha func StartServer( ctx context.Context, - grpcServer *grpc.Server, - gwmux *runtime.ServeMux, + grpcServer http.Handler, + gwmux http.Handler, address string, -) error { +) (*http.Server, error) { log.Logger.WithContext(ctx).Info("Starting server") listen, err := net.Listen("tcp", address) if err != nil { - return fmt.Errorf("listen server: %w", err) + return nil, fmt.Errorf("listen server: %w", err) } httpSrv := &http.Server{ Addr: address, Handler: GRPCHandlerFunc(grpcServer, gwmux), } - return httpSrv.Serve(listen) + go func() { + httpSrv.Serve(listen) + }() + return httpSrv, nil } diff --git a/pkg/util/grpc_test.go b/pkg/util/grpc_test.go index f1a4e95..18beb00 100644 --- a/pkg/util/grpc_test.go +++ b/pkg/util/grpc_test.go @@ -70,13 +70,18 @@ var _ = Describe("Grpc util", func() { }) // @TODO: Find way to test without race conditions - // Describe("StartServer", func() { - // It("should start a server", func() { - // ctx := context.Background() - // listener := util.StartServer(ctx, grpcServer, httpServer, "127.0.0.1:9999") - // Expect(listener.Addr().String()).To(Equal("127.0.0.1:9999")) - // }) - // }) + Describe("StartServer", func() { + It("should start a server", func(ctx SpecContext) { + srv, err := util.StartServer(ctx, grpcServer, httpServer, "127.0.0.1:9999") + Expect(err).NotTo(HaveOccurred()) + Expect(srv).NotTo(BeNil()) + }) + It("should error if invalid host is given", func(ctx SpecContext) { + srv, err := util.StartServer(ctx, grpcServer, httpServer, ".0.1:9999") + Expect(err).To(HaveOccurred()) + Expect(srv).To(BeNil()) + }) + }) }) type fakeHttpHandler struct { diff --git a/pkg/util/misc_test.go b/pkg/util/misc_test.go new file mode 100644 index 0000000..80beebe --- /dev/null +++ b/pkg/util/misc_test.go @@ -0,0 +1,27 @@ +package util_test + +import ( + "github.com/WilSimpson/gocloak/v13" + "github.com/go-faker/faker/v4" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/ShatteredRealms/go-common-service/pkg/util" +) + +var _ = Describe("Misc", func() { + Describe("RegisterRole", func() { + It("should append the role to the roles slice", func() { + roles := []*gocloak.Role{} + role := &gocloak.Role{} + role2 := &gocloak.Role{} + Expect(faker.FakeData(role)).To(Succeed()) + Expect(faker.FakeData(role2)).To(Succeed()) + Expect(util.RegisterRole(role, &roles)).To(Equal(role)) + Expect(roles).To(ContainElement(role)) + Expect(util.RegisterRole(role2, &roles)).To(Equal(role2)) + Expect(roles).To(ContainElement(role2)) + Expect(roles).To(HaveLen(2)) + }) + }) +})