...

Source file src/go.etcd.io/etcd/server/v3/embed/config_tracing_test.go

Documentation: go.etcd.io/etcd/server/v3/embed

     1  // Copyright 2021 The etcd Authors
     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 embed
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  const neverSampleDescription = "AlwaysOffSampler"
    22  
    23  func TestDetermineSampler(t *testing.T) {
    24  	tests := []struct {
    25  		name                   string
    26  		sampleRate             int
    27  		wantSamplerDescription string
    28  	}{
    29  		{
    30  			name:                   "sample rate is disabled",
    31  			sampleRate:             0,
    32  			wantSamplerDescription: neverSampleDescription,
    33  		},
    34  		{
    35  			name:                   "sample rate is 100",
    36  			sampleRate:             100,
    37  			wantSamplerDescription: "TraceIDRatioBased{0.0001}",
    38  		},
    39  	}
    40  	for _, tc := range tests {
    41  		t.Run(tc.name, func(t *testing.T) {
    42  			sampler := determineSampler(tc.sampleRate)
    43  			if tc.wantSamplerDescription != sampler.Description() {
    44  				t.Errorf("tracing sampler was not as expected; expected sampler: %#+v, got sampler: %#+v", tc.wantSamplerDescription, sampler.Description())
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestTracingConfig(t *testing.T) {
    51  	tests := []struct {
    52  		name       string
    53  		sampleRate int
    54  		wantErr    bool
    55  	}{
    56  		{
    57  			name:       "invalid - sample rate is less than 0",
    58  			sampleRate: -1,
    59  			wantErr:    true,
    60  		},
    61  		{
    62  			name:       "invalid - sample rate is more than allowed value",
    63  			sampleRate: maxSamplingRatePerMillion + 1,
    64  			wantErr:    true,
    65  		},
    66  		{
    67  			name:       "valid - sample rate is 100",
    68  			sampleRate: 100,
    69  			wantErr:    false,
    70  		},
    71  	}
    72  	for _, tc := range tests {
    73  		t.Run(tc.name, func(t *testing.T) {
    74  			err := validateTracingConfig(tc.sampleRate)
    75  			if err == nil && tc.wantErr {
    76  				t.Errorf("expected error got (%v) error", err)
    77  			}
    78  			if err != nil && !tc.wantErr {
    79  				t.Errorf("expected no errors, got error: (%v)", err)
    80  			}
    81  		})
    82  	}
    83  }
    84  

View as plain text