-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds unbuffered worker * remove interface method
- Loading branch information
1 parent
a107145
commit 120b323
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package worker | ||
|
||
import "sync" | ||
|
||
// UnbufferedPool is an interface which implements BufferedPoolImpl, allowing you to mock out the pool for tests. | ||
type UnbufferedPool interface { | ||
// Do enqueues a task and blocks the goroutine until it's enqueued. | ||
Do(func()) | ||
|
||
// Wait blocks the goroutine until all tasks are complete. | ||
Wait() | ||
} | ||
|
||
// compile time assertion | ||
var _ UnbufferedPool = (*UnbufferedPoolImpl)(nil) | ||
|
||
// UnbufferedPoolImpl is an implementation that is compatible with UnbufferedPool. | ||
// | ||
// The main purpose of this tool is to let you ensure all goroutines have been closed before exiting your app, you can | ||
// pass an instance of this around everywhere instead. | ||
type UnbufferedPoolImpl struct { | ||
// WaitGroup tracks how many running/queued tasks there are, we expose Wait() so you can wait until all tasks are complete. | ||
WaitGroup sync.WaitGroup | ||
} | ||
|
||
// NewUnbufferedPool creates a new instance of BufferedPoolImpl | ||
func NewUnbufferedPool() *UnbufferedPoolImpl { | ||
return &UnbufferedPoolImpl{} | ||
} | ||
|
||
// Do increments the wait group and invokes the goroutine, then decrements it. | ||
func (w *UnbufferedPoolImpl) Do(cb func()) { | ||
w.WaitGroup.Add(1) | ||
go func(cb func()) { | ||
cb() | ||
w.WaitGroup.Done() | ||
}(cb) | ||
} | ||
|
||
// Wait blocks the goroutine until all tasks are complete. | ||
func (w *UnbufferedPoolImpl) Wait() { | ||
w.WaitGroup.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package worker_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/worker" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnbufferedPool(t *testing.T) { | ||
t.Parallel() | ||
|
||
p := worker.NewUnbufferedPool() | ||
|
||
var ( | ||
m sync.Mutex | ||
v int | ||
) | ||
|
||
for i := 0; i < 10; i++ { | ||
p.Do(func() { | ||
m.Lock() | ||
v++ | ||
m.Unlock() | ||
}) | ||
} | ||
|
||
p.Wait() | ||
|
||
assert.Equal(t, 10, v) | ||
} |