Skip to content

Commit

Permalink
Merge pull request #1 from SamuelTissot/feature/waitgroup
Browse files Browse the repository at this point in the history
added waitgroup to the Queue.
  • Loading branch information
SamuelTissot authored Jun 10, 2019
2 parents 6ca2664 + 6385fd7 commit 241f9cc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
// interface `Job`
package bqueue

import "sync"

var WG sync.WaitGroup

// Queue that process jobs reveived
type Queue struct {
maxWorker int
Expand Down Expand Up @@ -35,6 +39,7 @@ func (q *Queue) Start() {

// CollectJob Adds a job to the Queue
func (q *Queue) CollectJob(job Job) {
WG.Add(1)
q.JobReceived <- job
}

Expand Down
27 changes: 27 additions & 0 deletions queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package bqueue
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -37,6 +40,30 @@ func Test_Collect_and_process_job(t *testing.T) {

}

func Test_WG(t *testing.T) {
q := New(1)
q.Start()

//var output string
var output string
output += captureStout(func() {
for i := 0; i <= 200; i++ {
j := aJob{strconv.Itoa(i)}
q.CollectJob(j)

}
WG.Wait()
})

// look for all instances of "test job: N"
// not very efficient but minimizes false positive
for i := 0; i <= 200; i++ {
if !strings.Contains(output, fmt.Sprintf("test job: %d", i)) {
t.FailNow()
}
}
}

func captureStout(f func()) string {
var buf bytes.Buffer
output := ""
Expand Down
2 changes: 2 additions & 0 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ func (w *worker) do(j Job) {
if err != nil {
fmt.Println(err)
}

WG.Done()
}

0 comments on commit 241f9cc

Please sign in to comment.