File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44// interface `Job`
55package bqueue
66
7+ import "sync"
8+
9+ var WG sync.WaitGroup
10+
711// Queue that process jobs reveived
812type Queue struct {
913 maxWorker int
@@ -35,6 +39,7 @@ func (q *Queue) Start() {
3539
3640// CollectJob Adds a job to the Queue
3741func (q * Queue ) CollectJob (job Job ) {
42+ WG .Add (1 )
3843 q .JobReceived <- job
3944}
4045
Original file line number Diff line number Diff line change @@ -3,8 +3,11 @@ package bqueue
33import (
44 "bytes"
55 "errors"
6+ "fmt"
67 "log"
78 "os"
9+ "strconv"
10+ "strings"
811 "testing"
912 "time"
1013
@@ -37,6 +40,30 @@ func Test_Collect_and_process_job(t *testing.T) {
3740
3841}
3942
43+ func Test_WG (t * testing.T ) {
44+ q := New (1 )
45+ q .Start ()
46+
47+ //var output string
48+ var output string
49+ output += captureStout (func () {
50+ for i := 0 ; i <= 200 ; i ++ {
51+ j := aJob {strconv .Itoa (i )}
52+ q .CollectJob (j )
53+
54+ }
55+ WG .Wait ()
56+ })
57+
58+ // look for all instances of "test job: N"
59+ // not very efficient but minimizes false positive
60+ for i := 0 ; i <= 200 ; i ++ {
61+ if ! strings .Contains (output , fmt .Sprintf ("test job: %d" , i )) {
62+ t .FailNow ()
63+ }
64+ }
65+ }
66+
4067func captureStout (f func ()) string {
4168 var buf bytes.Buffer
4269 output := ""
Original file line number Diff line number Diff line change @@ -52,4 +52,6 @@ func (w *worker) do(j Job) {
5252 if err != nil {
5353 fmt .Println (err )
5454 }
55+
56+ WG .Done ()
5557}
You can’t perform that action at this time.
0 commit comments