Skip to content

Commit

Permalink
feat: updated testing. changed srv to start listenng in new routine a…
Browse files Browse the repository at this point in the history
…nd return server that can be cancelled
  • Loading branch information
Wil Simpson committed Dec 5, 2024
1 parent a591fd0 commit 7d83b63
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
15 changes: 8 additions & 7 deletions pkg/util/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
19 changes: 12 additions & 7 deletions pkg/util/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions pkg/util/misc_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})

0 comments on commit 7d83b63

Please sign in to comment.