goroutinepool_test.go 1.8 KB
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
package goroutinepool

import (
	"errors"
	"sync"
	"testing"
	"time"

	log "github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

func TestGorourinepool_without_error(t *testing.T) {
	pool, err := NewGoroutinePool("TEST-POOL", 2, 10)
	assert.Nil(t, err)
	wg := sync.WaitGroup{}
	wg.Add(3)
	for i := 0; i < 3; i++ {
		idx := i
		pool.Put("TASK", func() error {
			defer wg.Done()
			log.Infof("TASK ID:%d", idx+1)
			return nil
		})
	}
	wg.Wait()
	pool.Close()
}

func TestGorourinepool_with_error(t *testing.T) {
	pool, err := NewGoroutinePool("TEST-POOL", 1, 10)
	assert.Nil(t, err)
	wg := sync.WaitGroup{}
	wg.Add(3)
	for i := 0; i < 3; i++ {
		index := i
		pool.Put("TASK", func() error {
			defer wg.Done()
			log.Infof("TASK ID:%d", index+1)
			if index == 0 {
				return errors.New("task execute failed")
			}
			return nil
		})
	}
	wg.Wait()
	pool.Close()
}

func TestGorourinepool_with_timeout(t *testing.T) {
	pool, err := NewGoroutinePool("TEST-POOL", 3, 10)
	assert.Nil(t, err)
	for i := 0; i < 3; i++ {
		index := i
		pool.PutWithTimeout("TASK", func() error {
			time.Sleep(time.Millisecond * 10)
			log.Infof("TASK ID:%d", index+1)
			return nil
		}, time.Millisecond)
	}

	t1 := time.Now()
	pool.Close()
	dur := time.Now().Sub(t1)
	log.Infof("pool exited, duration:%+v", dur)
	assert.True(t, dur < time.Millisecond*5)
}

func TestGorourinepool_without_timeout(t *testing.T) {
	pool, err := NewGoroutinePool("TEST-POOL-WITHOUT-TIMEOUT", 3, 10)
	assert.Nil(t, err)
	for i := 0; i < 3; i++ {
		index := i
		pool.Put("TASK", func() error {
			time.Sleep(time.Millisecond * 10)
			log.Infof("TASK ID:%d", index+1)
			return nil
		})
	}

	t1 := time.Now()

	time.Sleep(time.Millisecond)

	pool.Close()
	dur := time.Now().Sub(t1)
	log.Infof("pool exited, duration:%+v", dur)
	assert.True(t, dur >= time.Millisecond*10)
}