Skip to content

Commit

Permalink
refac: StartServer returns channel for errors encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Dec 6, 2024
1 parent 7d83b63 commit f614271
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 8 additions & 4 deletions pkg/util/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,23 @@ func StartServer(
grpcServer http.Handler,
gwmux http.Handler,
address string,
) (*http.Server, error) {
) (*http.Server, chan error) {
log.Logger.WithContext(ctx).Info("Starting server")
listen, err := net.Listen("tcp", address)
ch := make(chan error, 1)
if err != nil {
return nil, fmt.Errorf("listen server: %w", err)
go func() {
ch <- fmt.Errorf("listen server: %w", err)
}()
return nil, ch
}

httpSrv := &http.Server{
Addr: address,
Handler: GRPCHandlerFunc(grpcServer, gwmux),
}
go func() {
httpSrv.Serve(listen)
ch <- httpSrv.Serve(listen)
}()
return httpSrv, nil
return httpSrv, ch
}
8 changes: 5 additions & 3 deletions pkg/util/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus/hooks/test"

"github.com/ShatteredRealms/go-common-service/pkg/log"
"github.com/ShatteredRealms/go-common-service/pkg/util"
)

Expand All @@ -32,7 +34,7 @@ var _ = Describe("Grpc util", func() {
},
URL: &url.URL{},
}
// log.Logger, hook = test.NewNullLogger()
log.Logger, _ = test.NewNullLogger()
})

Describe("GRPCHandlerFunc", func() {
Expand Down Expand Up @@ -73,12 +75,12 @@ var _ = Describe("Grpc util", func() {
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())
Consistently(err).Should(HaveLen(0))
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())
Eventually(err).Should(HaveLen(1))
Expect(srv).To(BeNil())
})
})
Expand Down

0 comments on commit f614271

Please sign in to comment.