Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
We should not panic when gRPC or HTTP server empty
Browse files Browse the repository at this point in the history
  • Loading branch information
im-kulikov committed Apr 23, 2021
1 parent 34c8019 commit 22d5ea7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
9 changes: 5 additions & 4 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/dig"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
Expand All @@ -18,7 +19,7 @@ type errService struct {
start bool
stop bool

stopError error
*atomic.Error
}

func (e *errService) Start(_ context.Context) error {
Expand All @@ -31,7 +32,7 @@ func (e *errService) Start(_ context.Context) error {

func (e *errService) Stop(context.Context) {
if e.stop {
e.stopError = testError
e.Store(testError)
}
}

Expand Down Expand Up @@ -77,7 +78,7 @@ func TestDefaultApp(t *testing.T) {

t.Run("default application with stop err", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
svc := &errService{stop: true}
svc := &errService{stop: true, Error: atomic.NewError(nil)}

h, err := New(&Settings{},
DefaultApp,
Expand All @@ -92,6 +93,6 @@ func TestDefaultApp(t *testing.T) {

cancel()
require.NoError(t, h.Run())
require.EqualError(t, svc.stopError, testError.Error())
require.EqualError(t, svc.Load(), testError.Error())
})
}
4 changes: 2 additions & 2 deletions group/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestNew(t *testing.T) {
}

{ // should stop on context deadline
ctx, cancel := context.WithTimeout(context.Background(), defaultAwait/3)
ctx, cancel := context.WithTimeout(context.Background(), defaultAwait/4)
cases = append(cases, testCase{
name: "should stop on context deadline",

Expand All @@ -77,7 +77,7 @@ func TestNew(t *testing.T) {

await: defaultAwait,

shutdown: defaultAwait / 3,
shutdown: defaultAwait / 4,
services: []service{
{
shutdown: func(ctx context.Context) { <-ctx.Done() },
Expand Down
6 changes: 5 additions & 1 deletion web/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ func (g *gRPC) Start(context.Context) error {
// Stop tries to stop gRPC service.
func (g *gRPC) Stop(context.Context) {
if g.server == nil {
panic(ErrEmptyGRPCServer)
g.logger.Error("could not stop gRPC server",
zap.String("name", g.name),
zap.Error(ErrEmptyGRPCServer))

return
}

g.server.GracefulStop()
Expand Down
6 changes: 5 additions & 1 deletion web/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ func (s *httpService) Start(context.Context) error {
// if something went wrong.
func (s *httpService) Stop(ctx context.Context) {
if s.server == nil {
panic(ErrEmptyHTTPServer)
s.logger.Error("could not stop http.Server",
zap.String("name", s.name),
zap.Error(ErrEmptyHTTPServer))

return
}

if err := s.catch(s.server.Shutdown(ctx)); err != nil {
Expand Down

0 comments on commit 22d5ea7

Please sign in to comment.