-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from huanghaoyuanhhy/fix_workerpool
common: no longer reuse worker
- Loading branch information
Showing
3 changed files
with
77 additions
and
18 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,50 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func Test0Worker(t *testing.T) { | ||
_, err := NewWorkerPool(context.Background(), 0, 0) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestRunTaskNoErr(t *testing.T) { | ||
wp, err := NewWorkerPool(context.Background(), 3, 10) | ||
assert.Nil(t, err) | ||
|
||
wp.Start() | ||
|
||
var v atomic.Int64 | ||
for i := 0; i < 10; i++ { | ||
wp.Submit(func(ctx context.Context) error { | ||
v.Add(1) | ||
return nil | ||
}) | ||
} | ||
|
||
wp.Done() | ||
assert.Nil(t, wp.Wait()) | ||
assert.Equal(t, int64(10), v.Load()) | ||
} | ||
|
||
func TestRunTaskReturnErr(t *testing.T) { | ||
wp, err := NewWorkerPool(context.Background(), 10, 10) | ||
assert.Nil(t, err) | ||
|
||
wp.Start() | ||
|
||
for i := 0; i < 100; i++ { | ||
wp.Submit(func(ctx context.Context) error { | ||
return errors.New("some err") | ||
}) | ||
} | ||
|
||
wp.Done() | ||
assert.NotNil(t, wp.Wait()) | ||
} |