Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Refactored mockJob test to use Promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorDoyle committed Dec 22, 2015
1 parent 869c954 commit 94fa0e2
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions scheduler/work_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,56 @@ import (
"time"

log "github.com/Sirupsen/logrus"
. "github.com/intelsdi-x/snap/pkg/promise"
. "github.com/smartystreets/goconvey/convey"
)

// A mockJob can be either synchronus (will not return from Run()
// until a receiver is ready on completeChan) or asynchronous.
// A mockJob can be either asynchronous or synchronous (will not return from
// Run() until a caller is ready in Await()).
//
// Test code can rendez-vous with synchronous mockJobs in Run()
// by invoking Await(), which also unblocks the worker executing
// the job.
//
// For asynchronous mockJobs, Await() blocks the caller until the
// job does a buffered send on completeChan in Run().
// job is completed in Run().
type mockJob struct {
errors []error
worked bool
deadline time.Time
starttime time.Time
completeChan chan struct{}
errors []error
sync bool
worked bool
deadline time.Time
starttime time.Time
completePromise Promise
syncPromise Promise
}

func newMockJob(sync bool) *mockJob {
var completeChan chan struct{}
if sync {
completeChan = make(chan struct{})
} else {
completeChan = make(chan struct{}, 1)
}

return &mockJob{
worked: false,
deadline: time.Now().Add(1 * time.Second),
starttime: time.Now(),
completeChan: completeChan,
sync: sync,
worked: false,
deadline: time.Now().Add(1 * time.Second),
starttime: time.Now(),
completePromise: NewPromise(),
syncPromise: NewPromise(),
}
}

func (mj *mockJob) Errors() []error { return mj.errors }
func (mj *mockJob) StartTime() time.Time { return mj.starttime }
func (mj *mockJob) Deadline() time.Time { return mj.deadline }
func (mj *mockJob) Type() jobType { return collectJobType }
func (mj *mockJob) Await() { <-mj.completeChan }

func (mj *mockJob) Await() {
mj.syncPromise.Complete([]error{})
mj.completePromise.Await()
}

func (mj *mockJob) Run() {
mj.worked = true
mj.completeChan <- struct{}{}
if mj.sync {
mj.syncPromise.Await()
}
mj.completePromise.Complete([]error{})
}

func TestWorkerManager(t *testing.T) {
Expand All @@ -87,7 +92,7 @@ func TestWorkerManager(t *testing.T) {
manager := newWorkManager(CollectQSizeOption(1), CollectWkrSizeOption(1))
manager.Start()

j1 := newMockJob(true) // j1 does a blocking send on its completeChan
j1 := newMockJob(true) // j1 blocks in Run() until Await() is invoked.
j2 := newMockJob(false)
j3 := newMockJob(false)

Expand Down

0 comments on commit 94fa0e2

Please sign in to comment.