Skip to content

Commit

Permalink
chore: remove global test variables
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior committed Nov 16, 2022
1 parent 458e2a2 commit eb00b02
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 81 deletions.
70 changes: 35 additions & 35 deletions lib/grandpa/finalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
var (
errServicesStopFailed = errors.New("services stop failed")
errvotingRoundHandlerFailed = errors.New("voting round ephemeral failed")
errFinalizationEngineFailed = errors.New("finalisation engine ephemeral failed")
errfinalisationEngineFailed = errors.New("finalisation engine ephemeral failed")
)

type ephemeralService interface {
Expand All @@ -25,7 +25,7 @@ type ephemeralService interface {

type finalisationHandler struct {
servicesLock sync.Mutex
finalizationEngine ephemeralService
finalisationEngine ephemeralService
votingRound ephemeralService

// newServices is a constructor function which takes care to instantiate
Expand All @@ -41,9 +41,9 @@ type finalisationHandler struct {
func newFinalisationHandler(service *Service) *finalisationHandler {
return &finalisationHandler{
newServices: func() (engine, voting ephemeralService) {
finalizationEngine := newFinalizationEngine(service)
votingRound := newvotingRoundHandler(service, finalizationEngine.actionCh)
return finalizationEngine, votingRound
finalisationEngine := newfinalisationEngine(service)
votingRound := newvotingRoundHandler(service, finalisationEngine.actionCh)
return finalisationEngine, votingRound
},
initiateRound: service.initiateRound,
stopCh: make(chan struct{}),
Expand Down Expand Up @@ -89,24 +89,24 @@ func (fh *finalisationHandler) stop() (err error) {
fh.servicesLock.Lock()
defer fh.servicesLock.Unlock()

finalizationEngineErrCh := make(chan error)
finalisationEngineErrCh := make(chan error)
go func() {
finalizationEngineErrCh <- fh.finalizationEngine.Stop()
finalisationEngineErrCh <- fh.finalisationEngine.Stop()
}()

votingRoundErrCh := make(chan error)
go func() {
votingRoundErrCh <- fh.votingRound.Stop()
}()

finalizationEngErr := <-finalizationEngineErrCh
finalisationEngErr := <-finalisationEngineErrCh
votingRoundErr := <-votingRoundErrCh

switch {
case finalizationEngErr != nil && votingRoundErr != nil:
return fmt.Errorf("%w: %s; %s", errServicesStopFailed, finalizationEngErr, votingRoundErr)
case finalizationEngErr != nil:
return fmt.Errorf("%w: %s", errServicesStopFailed, finalizationEngErr)
case finalisationEngErr != nil && votingRoundErr != nil:
return fmt.Errorf("%w: %s; %s", errServicesStopFailed, finalisationEngErr, votingRoundErr)
case finalisationEngErr != nil:
return fmt.Errorf("%w: %s", errServicesStopFailed, finalisationEngErr)
case votingRoundErr != nil:
return fmt.Errorf("%w: %s", errServicesStopFailed, votingRoundErr)
}
Expand All @@ -129,12 +129,12 @@ func (fh *finalisationHandler) Stop() (err error) {
// handler is stopped.
func (fh *finalisationHandler) runEphemeralServices() error {
fh.servicesLock.Lock()
fh.finalizationEngine, fh.votingRound = fh.newServices()
fh.finalisationEngine, fh.votingRound = fh.newServices()
fh.servicesLock.Unlock()

finalizationEngineErr := make(chan error)
finalisationEngineErr := make(chan error)
go func() {
finalizationEngineErr <- fh.finalizationEngine.Run()
finalisationEngineErr <- fh.finalisationEngine.Run()
}()

votingRoundErr := make(chan error)
Expand All @@ -154,15 +154,15 @@ func (fh *finalisationHandler) runEphemeralServices() error {
break
}

stopErr := fh.finalizationEngine.Stop()
stopErr := fh.finalisationEngine.Stop()
if stopErr != nil {
logger.Warnf("stopping finalisation engine: %s", stopErr)
}
return fmt.Errorf("%w: %s", errvotingRoundHandlerFailed, err)

case err := <-finalizationEngineErr:
case err := <-finalisationEngineErr:
if err == nil {
finalizationEngineErr = nil
finalisationEngineErr = nil
// go out from the select case
break
}
Expand All @@ -172,31 +172,31 @@ func (fh *finalisationHandler) runEphemeralServices() error {
logger.Warnf("stopping voting round: %s", stopErr)
}

return fmt.Errorf("%w: %s", errFinalizationEngineFailed, err)
return fmt.Errorf("%w: %s", errfinalisationEngineFailed, err)
}

finish := votingRoundErr == nil && finalizationEngineErr == nil
finish := votingRoundErr == nil && finalisationEngineErr == nil
if finish {
return nil
}
}
}

// votingRoundHandler interacts with finalizationEngine service
// votingRoundHandler interacts with finalisationEngine service
// executing the actions based on what it receives throuhg channel
type votingRoundHandler struct {
grandpaService *Service
finalizationEngineCh <-chan engineAction
finalisationEngineCh <-chan engineAction
stopCh chan struct{}
engineDone chan struct{}
}

func newvotingRoundHandler(service *Service, finalizationEngineCh <-chan engineAction) *votingRoundHandler {
func newvotingRoundHandler(service *Service, finalisationEngineCh <-chan engineAction) *votingRoundHandler {
return &votingRoundHandler{
grandpaService: service,
stopCh: make(chan struct{}),
engineDone: make(chan struct{}),
finalizationEngineCh: finalizationEngineCh,
finalisationEngineCh: finalisationEngineCh,
}
}

Expand All @@ -220,7 +220,7 @@ func (h *votingRoundHandler) Run() (err error) {
case <-h.stopCh:
return nil

case action := <-h.finalizationEngineCh:
case action := <-h.finalisationEngineCh:
switch action {
case determinePrevote:
isPrimary, err := h.grandpaService.handleIsPrimary()
Expand Down Expand Up @@ -309,31 +309,31 @@ const (
finalize
)

type finalizationEngine struct {
type finalisationEngine struct {
grandpaService *Service
stopCh chan struct{}
engineDone chan struct{}
actionCh chan engineAction
}

func newFinalizationEngine(service *Service) *finalizationEngine {
return &finalizationEngine{
func newfinalisationEngine(service *Service) *finalisationEngine {
return &finalisationEngine{
grandpaService: service,
actionCh: make(chan engineAction),
stopCh: make(chan struct{}),
engineDone: make(chan struct{}),
}
}

func (f *finalizationEngine) Stop() (err error) {
func (f *finalisationEngine) Stop() (err error) {
close(f.stopCh)
<-f.engineDone

close(f.actionCh)
return nil
}

func (f *finalizationEngine) Run() (err error) {
func (f *finalisationEngine) Run() (err error) {
defer close(f.engineDone)

err = f.defineRoundVotes()
Expand All @@ -349,7 +349,7 @@ func (f *finalizationEngine) Run() (err error) {
return nil
}

func (f *finalizationEngine) defineRoundVotes() error {
func (f *finalisationEngine) defineRoundVotes() error {
gossipInterval := f.grandpaService.interval
determinePrevoteTimer := time.NewTimer(2 * gossipInterval)
determinePrecommitTimer := time.NewTimer(4 * gossipInterval)
Expand Down Expand Up @@ -432,10 +432,10 @@ func (f *finalizationEngine) defineRoundVotes() error {
return nil
}

func (f *finalizationEngine) finalizeRound() error {
func (f *finalisationEngine) finalizeRound() error {
gossipInterval := f.grandpaService.interval
attemptFinalizationTicker := time.NewTicker(gossipInterval / 2)
defer attemptFinalizationTicker.Stop()
attemptfinalisationTicker := time.NewTicker(gossipInterval / 2)
defer attemptfinalisationTicker.Stop()

for {
completable, err := f.grandpaService.checkRoundCompletable()
Expand All @@ -461,7 +461,7 @@ func (f *finalizationEngine) finalizeRound() error {
select {
case <-f.stopCh:
return nil
case <-attemptFinalizationTicker.C:
case <-attemptfinalisationTicker.C:
}
}
}
2 changes: 1 addition & 1 deletion lib/grandpa/finalization_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_finalisationHandler_Stop_ShouldHalt_Services(t *testing.T) {
"halt_ephemeral_services_after_calling_stop": {
// when we start the finalisation handler we instantiate
// and call the Run method from each ephemeral services
// (votingHandler, finalizationEngine) since they are mocked
// (votingHandler, finalisationEngine) since they are mocked
// they will wait until the Stop method being called to release
// the blocking channel and return from the function
newHandler: func(ctrl *gomock.Controller) *finalisationHandler {
Expand Down
6 changes: 3 additions & 3 deletions lib/grandpa/finalization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func Test_finalisationHandler_runEphemeralServices(t *testing.T) {
},

"engine_fails_should_stop_voting_round_service": {
errString: "finalisation engine ephemeral failed: mocked finalization engine failed",
wantErr: errFinalizationEngineFailed,
errString: "finalisation engine ephemeral failed: mocked finalisation engine failed",
wantErr: errfinalisationEngineFailed,
createfinalisationHandler: func(ctrl *gomock.Controller) *finalisationHandler {
builder := func() (engine ephemeralService, voting ephemeralService) {
mockEngine := NewMockephemeralService(ctrl)
mockEngine.EXPECT().Run().DoAndReturn(func() error {
return errors.New("mocked finalization engine failed")
return errors.New("mocked finalisation engine failed")
})

// once the finalisation engine fails the finalisation handler
Expand Down
Loading

0 comments on commit eb00b02

Please sign in to comment.