Skip to content

Commit

Permalink
Merge pull request #43 from thrawn01/thrawn/develop
Browse files Browse the repository at this point in the history
Added WaitGroup.Go()
  • Loading branch information
thrawn01 authored Feb 13, 2019
2 parents b4e6abe + 187283c commit 5488fd5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.3
2.3.4
16 changes: 15 additions & 1 deletion waitgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type WaitGroup struct {
}

// Run a routine and collect errors if any
//func (wg *WaitGroup) Run(callBack func() error) {
func (wg *WaitGroup) Run(callBack func(interface{}) error, data interface{}) {
wg.wg.Add(1)
go func() {
Expand All @@ -41,6 +40,21 @@ func (wg *WaitGroup) Run(callBack func(interface{}) error, data interface{}) {
}()
}

// Execute a long running routine
func (wg *WaitGroup) Go(cb func()) {
wg.wg.Add(1)
go func() {
err := cb
if err == nil {
wg.wg.Done()
return
}
wg.mutex.Lock()
wg.wg.Done()
wg.mutex.Unlock()
}()
}

// Run a goroutine in a loop continuously, if the callBack returns false the loop is broken.
// `Until()` differs from `Loop()` in that if the `Stop()` is called on the WaitGroup
// the `done` channel is closed. Implementations of the callBack function can listen
Expand Down
23 changes: 20 additions & 3 deletions waitgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ package holster_test

import (
"sync/atomic"
"testing"
"time"

"github.com/mailgun/holster"
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
"gopkg.in/ahmetb/go-linq.v3"
"testing"
"github.com/mailgun/holster"
)

type WaitGroupTestSuite struct{
type WaitGroupTestSuite struct {
suite.Suite
}

Expand Down Expand Up @@ -59,6 +59,23 @@ func (s *WaitGroupTestSuite) TestRun() {
s.Equal(true, linq.From(errs).Contains(items[1]))
}

func (s *WaitGroupTestSuite) TestGo() {
var wg holster.WaitGroup

wg.Go(func() {
// Do some long running thing
time.Sleep(time.Nanosecond * 500)
})

wg.Go(func() {
// Do some long running thing
time.Sleep(time.Nanosecond * 50)
})

errs := wg.Wait()
s.Nil(errs)
}

func (s *WaitGroupTestSuite) TestLoop() {
pipe := make(chan int32, 0)
var wg holster.WaitGroup
Expand Down

0 comments on commit 5488fd5

Please sign in to comment.