worker-pool: init

This commit is contained in:
2024-02-15 18:39:41 +03:00
parent d1cb8beba3
commit 9ed5a2f85a
4 changed files with 126 additions and 0 deletions

41
worker-pool/main_test.go Normal file
View 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)
}
}