Skip to content

Commit

Permalink
Fix race condition in MarathonerStub
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdubiel committed Dec 27, 2016
1 parent 1f77925 commit 63eb0a8
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions marathon/marathon_stub.go
Expand Up @@ -2,48 +2,58 @@ package marathon

import (
"errors"
"sync"

"github.com/allegro/marathon-consul/apps"
)

type MarathonerStub struct {
AppsStub []*apps.App
AppStub map[apps.AppID]*apps.App
TasksStub map[apps.AppID][]*apps.Task
leader string
interactions bool
AppsStub []*apps.App
AppStub map[apps.AppID]*apps.App
TasksStub map[apps.AppID][]*apps.Task
leader string
interactionsMu sync.RWMutex
interactions bool
}

func (m *MarathonerStub) ConsulApps() ([]*apps.App, error) {
m.interactions = true
m.noteInteraction()
return m.AppsStub, nil
}

func (m *MarathonerStub) App(id apps.AppID) (*apps.App, error) {
m.interactions = true
m.noteInteraction()
if app, ok := m.AppStub[id]; ok {
return app, nil
}
return nil, errors.New("app not found")
}

func (m *MarathonerStub) Tasks(appID apps.AppID) ([]*apps.Task, error) {
m.interactions = true
m.noteInteraction()
if app, ok := m.TasksStub[appID]; ok {
return app, nil
}
return nil, errors.New("app not found")
}

func (m *MarathonerStub) Leader() (string, error) {
m.interactions = true
m.noteInteraction()
return m.leader, nil
}

func (m MarathonerStub) Interactions() bool {
func (m *MarathonerStub) Interactions() bool {
m.interactionsMu.RLock()
defer m.interactionsMu.RUnlock()
return m.interactions
}

func (m *MarathonerStub) noteInteraction() {
m.interactionsMu.Lock()
defer m.interactionsMu.Unlock()
m.interactions = true
}

func MarathonerStubWithLeaderForApps(leader string, args ...*apps.App) *MarathonerStub {
stub := MarathonerStubForApps(args...)
stub.leader = leader
Expand Down

0 comments on commit 63eb0a8

Please sign in to comment.