This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from im-kulikov/test-coverage
Increase test coverage and
- Loading branch information
Showing
7 changed files
with
305 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package web | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestGRPCService(t *testing.T) { | ||
t.Run("should set network", func(t *testing.T) { | ||
serve, err := NewGRPCService( | ||
grpc.NewServer(), | ||
GRPCSkipErrors(), | ||
GRPCListenAddress(":8080"), | ||
GRPCListenNetwork("test")) | ||
require.NoError(t, err) | ||
|
||
s, ok := serve.(*gRPC) | ||
require.True(t, ok) | ||
require.Equal(t, "test", s.network) | ||
}) | ||
|
||
t.Run("should fail on empty address", func(t *testing.T) { | ||
serve, err := NewGRPCService(grpc.NewServer()) | ||
require.Nil(t, serve) | ||
require.EqualError(t, err, ErrEmptyGRPCAddress.Error()) | ||
}) | ||
|
||
t.Run("should fail on empty server", func(t *testing.T) { | ||
serve, err := NewGRPCService(nil) | ||
require.Nil(t, serve) | ||
require.EqualError(t, err, ErrEmptyGRPCServer.Error()) | ||
}) | ||
|
||
t.Run("should fail on Start and Stop", func(t *testing.T) { | ||
require.EqualError(t, (&gRPC{}).Start(), ErrEmptyGRPCServer.Error()) | ||
require.EqualError(t, (&gRPC{}).Stop(), ErrEmptyGRPCServer.Error()) | ||
}) | ||
|
||
t.Run("should fail on net.Listen", func(t *testing.T) { | ||
require.EqualError(t, (&gRPC{server: grpc.NewServer()}).Start(), "listen: unknown network ") | ||
}) | ||
|
||
t.Run("shoud ignore ErrServerStopped", func(t *testing.T) { | ||
lis, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
require.NoError(t, lis.Close()) | ||
|
||
serve, err := NewGRPCService( | ||
grpc.NewServer(), | ||
GRPCSkipErrors(), | ||
GRPCListenAddress(lis.Addr().String())) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, serve.Stop()) | ||
require.NoError(t, serve.Start()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package web | ||
|
||
import ( | ||
"crypto/tls" | ||
"errors" | ||
"github.com/stretchr/testify/require" | ||
"net" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestHTTPService(t *testing.T) { | ||
t.Run("should set network", func(t *testing.T) { | ||
serve, err := NewHTTPService( | ||
&http.Server{}, | ||
HTTPSkipErrors(), | ||
HTTPListenAddress(":8080"), | ||
HTTPListenNetwork("test")) | ||
require.NoError(t, err) | ||
|
||
s, ok := serve.(*httpService) | ||
require.True(t, ok) | ||
require.Equal(t, "test", s.network) | ||
}) | ||
|
||
t.Run("should fail on empty address", func(t *testing.T) { | ||
serve, err := NewHTTPService(&http.Server{}) | ||
require.Nil(t, serve) | ||
require.EqualError(t, err, ErrEmptyHTTPAddress.Error()) | ||
}) | ||
|
||
t.Run("should fail on empty server", func(t *testing.T) { | ||
serve, err := NewHTTPService(nil) | ||
require.Nil(t, serve) | ||
require.EqualError(t, err, ErrEmptyHTTPServer.Error()) | ||
}) | ||
|
||
t.Run("should fail on Start and Stop", func(t *testing.T) { | ||
require.EqualError(t, (&httpService{}).Start(), ErrEmptyHTTPServer.Error()) | ||
require.EqualError(t, (&httpService{}).Stop(), ErrEmptyHTTPServer.Error()) | ||
}) | ||
|
||
t.Run("should fail on net.Listen", func(t *testing.T) { | ||
require.EqualError(t, (&httpService{server: &http.Server{}}).Start(), "listen: unknown network ") | ||
}) | ||
|
||
t.Run("should not fail for tls", func(t *testing.T) { | ||
lis, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err) | ||
require.NoError(t, lis.Close()) | ||
|
||
s := &http.Server{ | ||
TLSConfig: &tls.Config{ | ||
GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
return nil, errors.New("test") | ||
}, | ||
}, | ||
} | ||
|
||
serve, err := NewHTTPService(s, HTTPListenAddress(lis.Addr().String())) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, serve.Start()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package web | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type fakeListener struct { | ||
startError error | ||
stopError error | ||
} | ||
|
||
func (f fakeListener) ListenAndServe() error { | ||
return f.startError | ||
} | ||
|
||
func (f fakeListener) Shutdown(context.Context) error { | ||
return f.stopError | ||
} | ||
|
||
func TestListenerService(t *testing.T) { | ||
t.Run("should set network", func(t *testing.T) { | ||
serve, err := NewListener( | ||
&fakeListener{}, | ||
ListenerIgnoreError(ErrEmptyListener), | ||
ListenerSkipErrors(), | ||
ListenerShutdownTimeout(time.Second)) | ||
require.NoError(t, err) | ||
|
||
s, ok := serve.(*listener) | ||
require.True(t, ok) | ||
require.True(t, s.skipErrors) | ||
require.Equal(t, time.Second, s.shutdownTimeout) | ||
require.Equal(t, ErrEmptyListener, s.ignoreErrors[0]) | ||
}) | ||
|
||
t.Run("should fail on empty server", func(t *testing.T) { | ||
serve, err := NewListener(nil) | ||
require.Nil(t, serve) | ||
require.EqualError(t, err, ErrEmptyListener.Error()) | ||
}) | ||
|
||
t.Run("should fail on Start and Stop", func(t *testing.T) { | ||
require.EqualError(t, (&listener{}).Start(), ErrEmptyListener.Error()) | ||
require.EqualError(t, (&listener{}).Stop(), ErrEmptyListener.Error()) | ||
}) | ||
|
||
t.Run("should successfully start and stop", func(t *testing.T) { | ||
require.NoError(t, (&listener{server: &fakeListener{}}).Start()) | ||
require.NoError(t, (&listener{server: &fakeListener{}}).Stop()) | ||
}) | ||
|
||
t.Run("should skip errors", func(t *testing.T) { | ||
s := &fakeListener{stopError: errors.New("stopping")} | ||
serve, err := NewListener(s, ListenerSkipErrors()) | ||
require.NoError(t, err) | ||
require.NoError(t, serve.Stop()) | ||
}) | ||
|
||
t.Run("should ignore errors", func(t *testing.T) { | ||
s := &fakeListener{stopError: ErrEmptyListener} | ||
serve, err := NewListener(s, ListenerIgnoreError(ErrEmptyListener)) | ||
require.NoError(t, err) | ||
require.NoError(t, serve.Stop()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package web | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"testing" | ||
) | ||
|
||
type fakeService struct { | ||
startError error | ||
stopError error | ||
} | ||
|
||
func (f fakeService) Start() error { | ||
return f.startError | ||
} | ||
|
||
func (f fakeService) Stop() error { | ||
return f.stopError | ||
} | ||
|
||
func TestMultiService(t *testing.T) { | ||
t.Run("fail on empty logger", func(t *testing.T) { | ||
svc, err := New(nil) | ||
require.Nil(t, svc) | ||
require.EqualError(t, err, ErrEmptyLogger.Error()) | ||
}) | ||
|
||
t.Run("fail on empty services", func(t *testing.T) { | ||
svc, err := New(zap.L(), nil, nil) | ||
require.Nil(t, svc) | ||
require.EqualError(t, err, ErrEmptyServices.Error()) | ||
}) | ||
|
||
t.Run("should fail on start and return first error", func(t *testing.T) { | ||
svc, err := New(zap.L(), | ||
&fakeService{startError: ErrEmptyServices}, | ||
&fakeService{startError: ErrEmptyLogger}) | ||
require.NoError(t, err) | ||
require.EqualError(t, svc.Start(), ErrEmptyServices.Error()) | ||
}) | ||
|
||
t.Run("should fail on stop and return last error", func(t *testing.T) { | ||
l, err := zap.NewDevelopment() | ||
require.NoError(t, err) | ||
|
||
svc, err := New(l, | ||
&fakeService{stopError: ErrEmptyServices}, | ||
&fakeService{stopError: ErrEmptyLogger}) | ||
require.NoError(t, err) | ||
require.EqualError(t, svc.Stop(), ErrEmptyLogger.Error()) | ||
}) | ||
} |