Skip to content

Commit

Permalink
Abstracted test app locking
Browse files Browse the repository at this point in the history
[#63154330]

Signed-off-by: Michael Fraenkel <[email protected]>
  • Loading branch information
Karl Isenberg authored and Michael Fraenkel committed May 15, 2014
1 parent 56f54d1 commit 21411fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
7 changes: 3 additions & 4 deletions common/log_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package common

import (
"encoding/json"
steno "github.com/cloudfoundry/gosteno"
"sync"

steno "github.com/cloudfoundry/gosteno"
)

type LogCounter struct {
Expand All @@ -20,15 +21,13 @@ func NewLogCounter() *LogCounter {

func (lc *LogCounter) AddRecord(record *steno.Record) {
lc.Lock()
defer lc.Unlock()

lc.counts[record.Level.Name] += 1
lc.Unlock()
}

func (lc *LogCounter) GetCount(key string) int {
lc.Lock()
defer lc.Unlock()

return lc.counts[key]
}

Expand Down
30 changes: 21 additions & 9 deletions test/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,14 @@ func (a *TestApp) Listen() {
}

func (a *TestApp) RegisterRepeatedly(duration time.Duration) {
a.mutex.Lock()
a.stopped = false
a.start()
for {
if a.stopped {
if a.isStopped() {
break
}
a.mutex.Unlock()
a.Register()
time.Sleep(duration)
a.mutex.Lock()
}
a.mutex.Unlock()
}

func (a *TestApp) Register() {
Expand Down Expand Up @@ -108,9 +104,7 @@ func (a *TestApp) Unregister() {
b, _ := json.Marshal(rm)
a.mbusClient.Publish("router.unregister", b)

a.mutex.Lock()
a.stopped = true
a.mutex.Unlock()
a.stop()
}

func (a *TestApp) VerifyAppStatus(status int) {
Expand All @@ -134,6 +128,24 @@ func (a *TestApp) CheckAppStatus(status int) error {
return nil
}

func (a *TestApp) start() {
a.mutex.Lock()
a.stopped = false
a.mutex.Unlock()
}

func (a *TestApp) stop() {
a.mutex.Lock()
a.stopped = true
a.mutex.Unlock()
}

func (a *TestApp) isStopped() bool {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.stopped
}

type registerMessage struct {
Host string `json:"host"`
Port uint16 `json:"port"`
Expand Down

0 comments on commit 21411fb

Please sign in to comment.