-
Notifications
You must be signed in to change notification settings - Fork 28
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
0b153f4
commit 18cc215
Showing
6 changed files
with
112 additions
and
6 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
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,25 @@ | ||
package util | ||
|
||
import "sync" | ||
|
||
// BlockRunner provides a simple way to run tasks, | ||
// block until all the tasks are finished. | ||
type BlockRunner struct { | ||
once sync.Once | ||
wg sync.WaitGroup | ||
} | ||
|
||
// Init initializes how many tasks we want to run synchronously. | ||
func (r *BlockRunner) Init(n int) { | ||
r.once.Do(func() { | ||
r.wg.Add(n) | ||
}) | ||
} | ||
|
||
// Run runs the task in different goroutines and | ||
// block until all the tasks are finished. | ||
func (r *BlockRunner) Run(f func()) { | ||
f() | ||
r.wg.Done() | ||
r.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,39 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBlockRunner(t *testing.T) { | ||
r := &BlockRunner{} | ||
|
||
r.Init(2) | ||
|
||
ch := make(chan int, 2) | ||
for i := 0; i < 2; i++ { | ||
go func(i int) { | ||
// Can only initialize once | ||
r.Init(3) | ||
r.Run(func() { | ||
t.Logf("block run %d", i+1) | ||
time.Sleep(time.Second) | ||
}) | ||
ch <- i | ||
}(i) | ||
} | ||
|
||
select { | ||
case <-ch: | ||
t.Fatal("can't get data at this time") | ||
case <-time.After(100 * time.Millisecond): | ||
} | ||
|
||
for i := 0; i < 2; i++ { | ||
select { | ||
case <-ch: | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("can't wait ") | ||
} | ||
} | ||
} |