diff --git a/pool.go b/pool.go index 14887a4..b460983 100644 --- a/pool.go +++ b/pool.go @@ -14,7 +14,7 @@ type Pool struct { // lock represents a mutex to protect the workers slice lock sync.Mutex // cond represents a condition variable to manage the pool - cond *sync.Cond + cond *sync.Cond } func NewPool(capacity int) *Pool { @@ -38,7 +38,7 @@ func (p *Pool) Submit(task Task) { if worker == nil { worker = &Worker{ pool: p, - task: make(chan Task), + task: make(chan Task, 1), } } worker.task <- task diff --git a/pool_test.go b/pool_test.go new file mode 100644 index 0000000..e0b76fe --- /dev/null +++ b/pool_test.go @@ -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()) + } +}