From 21411fbbcfec20a4fc4e944e04c24a5c44d43658 Mon Sep 17 00:00:00 2001 From: Karl Isenberg Date: Thu, 15 May 2014 14:23:25 -0700 Subject: [PATCH] Abstracted test app locking [#63154330] Signed-off-by: Michael Fraenkel --- common/log_counter.go | 7 +++---- test/app.go | 30 +++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/common/log_counter.go b/common/log_counter.go index 22aae034e..b80f013bb 100644 --- a/common/log_counter.go +++ b/common/log_counter.go @@ -2,8 +2,9 @@ package common import ( "encoding/json" - steno "github.com/cloudfoundry/gosteno" "sync" + + steno "github.com/cloudfoundry/gosteno" ) type LogCounter struct { @@ -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] } diff --git a/test/app.go b/test/app.go index 8d4374a9a..373bed3fd 100644 --- a/test/app.go +++ b/test/app.go @@ -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() { @@ -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) { @@ -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"`