github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/run/pool_test.go (about)

     1  package run
     2  
     3  import (
     4  	"sync"
     5  	"sync/atomic"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPoolRunsWorker(t *testing.T) {
    12  	ch := make(chan int)
    13  	p := NewGoroutinePool(5)
    14  	p.Submit(func() {
    15  		ch <- 42
    16  	})
    17  	assert.Equal(t, 42, <-ch)
    18  }
    19  
    20  func TestPoolHasSufficientWorkers(t *testing.T) {
    21  	var wg sync.WaitGroup
    22  	var count int64
    23  	wg.Add(5)
    24  	p := NewGoroutinePool(5)
    25  	for i := 0; i < 5; i++ {
    26  		p.Submit(func() {
    27  			atomic.AddInt64(&count, 1)
    28  			wg.Done()
    29  			// This blocks forever so if the pool hasn't provisioned enough workers
    30  			// we'll never get to the end of the test.
    31  			select {}
    32  		})
    33  	}
    34  	wg.Wait()
    35  	assert.EqualValues(t, 5, count)
    36  }
    37  
    38  func TestSubmitParam(t *testing.T) {
    39  	ch := make(chan interface{})
    40  	p := NewGoroutinePool(5)
    41  	p.SubmitParam(func(p interface{}) {
    42  		ch <- p
    43  	}, 42)
    44  	v := <-ch
    45  	assert.Equal(t, 42, v.(int))
    46  }