From 6688c29c37c0e91da1032f2e19ef4fbf81ad8b89 Mon Sep 17 00:00:00 2001 From: draxil Date: Sun, 1 Jun 2014 16:59:57 +0100 Subject: [PATCH] Enahanced Work() without Ready() behaviour: Now if you try to call Work() without calling Ready(), it will trigger an attempt to run Ready(), and will only panic if there is an error. --- worker/worker.go | 6 +++++- worker/worker_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/worker/worker.go b/worker/worker.go index f4d926e..a81f4ab 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -183,7 +183,11 @@ func (worker *Worker) Ready() (err error) { // Most of time, this should be evaluated in goroutine. func (worker *Worker) Work() { if ! worker.ready { - panic( "worker: Work() called before Ready()") + // didn't run Ready beforehand, so we'll have to do it: + err := worker.Ready() + if err != nil { + panic( err ) + } } defer func() { diff --git a/worker/worker_test.go b/worker/worker_test.go index 7fa8f0b..9960591 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -77,6 +77,33 @@ func TestWork(t *testing.T) { wg.Wait() } + func TestWorkerClose(t *testing.T) { worker.Close() } + +func TestWorkWithoutReady(t * testing.T){ + other_worker := New(Unlimited) + var wg sync.WaitGroup + + if err := other_worker.AddServer(Network, "127.0.0.1:4730"); err != nil { + t.Error(err) + } + if err := other_worker.AddFunc("foobar", foobar, 0); err != nil { + t.Error(err) + } + + other_worker.JobHandler = func( j Job ) error { + if( ! other_worker.ready ){ + t.Error("Worker not ready as expected"); + } + wg.Done() + return nil + } + + go other_worker.Work(); + + wg.Add(1) + worker.Echo([]byte("Hello")) + wg.Wait(); +}