trpc.group/trpc-go/trpc-go@v1.0.3/rpcz/id_generator_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package rpcz
    15  
    16  import (
    17  	"math/rand"
    18  	"reflect"
    19  	"sync"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func Test_newRandomIDGenerator(t *testing.T) {
    26  	type args struct {
    27  		seed int64
    28  	}
    29  	tests := []struct {
    30  		name string
    31  		args args
    32  		want *randomIDGenerator
    33  	}{
    34  		{
    35  			name: "seed equals zero",
    36  			args: args{seed: 0},
    37  			want: &randomIDGenerator{randSource: rand.New(rand.NewSource(0))},
    38  		},
    39  		{
    40  			name: "seed greater zero",
    41  			args: args{seed: 20221111},
    42  			want: &randomIDGenerator{randSource: rand.New(rand.NewSource(20221111))},
    43  		},
    44  		{
    45  			name: "seed less zero",
    46  			args: args{seed: -20221111},
    47  			want: &randomIDGenerator{randSource: rand.New(rand.NewSource(-20221111))},
    48  		},
    49  	}
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			if got := newRandomIDGenerator(tt.args.seed); !reflect.DeepEqual(got, tt.want) {
    53  				t.Errorf("newRandomIDGenerator() = %v, want %v", got, tt.want)
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func Test_randomIDGenerator_newSpanID(t *testing.T) {
    60  	t.Run("testNewSpanIDSequentiallyIsOk", testNewSpanIDSequentiallyIsOk)
    61  	t.Run("testNewSpanIDConcurrentlyIsOk", testNewSpanIDConcurrentlyIsOk)
    62  }
    63  
    64  func testNewSpanIDSequentiallyIsOk(t *testing.T) {
    65  	const seed = 20221111
    66  	gen := newRandomIDGenerator(seed)
    67  	rand.Seed(seed)
    68  	for i := 0; i < 1111; i++ {
    69  		require.Equal(t, SpanID(rand.Int63()), gen.newSpanID())
    70  	}
    71  }
    72  func testNewSpanIDConcurrentlyIsOk(t *testing.T) {
    73  	const (
    74  		idNum   = 1111
    75  		iterNum = 11
    76  		seed    = 20221111
    77  	)
    78  	gen := newRandomIDGenerator(seed)
    79  	idChan := make(chan SpanID, idNum)
    80  
    81  	var wg sync.WaitGroup
    82  	wg.Add(idNum)
    83  	for i := 0; i < iterNum; i++ {
    84  		for j := 0; j < idNum/iterNum; j++ {
    85  			go func() {
    86  				idChan <- gen.newSpanID()
    87  				wg.Done()
    88  			}()
    89  		}
    90  	}
    91  	wg.Wait()
    92  	close(idChan)
    93  
    94  	expectedGen := newRandomIDGenerator(seed)
    95  	expectedIDs := make([]SpanID, idNum)
    96  	actualIDs := make([]SpanID, idNum)
    97  	for id := range idChan {
    98  		expectedIDs = append(expectedIDs, expectedGen.newSpanID())
    99  		actualIDs = append(actualIDs, id)
   100  	}
   101  	require.ElementsMatch(t, expectedIDs, actualIDs)
   102  }