trpc.group/trpc-go/trpc-go@v1.0.3/rpcz/sampler_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" 18 "reflect" 19 "testing" 20 ) 21 22 func Test_newSpanIDRatioSampler(t *testing.T) { 23 const ( 24 maxFraction = 1.0 25 minFraction = 0.0 26 maxUpperBound = math.MaxInt64 27 ) 28 type args struct { 29 fraction float64 30 } 31 tests := []struct { 32 name string 33 args args 34 want *spanIDRatioSampler 35 }{ 36 { 37 name: "fraction equals maxFraction", 38 args: args{fraction: maxFraction}, 39 want: &spanIDRatioSampler{spanIDUpperBound: maxUpperBound}, 40 }, 41 { 42 name: "fraction greater than maxFraction", 43 args: args{fraction: 1.20221111}, 44 want: &spanIDRatioSampler{spanIDUpperBound: maxUpperBound}, 45 }, 46 { 47 name: "fraction equals minFraction", 48 args: args{fraction: minFraction}, 49 want: &spanIDRatioSampler{spanIDUpperBound: 0}, 50 }, 51 { 52 name: "fraction less than minFraction", 53 args: args{fraction: -0.20221111}, 54 want: &spanIDRatioSampler{spanIDUpperBound: 0}, 55 }, 56 { 57 name: "fraction between minFraction and maxFraction", 58 args: args{fraction: 0.20221111}, 59 want: &spanIDRatioSampler{spanIDUpperBound: int64(float64(0.20221111) * maxUpperBound)}, 60 }, 61 { 62 name: "fraction is very close to 1", 63 args: args{fraction: 0.99999999999}, 64 want: &spanIDRatioSampler{spanIDUpperBound: int64(float64(0.99999999999) * maxUpperBound)}, 65 }, 66 } 67 for _, tt := range tests { 68 t.Run(tt.name, func(t *testing.T) { 69 if got := newSpanIDRatioSampler(tt.args.fraction); !reflect.DeepEqual(got, tt.want) { 70 t.Errorf("newSpanIDRatioSampler() = %v, want %v", got, tt.want) 71 } 72 }) 73 } 74 } 75 76 func Test_spanIDRatioSampler_ShouldSample(t *testing.T) { 77 type fields struct { 78 spanIDUpperBound int64 79 } 80 type args struct { 81 id SpanID 82 } 83 tests := []struct { 84 name string 85 fields fields 86 args args 87 want bool 88 }{ 89 { 90 name: "ID equals spanIDUpperBound", 91 fields: fields{spanIDUpperBound: 20221111}, 92 args: args{id: 20221111}, 93 want: false, 94 }, 95 { 96 name: "ID less than spanIDUpperBound", 97 fields: fields{spanIDUpperBound: 20221111}, 98 args: args{id: 20211111}, 99 want: true, 100 }, 101 {name: "ID greater than spanIDUpperBound", 102 fields: fields{spanIDUpperBound: 20221111}, 103 args: args{id: 20231111}, 104 want: false, 105 }, 106 } 107 for _, tt := range tests { 108 t.Run(tt.name, func(t *testing.T) { 109 ss := spanIDRatioSampler{ 110 spanIDUpperBound: tt.fields.spanIDUpperBound, 111 } 112 if got := ss.shouldSample(tt.args.id); got != tt.want { 113 t.Errorf("shouldSample() = %v, want %v", got, tt.want) 114 } 115 }) 116 } 117 }