Skip to content

Commit

Permalink
Shorten scheduler/factory test by making backoff a struct var
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Jan 9, 2015
1 parent e67786b commit b38c25e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
19 changes: 12 additions & 7 deletions plugin/pkg/scheduler/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ func (f *ConfigFactory) CreateFromKeys(predicateKeys, priorityKeys util.StringSe
podBackoff := podBackoff{
perPodBackoff: map[string]*backoffEntry{},
clock: realClock{},

defaultDuration: 1 * time.Second,
maxDuration: 60 * time.Second,
}

return &scheduler.Config{
Expand Down Expand Up @@ -245,17 +248,19 @@ type backoffEntry struct {
}

type podBackoff struct {
perPodBackoff map[string]*backoffEntry
lock sync.Mutex
clock clock
perPodBackoff map[string]*backoffEntry
lock sync.Mutex
clock clock
defaultDuration time.Duration
maxDuration time.Duration
}

func (p *podBackoff) getEntry(podID string) *backoffEntry {
p.lock.Lock()
defer p.lock.Unlock()
entry, ok := p.perPodBackoff[podID]
if !ok {
entry = &backoffEntry{backoff: 1 * time.Second}
entry = &backoffEntry{backoff: p.defaultDuration}
p.perPodBackoff[podID] = entry
}
entry.lastUpdate = p.clock.Now()
Expand All @@ -266,8 +271,8 @@ func (p *podBackoff) getBackoff(podID string) time.Duration {
entry := p.getEntry(podID)
duration := entry.backoff
entry.backoff *= 2
if entry.backoff > 60*time.Second {
entry.backoff = 60 * time.Second
if entry.backoff > p.maxDuration {
entry.backoff = p.maxDuration
}
glog.V(4).Infof("Backing off %s for pod %s", duration.String(), podID)
return duration
Expand All @@ -282,7 +287,7 @@ func (p *podBackoff) gc() {
defer p.lock.Unlock()
now := p.clock.Now()
for podID, entry := range p.perPodBackoff {
if now.Sub(entry.lastUpdate) > 60*time.Second {
if now.Sub(entry.lastUpdate) > p.maxDuration {
delete(p.perPodBackoff, podID)
}
}
Expand Down
12 changes: 8 additions & 4 deletions plugin/pkg/scheduler/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ func TestDefaultErrorFunc(t *testing.T) {
factory := NewConfigFactory(client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()}))
queue := cache.NewFIFO()
podBackoff := podBackoff{
perPodBackoff: map[string]*backoffEntry{},
clock: &fakeClock{},
perPodBackoff: map[string]*backoffEntry{},
clock: &fakeClock{},
defaultDuration: 1 * time.Millisecond,
maxDuration: 1 * time.Second,
}
errFunc := factory.makeDefaultErrorFunc(&podBackoff, queue)

Expand Down Expand Up @@ -203,8 +205,10 @@ func TestBind(t *testing.T) {
func TestBackoff(t *testing.T) {
clock := fakeClock{}
backoff := podBackoff{
perPodBackoff: map[string]*backoffEntry{},
clock: &clock,
perPodBackoff: map[string]*backoffEntry{},
clock: &clock,
defaultDuration: 1 * time.Second,
maxDuration: 60 * time.Second,
}

tests := []struct {
Expand Down

0 comments on commit b38c25e

Please sign in to comment.