Skip to content

Commit

Permalink
cleanup of code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Dijk committed Sep 13, 2016
1 parent bc19d8a commit dcc3b97
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions swim/gossip.go
Expand Up @@ -131,8 +131,8 @@ func (g *gossip) Start() {
g.state.running = true

// schedule repeat execution in the background
g.state.protocolPeriodStop, g.state.protocolPeriodWait = schedule(g.ProtocolPeriod, g.ComputeProtocolDelay, g.node.clock)
g.state.protocolRateChannel, _ = schedule(g.AdjustProtocolRate, func() time.Duration {
g.state.protocolPeriodStop, g.state.protocolPeriodWait = scheduleRepeaditly(g.ProtocolPeriod, g.ComputeProtocolDelay, g.node.clock)
g.state.protocolRateChannel, _ = scheduleRepeaditly(g.AdjustProtocolRate, func() time.Duration {
return time.Second
}, g.node.clock)

Expand Down
1 change: 0 additions & 1 deletion swim/memberlist_test.go
Expand Up @@ -25,7 +25,6 @@ import (
"testing"

"github.com/benbjohnson/clock"

"github.com/stretchr/testify/suite"
"github.com/uber/ringpop-go/util"
)
Expand Down
16 changes: 12 additions & 4 deletions swim/schedule.go
Expand Up @@ -6,8 +6,18 @@ import (
"github.com/benbjohnson/clock"
)

// TODO rename to scheduleRepeaditly
func schedule(what func(), delayFn func() time.Duration, clock clock.Clock) (stop chan bool, wait <-chan bool) {
// scheduleRepeaditly runs a function in the background continually with a
// dynamically computed delay in between invocations. The returned channels can
// be used to stop the background execution and a channel that will be unblocked
// when the background task is completely stopped.
//
// stop, wait := scheduleRepeaditly(func(){..}, func() time.Duration { return time.Second }, clock)
// time.sleep(time.Duration(10) * time.Second)
// // stop the background task
// close(stop)
// // wait till the background task is terminated
// <-wait
func scheduleRepeaditly(what func(), delayFn func() time.Duration, clock clock.Clock) (stop chan bool, wait <-chan bool) {
stop = make(chan bool)

internalWait := make(chan bool)
Expand All @@ -28,5 +38,3 @@ func schedule(what func(), delayFn func() time.Duration, clock clock.Clock) (sto

return stop, wait
}

// TODO add schedule once with a quit channel to cancel it for use in the update rollup
12 changes: 6 additions & 6 deletions swim/state_transitions_test.go
Expand Up @@ -110,28 +110,28 @@ func (s *StateTransitionsSuite) TestSuspectDisabled() {
func (s *StateTransitionsSuite) TestSuspectBecomesFaulty() {
s.m.MakeSuspect(s.suspect.Address, s.suspect.Incarnation)
member, _ := s.m.Member(s.suspect.Address)
s.Require().NotNil(member, "expected cannot be nil")
s.Require().NotNil(member, "expected member, cannot be nil")

s.stateTransitions.ScheduleSuspectToFaulty(*member)
s.NotNil(s.stateTransitions.timer(member.Address), "expected state transtition timer to be set")

s.clock.Add(5 * time.Second)
member, _ = s.m.Member(s.suspect.Address)
s.Require().NotNil(member, "expected cannot be nil")
s.Require().NotNil(member, "expected member, cannot be nil")
s.Equal(Faulty, member.Status, "expected member to be faulty")
}

func (s *StateTransitionsSuite) TestFaultyBecomesTombstone() {
s.m.MakeFaulty(s.suspect.Address, s.suspect.Incarnation)
member, _ := s.m.Member(s.suspect.Address)
s.Require().NotNil(member, "expected cannot be nil")
s.Require().NotNil(member, "expected member, cannot be nil")

s.stateTransitions.ScheduleFaultyToTombstone(*member)
s.NotNil(s.stateTransitions.timer(member.Address), "expected state transtition timer to be set")

s.clock.Add(10 * time.Second)
member, _ = s.m.Member(s.suspect.Address)
s.Require().NotNil(member, "expected cannot be nil")
s.Require().NotNil(member, "expected member, cannot be nil")
s.Equal(Tombstone, member.Status, "expected member to be tombstone")
}

Expand All @@ -140,7 +140,7 @@ func (s *StateTransitionsSuite) TestTombstoneBecomesEvicted() {
s.m.MakeAlive(s.suspect.Address, s.suspect.Incarnation)
s.m.MakeTombstone(s.suspect.Address, s.suspect.Incarnation)
member, _ := s.m.Member(s.suspect.Address)
s.Require().NotNil(member, "expected cannot be nil")
s.Require().NotNil(member, "expected member, cannot be nil")

s.stateTransitions.ScheduleTombstoneToEvict(*member)
s.NotNil(s.stateTransitions.timer(member.Address), "expected state transtition timer to be set")
Expand All @@ -156,7 +156,7 @@ func (s *StateTransitionsSuite) TestTombstoneBecomesEvicted() {
func (s *StateTransitionsSuite) TestTimerCreated() {
s.m.MakeAlive(s.suspect.Address, s.suspect.Incarnation)
member, _ := s.m.Member(s.suspect.Address)
s.Require().NotNil(member, "expected cannot be nil")
s.Require().NotNil(member, "expected member, cannot be nil")

old := s.stateTransitions.timer(member.Address)
s.Require().Nil(old, "expected timer to be nil")
Expand Down

0 comments on commit dcc3b97

Please sign in to comment.