github.com/songzhibin97/gkit@v1.2.13/concurrent/orderly_test.go (about)

     1  package concurrent
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func productionOrder() (ret []*OrderlyTask) {
    11  	for i := 0; i < 10; i++ {
    12  		i := i
    13  		ret = append(ret, NewOrderTask(func() {
    14  			fmt.Println(i)
    15  		}))
    16  	}
    17  	return ret
    18  }
    19  
    20  func TestOrderly(t *testing.T) {
    21  	var slice []int
    22  	var productionOrder func() []*OrderlyTask = func() (ret []*OrderlyTask) {
    23  		for i := 0; i < 10; i++ {
    24  			i := i
    25  			ret = append(ret, NewOrderTask(func() {
    26  				slice = append(slice, i)
    27  			}))
    28  		}
    29  		return ret
    30  	}
    31  	Orderly(productionOrder())
    32  	assert.Equal(t, slice, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
    33  }