Skip to content

Commit f194dc1

Browse files
committed
Fixed test
1 parent 0cfbb69 commit f194dc1

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ func main() {
4646
pool := workerpool.New(poolSize)
4747
out := make(chan int, jobsAmount)
4848

49-
go func() {
50-
// stop all workers after jobs are done
51-
defer pool.Stop()
52-
wg.Wait()
53-
close(out)
54-
}()
55-
5649
pool.Start(workersAmount, func(i int) {
5750
defer wg.Done()
5851
out <- i
@@ -64,6 +57,13 @@ func main() {
6457
pool.Delegate(i)
6558
}
6659

60+
go func() {
61+
// stop all workers after jobs are done
62+
wg.Wait()
63+
close(out)
64+
pool.Stop()
65+
}()
66+
6767
sum := 0
6868
for n := range out {
6969
sum += n

doc.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ Basic example:
2222
pool := workerpool.New(poolSize)
2323
out := make(chan int, jobsAmount)
2424
25-
go func() {
26-
// stop all workers after jobs are done
27-
defer pool.Stop()
28-
wg.Wait()
29-
close(out)
30-
}()
31-
3225
pool.Start(workersAmount, func(i int) {
3326
defer wg.Done()
3427
out <- i
@@ -40,6 +33,13 @@ Basic example:
4033
pool.Delegate(i)
4134
}
4235
36+
go func() {
37+
// stop all workers after jobs are done
38+
wg.Wait()
39+
close(out)
40+
pool.Stop()
41+
}()
42+
4343
sum := 0
4444
for n := range out {
4545
sum += n

pool_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ func TestNew(t *testing.T) {
1414
t.Fail()
1515
}
1616
}
17+
func TestInvalidWorkersNumber(t *testing.T) {
18+
pool := New(2)
19+
defer pool.Stop()
20+
21+
if pool.Start(0, func() {}) == nil {
22+
t.Fail()
23+
}
24+
}
25+
26+
func TestInvalidWorker(t *testing.T) {
27+
pool := New(2)
28+
defer pool.Stop()
29+
30+
if pool.Start(1, "worker") == nil {
31+
t.Fail()
32+
}
33+
}
1734

1835
func TestWorkers(t *testing.T) {
1936
delegateWorkToWorkers(t, 2, 3, 3) // same workers as jobs
@@ -27,12 +44,6 @@ func delegateWorkToWorkers(t *testing.T, poolSize int, jobsAmount int, workersAm
2744
pool := New(poolSize)
2845
out := make(chan int, jobsAmount)
2946

30-
go func() {
31-
wg.Wait()
32-
close(out)
33-
pool.Stop()
34-
}()
35-
3647
pool.Start(workersAmount, func(i int) {
3748
defer wg.Done()
3849
out <- i
@@ -44,6 +55,12 @@ func delegateWorkToWorkers(t *testing.T, poolSize int, jobsAmount int, workersAm
4455
pool.Delegate(i)
4556
}
4657

58+
go func() {
59+
wg.Wait()
60+
close(out)
61+
pool.Stop()
62+
}()
63+
4764
sum := 0
4865
for n := range out {
4966
sum += n

0 commit comments

Comments
 (0)