-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve worker task handling and add pool tests
- Changed the worker task channel to be buffered to prevent blocking when submitting tasks. - Added a new test file, pool_test.go, to test the Submit function of the pool. - The test ensures that all tasks are executed and that there are no running workers left after all tasks are done. Signed-off-by: Daniel Hu <tao.hu@merico.dev>
- Loading branch information
1 parent
678a72b
commit 6088427
Showing
2 changed files
with
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package gopool | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestSubmit(t *testing.T) { | ||
var wg sync.WaitGroup | ||
p := NewPool(10) | ||
|
||
for i := 0; i < 20; i++ { | ||
wg.Add(1) | ||
taskNum := i // Capture the task number | ||
task := func() { | ||
t.Logf("Executing task %d\n", taskNum) | ||
defer wg.Done() | ||
} | ||
p.Submit(task) | ||
} | ||
|
||
wg.Wait() | ||
|
||
if p.Running() != 0 { | ||
t.Errorf("Expected running workers to be 0, but got %d", p.Running()) | ||
} | ||
} |