Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Fix failing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
catmullet committed Jun 21, 2021
1 parent 8f76ef6 commit 361f319
Showing 1 changed file with 47 additions and 36 deletions.
83 changes: 47 additions & 36 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,52 @@ 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
out <- total
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) }
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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) {
Expand Down

0 comments on commit 361f319

Please sign in to comment.