github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/utils/parallel_test.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"sort"
    19  	"strconv"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestParallel(t *testing.T) {
    26  	for _, cs := range []int{1, 10, 100, 10000} {
    27  		t.Run(strconv.Itoa(cs), func(t *testing.T) {
    28  			sendSlice := make([]int, 0, cs)
    29  			recvSlice := make([]int, 0, cs)
    30  			recvCh := make(chan int, cs)
    31  			for i := 0; i < cs; i++ {
    32  				sendSlice = append(sendSlice, i)
    33  			}
    34  
    35  			ParallelExec(sendSlice, 10, 2, func(i int) {
    36  				recvCh <- i
    37  			})
    38  			close(recvCh)
    39  
    40  			for i := range recvCh {
    41  				recvSlice = append(recvSlice, i)
    42  			}
    43  
    44  			sort.Ints(recvSlice)
    45  
    46  			require.Equal(t, sendSlice, recvSlice)
    47  		})
    48  	}
    49  }