Browse Source

remove bool from Service#Reset

pull/1842/head
Anton Kaliaev 7 years ago
parent
commit
c2fcc093b2
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 43 additions and 12 deletions
  1. +5
    -4
      common/service.go
  2. +38
    -8
      common/service_test.go

+ 5
- 4
common/service.go View File

@ -2,6 +2,7 @@ package common
import (
"errors"
"fmt"
"sync/atomic"
"github.com/tendermint/tmlibs/log"
@ -19,7 +20,7 @@ type Service interface {
Stop() error
OnStop()
Reset() (bool, error)
Reset() error
OnReset() error
IsRunning() bool
@ -145,17 +146,17 @@ func (bs *BaseService) Stop() error {
func (bs *BaseService) OnStop() {}
// Implements Service
func (bs *BaseService) Reset() (bool, error) {
func (bs *BaseService) Reset() error {
if !atomic.CompareAndSwapUint32(&bs.stopped, 1, 0) {
bs.Logger.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl)
return false, nil
return fmt.Errorf("can't reset running %s", bs.name)
}
// whether or not we've started, we can reset
atomic.CompareAndSwapUint32(&bs.started, 1, 0)
bs.Quit = make(chan struct{})
return true, bs.impl.OnReset()
return bs.impl.OnReset()
}
// Implements Service


+ 38
- 8
common/service_test.go View File

@ -2,23 +2,53 @@ package common
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestBaseServiceWait(t *testing.T) {
type testService struct {
BaseService
}
type TestService struct {
BaseService
}
ts := &TestService{}
func (testService) OnReset() error {
return nil
}
func TestBaseServiceWait(t *testing.T) {
ts := &testService{}
ts.BaseService = *NewBaseService(nil, "TestService", ts)
ts.Start()
waitFinished := make(chan struct{})
go func() {
ts.Stop()
ts.Wait()
waitFinished <- struct{}{}
}()
for i := 0; i < 10; i++ {
ts.Wait()
go ts.Stop()
select {
case <-waitFinished:
// all good
case <-time.After(100 * time.Millisecond):
t.Fatal("expected Wait() to finish within 100 ms.")
}
}
func TestBaseServiceReset(t *testing.T) {
ts := &testService{}
ts.BaseService = *NewBaseService(nil, "TestService", ts)
ts.Start()
err := ts.Reset()
require.Error(t, err, "expected cant reset service error")
ts.Stop()
err = ts.Reset()
require.NoError(t, err)
err = ts.Start()
require.NoError(t, err)
}

Loading…
Cancel
Save