github.com/whoyao/protocol@v0.0.0-20230519045905-2d8ace718ca5/utils/parallel_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"sort"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestParallel(t *testing.T) {
    12  	for _, cs := range []int{1, 10, 100, 10000} {
    13  		t.Run(strconv.Itoa(cs), func(t *testing.T) {
    14  			sendSlice := make([]int, 0, cs)
    15  			recvSlice := make([]int, 0, cs)
    16  			recvCh := make(chan int, cs)
    17  			for i := 0; i < cs; i++ {
    18  				sendSlice = append(sendSlice, i)
    19  			}
    20  
    21  			ParallelExec(sendSlice, 10, 2, func(i int) {
    22  				recvCh <- i
    23  			})
    24  			close(recvCh)
    25  
    26  			for i := range recvCh {
    27  				recvSlice = append(recvSlice, i)
    28  			}
    29  
    30  			sort.Ints(recvSlice)
    31  
    32  			require.Equal(t, sendSlice, recvSlice)
    33  		})
    34  	}
    35  }