Skip to content

Commit

Permalink
Fix race in test (#22)
Browse files Browse the repository at this point in the history
* Fix race in test

* make codecov optional

* make codecov optional
  • Loading branch information
crabio authored Aug 31, 2021
1 parent 2928547 commit 72ff207
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.txt
flags: unittests
fail_ci_if_error: true
fail_ci_if_error: false
verbose: true
lint:
name: Lint
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ lint:

test:
@echo "Run unit tests"
go test -v ./... -coverprofile coverage.txt -covermode atomic
go test --race -v ./... -coverprofile coverage.txt -covermode atomic
@echo "Code coverage"
go tool cover -func coverage.txt

Expand Down
24 changes: 23 additions & 1 deletion pkg/api/health/v1/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
// External

"context"
"errors"
"sync"
"testing"
"time"

Expand All @@ -22,11 +24,22 @@ import (

type MockhWatchServer struct {
MsgCount uint32

sync.Mutex
Close bool

grpc.ServerStream
}

func (s *MockhWatchServer) Send(m *health_v1.HealthCheckResponse) error {
s.Lock()
s.MsgCount += 1
needClose := s.Close
s.Unlock()

if needClose {
return errors.New("connection is closed")
}
return nil
}

Expand All @@ -51,11 +64,20 @@ func TestWatch(t *testing.T) {
func testWatch(t *testing.T, s *api_health_v1.HealthServiceServer, r *health_v1.HealthCheckRequest) {
mockWatchServer := MockhWatchServer{}

exit := make(chan bool)
go func() {
assert.NoError(t, s.Watch(r, &mockWatchServer))
assert.Error(t, s.Watch(r, &mockWatchServer))
exit <- true
}()

time.Sleep(time.Millisecond * 250)

// Close Mock connection
mockWatchServer.Lock()
mockWatchServer.Close = true
assert.Equal(t, uint32(3), mockWatchServer.MsgCount)
mockWatchServer.Unlock()

// Wait exit
<-exit
}
13 changes: 7 additions & 6 deletions pkg/grpc_proxy_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ func New(config *config.Config, grpcServer *grpc_server.Server, services []servi
return nil, err
}

// Create a client connection to the gRPC server
ps.grpcConn, err = ps.createGrpcProxyConnection()
if err != nil {
return nil, err
}

// Create mux router to route HTTP requests in server
mux := createGrpcProxyMuxServer()

Expand Down Expand Up @@ -101,6 +95,13 @@ func createGrpcProxyMuxServer() *runtime.ServeMux {

// Function runs gRPC proxy server on the [port]
func (ps *Server) Run() error {
var err error
// Create a client connection to the gRPC server
ps.grpcConn, err = ps.createGrpcProxyConnection()
if err != nil {
return err
}

go func() {
if err := ps.httpServer.ListenAndServe(); err != nil {
ps.log.WithError(err).Fatal("Couldn't serve gRPC-Gateway server")
Expand Down
2 changes: 0 additions & 2 deletions pkg/grpc_proxy_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func TestNew(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, grpcs)

assert.NoError(t, grpcs.Run())

s, err := grpc_proxy_server.New(c.Config, grpcs, []service.IServiceServer{})
assert.NoError(t, err)
assert.NotNil(t, s)
Expand Down
3 changes: 2 additions & 1 deletion pkg/swagger/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"text/template"

// Internal
"github.com/iakrevetkho/archaeopteryx/docs"
docs_swagger "github.com/iakrevetkho/archaeopteryx/docs/swagger"
)

func (s *Server) createFsHandler() http.Handler {
return http.FileServer(http.Dir(s.config.Docs.SwaggerDir))
return http.FileServer(http.FS(docs.Swagger))
}

func (s *Server) createHomePageHandler() (http.Handler, error) {
Expand Down
33 changes: 19 additions & 14 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ type Server struct {
log *logrus.Entry
controllers *api_data.Controllers

services []service.IServiceServer
services []service.IServiceServer
grpcServer *grpc_server.Server
grpcProxyServer *grpc_proxy_server.Server
}

func New(config *config.Config, externalServices []service.IServiceServer) *Server {
func New(config *config.Config, externalServices []service.IServiceServer) (*Server, error) {
var err error

s := new(Server)

helpers.InitLogger(config)
Expand All @@ -47,25 +51,26 @@ func New(config *config.Config, externalServices []service.IServiceServer) *Serv
// Add external services
s.services = append(s.services, externalServices...)

return s
}
s.grpcServer, err = grpc_server.New(s.Config.GrpcPort, s.controllers, s.services)
if err != nil {
return nil, fmt.Errorf("couldn't create gRPC server. " + err.Error())
}

func (s *Server) Run() error {
grpcServer, err := grpc_server.New(s.Config.GrpcPort, s.controllers, s.services)
s.grpcProxyServer, err = grpc_proxy_server.New(s.Config, s.grpcServer, s.services)
if err != nil {
return fmt.Errorf("couldn't create gRPC server. " + err.Error())
return nil, fmt.Errorf("couldn't create gRPC proxy server. " + err.Error())
}

return s, nil
}

func (s *Server) Run() error {
// Run gRPC server before creating gRPC proxy to allow gRPC proxy dial connection with gRPC
if err := grpcServer.Run(); err != nil {
if err := s.grpcServer.Run(); err != nil {
return fmt.Errorf("couldn't run gRPC server. " + err.Error())
}

grpcProxyServer, err := grpc_proxy_server.New(s.Config, grpcServer, s.services)
if err != nil {
return fmt.Errorf("couldn't create gRPC proxy server. " + err.Error())
}

if err := grpcProxyServer.Run(); err != nil {
if err := s.grpcProxyServer.Run(); err != nil {
return fmt.Errorf("couldn't run gRPC proxy server. " + err.Error())
}

Expand Down

0 comments on commit 72ff207

Please sign in to comment.