42 lines
535 B
Go
42 lines
535 B
Go
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)
|
|
}
|
|
}
|