worker-pool: init
This commit is contained in:
41
worker-pool/main_test.go
Normal file
41
worker-pool/main_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWorkerPool(t *testing.T) {
|
||||
const tasks = 5
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(tasks)
|
||||
|
||||
var mt sync.Mutex
|
||||
done := make(map[int]bool, tasks)
|
||||
|
||||
worker := func(pid int, task int) {
|
||||
defer wg.Done()
|
||||
|
||||
mt.Lock()
|
||||
done[pid] = true
|
||||
mt.Unlock()
|
||||
|
||||
fmt.Printf("task:%+v\n", task)
|
||||
}
|
||||
|
||||
pool := WorkerPool(tasks, worker)
|
||||
|
||||
for j := 0; j < tasks; j++ {
|
||||
pool <- j
|
||||
}
|
||||
close(pool)
|
||||
wg.Wait()
|
||||
|
||||
for _, v := range done {
|
||||
assert.True(t, v)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user