-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7597af8
commit cbebf37
Showing
4 changed files
with
223 additions
and
2 deletions.
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
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,41 @@ | ||
package workerpool | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// Runs functions with one wait group | ||
type Group struct { | ||
wg *sync.WaitGroup | ||
} | ||
|
||
// NewGroup - creates new Group | ||
func NewGroup() Group { | ||
return Group{ | ||
wg: new(sync.WaitGroup), | ||
} | ||
} | ||
|
||
// Go - runs function with wait group | ||
func (g Group) Go(f func()) { | ||
g.wg.Add(1) | ||
go func() { | ||
defer g.wg.Done() | ||
f() | ||
}() | ||
} | ||
|
||
// GoCtx - runs function with wait group using context | ||
func (g Group) GoCtx(ctx context.Context, f func(ctx context.Context)) { | ||
g.wg.Add(1) | ||
go func() { | ||
defer g.wg.Done() | ||
f(ctx) | ||
}() | ||
} | ||
|
||
// Wait - waits until grouped functions end | ||
func (g Group) Wait() { | ||
g.wg.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,88 @@ | ||
package workerpool | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGroupWithContext(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
group := NewGroup() | ||
group.GoCtx(ctx, ticker1) | ||
group.GoCtx(ctx, ticker2) | ||
|
||
time.Sleep(10 * time.Second) | ||
cancel() | ||
|
||
group.Wait() | ||
} | ||
|
||
func ticker1(ctx context.Context) { | ||
ticker := time.NewTicker(time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
log.Print("second") | ||
} | ||
} | ||
} | ||
|
||
func ticker2(ctx context.Context) { | ||
ticker := time.NewTicker(time.Second * 2) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
log.Print("2 second") | ||
} | ||
} | ||
} | ||
|
||
func TestGroup(t *testing.T) { | ||
group := NewGroup() | ||
group.Go(ticker3) | ||
group.Go(ticker4) | ||
group.Wait() | ||
} | ||
|
||
func ticker3() { | ||
ticker := time.NewTicker(time.Second) | ||
defer ticker.Stop() | ||
|
||
var count int | ||
|
||
for range ticker.C { | ||
count++ | ||
log.Print("second") | ||
|
||
if count == 5 { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func ticker4() { | ||
ticker := time.NewTicker(time.Second * 2) | ||
defer ticker.Stop() | ||
|
||
var count int | ||
|
||
for range ticker.C { | ||
count++ | ||
log.Print("2 second") | ||
|
||
if count == 5 { | ||
return | ||
} | ||
} | ||
} |