diff --git a/workers_test.go b/workers_test.go index dd4fcbd..7401727 100644 --- a/workers_test.go +++ b/workers_test.go @@ -20,26 +20,31 @@ const ( ) type WorkerOne struct { + Count int + sync.Mutex } type WorkerTwo struct { + Count int + sync.Mutex } -func NewWorkerOne() Worker { +func NewWorkerOne() *WorkerOne { return &WorkerOne{} } -func NewWorkerTwo() Worker { +func NewWorkerTwo() *WorkerTwo { return &WorkerTwo{} } +func (wo *WorkerOne) CurrentCount() int { + wo.Lock() + defer wo.Unlock() + return wo.Count +} + func (wo *WorkerOne) Work(in interface{}, out chan<- interface{}) error { - var workerOne = "worker_one" mut.Lock() - if val, ok := count[workerOne]; ok { - count[workerOne] = val + 1 - } else { - count[workerOne] = 1 - } + wo.Count = wo.Count + 1 mut.Unlock() total := in.(int) * 2 @@ -47,20 +52,20 @@ func (wo *WorkerOne) Work(in interface{}, out chan<- interface{}) error { return nil } +func (wt *WorkerTwo) CurrentCount() int { + wt.Lock() + defer wt.Unlock() + return wt.Count +} + func (wt *WorkerTwo) Work(in interface{}, out chan<- interface{}) error { - var workerTwo = "worker_two" mut.Lock() - if val, ok := count[workerTwo]; ok { - count[workerTwo] = val + 1 - } else { - count[workerTwo] = 1 - } + wt.Count = wt.Count + 1 mut.Unlock() return nil } var ( - count = make(map[string]int) mut = sync.RWMutex{} err = errors.New("test error") deadline = func() time.Time { return time.Now().Add(workerTimeout) } @@ -197,8 +202,10 @@ func TestWorkers(t *testing.T) { func TestWorkersFinish100(t *testing.T) { const workCount = 100 ctx := context.Background() - workerOne := NewRunner(ctx, NewWorkerOne(), 1000).Start() - workerTwo := NewRunner(ctx, NewWorkerTwo(), 1000).InFrom(workerOne).Start() + w1 := NewWorkerOne() + w2 := NewWorkerTwo() + workerOne := NewRunner(ctx, w1, 1000).Start() + workerTwo := NewRunner(ctx, w2, 1000).InFrom(workerOne).Start() for i := 0; i < workCount; i++ { workerOne.Send(rand.Intn(100)) @@ -212,23 +219,25 @@ func TestWorkersFinish100(t *testing.T) { fmt.Println(err) } - if count["worker_one"] != workCount { - t.Log("worker one failed to finish,", "worker_one count", count["worker_one"], "/ 100000") + if w1.CurrentCount() != workCount { + t.Log("worker one failed to finish,", "worker_one count", w1.CurrentCount(), "/ 100000") t.Fail() } - if count["worker_two"] != workCount { - t.Log("worker two failed to finish,", "worker_two count", count["worker_two"], "/ 100000") + if w2.CurrentCount() != workCount { + t.Log("worker two failed to finish,", "worker_two count", w2.CurrentCount(), "/ 100000") t.Fail() } - t.Logf("worker_one count: %d, worker_two count: %d", count["worker_one"], count["worker_two"]) + t.Logf("worker_one count: %d, worker_two count: %d", w1.CurrentCount(), w2.CurrentCount()) } func TestWorkersFinish100000(t *testing.T) { const workCount = 100000 ctx := context.Background() - workerOne := NewRunner(ctx, NewWorkerOne(), 1000).Start() - workerTwo := NewRunner(ctx, NewWorkerTwo(), 1000).InFrom(workerOne).Start() + w1 := NewWorkerOne() + w2 := NewWorkerTwo() + workerOne := NewRunner(ctx, w1, 1000).Start() + workerTwo := NewRunner(ctx, w2, 1000).InFrom(workerOne).Start() for i := 0; i < workCount; i++ { workerOne.Send(rand.Intn(100)) @@ -242,23 +251,25 @@ func TestWorkersFinish100000(t *testing.T) { fmt.Println(err) } - if count["worker_one"] != workCount { - t.Log("worker one failed to finish,", "worker_one count", count["worker_one"], "/ 100000") + if w1.CurrentCount() != workCount { + t.Log("worker one failed to finish,", "worker_one count", w1.CurrentCount(), "/ 100000") t.Fail() } - if count["worker_two"] != workCount { - t.Log("worker two failed to finish,", "worker_two count", count["worker_two"], "/ 100000") + if w2.CurrentCount() != workCount { + t.Log("worker two failed to finish,", "worker_two count", w2.CurrentCount(), "/ 100000") t.Fail() } - t.Logf("worker_one count: %d, worker_two count: %d", count["worker_one"], count["worker_two"]) + t.Logf("worker_one count: %d, worker_two count: %d", w1.CurrentCount(), w2.CurrentCount()) } func TestWorkersFinish1000000(t *testing.T) { const workCount = 1000000 ctx := context.Background() - workerOne := NewRunner(ctx, NewWorkerOne(), 1000).Start() - workerTwo := NewRunner(ctx, NewWorkerTwo(), 1000).InFrom(workerOne).Start() + w1 := NewWorkerOne() + w2 := NewWorkerTwo() + workerOne := NewRunner(ctx, w1, 1000).Start() + workerTwo := NewRunner(ctx, w2, 1000).InFrom(workerOne).Start() for i := 0; i < workCount; i++ { workerOne.Send(rand.Intn(100)) @@ -272,16 +283,16 @@ func TestWorkersFinish1000000(t *testing.T) { fmt.Println(err) } - if count["worker_one"] != workCount { - t.Log("worker one failed to finish,", "worker_one count", count["worker_one"], "/ 100000") + if w1.CurrentCount() != workCount { + t.Log("worker one failed to finish,", "worker_one count", w1.CurrentCount(), "/ 100000") t.Fail() } - if count["worker_two"] != workCount { - t.Log("worker two failed to finish,", "worker_two count", count["worker_two"], "/ 100000") + if w2.CurrentCount() != workCount { + t.Log("worker two failed to finish,", "worker_two count", w2.CurrentCount(), "/ 100000") t.Fail() } - t.Logf("worker_one count: %d, worker_two count: %d", count["worker_one"], count["worker_two"]) + t.Logf("worker_one count: %d, worker_two count: %d", w1.CurrentCount(), w2.CurrentCount()) } func BenchmarkGoWorkers1to1(b *testing.B) {