...

Source file src/go.opencensus.io/trace/config_test.go

Documentation: go.opencensus.io/trace

     1  // Copyright 2018, OpenCensus 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 trace
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func TestApplyConfig(t *testing.T) {
    23  	cfg := config.Load().(*Config)
    24  	defaultCfg := Config{
    25  		DefaultSampler:             cfg.DefaultSampler,
    26  		IDGenerator:                cfg.IDGenerator,
    27  		MaxAttributesPerSpan:       DefaultMaxAttributesPerSpan,
    28  		MaxAnnotationEventsPerSpan: DefaultMaxAnnotationEventsPerSpan,
    29  		MaxMessageEventsPerSpan:    DefaultMaxMessageEventsPerSpan,
    30  		MaxLinksPerSpan:            DefaultMaxLinksPerSpan,
    31  	}
    32  	testCases := []struct {
    33  		name    string
    34  		newCfg  Config
    35  		wantCfg Config
    36  	}{
    37  		{
    38  			name:    "Initialize to default config",
    39  			newCfg:  defaultCfg,
    40  			wantCfg: defaultCfg,
    41  		},
    42  		{
    43  			name:    "Empty Config",
    44  			newCfg:  Config{},
    45  			wantCfg: defaultCfg,
    46  		},
    47  		{
    48  			name: "Valid non-default config",
    49  			newCfg: Config{
    50  				MaxAttributesPerSpan:       1,
    51  				MaxAnnotationEventsPerSpan: 2,
    52  				MaxMessageEventsPerSpan:    3,
    53  				MaxLinksPerSpan:            4,
    54  			},
    55  			wantCfg: Config{
    56  				DefaultSampler:             cfg.DefaultSampler,
    57  				IDGenerator:                cfg.IDGenerator,
    58  				MaxAttributesPerSpan:       1,
    59  				MaxAnnotationEventsPerSpan: 2,
    60  				MaxMessageEventsPerSpan:    3,
    61  				MaxLinksPerSpan:            4,
    62  			},
    63  		},
    64  		{
    65  			name: "Partially invalid config",
    66  			newCfg: Config{
    67  				MaxAttributesPerSpan:       -1,
    68  				MaxAnnotationEventsPerSpan: 3,
    69  				MaxMessageEventsPerSpan:    -3,
    70  				MaxLinksPerSpan:            5,
    71  			},
    72  			wantCfg: Config{
    73  				DefaultSampler:             cfg.DefaultSampler,
    74  				IDGenerator:                cfg.IDGenerator,
    75  				MaxAttributesPerSpan:       1,
    76  				MaxAnnotationEventsPerSpan: 3,
    77  				MaxMessageEventsPerSpan:    3,
    78  				MaxLinksPerSpan:            5,
    79  			},
    80  		},
    81  	}
    82  
    83  	for i, tt := range testCases {
    84  		newCfg := tt.newCfg
    85  		ApplyConfig(newCfg)
    86  		gotCfg := config.Load().(*Config)
    87  		wantCfg := tt.wantCfg
    88  
    89  		if got, want := reflect.ValueOf(gotCfg.DefaultSampler).Pointer(), reflect.ValueOf(wantCfg.DefaultSampler).Pointer(); got != want {
    90  			t.Fatalf("testId = %d, testName = %s: config.DefaultSampler = %#v; want %#v", i, tt.name, got, want)
    91  		}
    92  		if got, want := gotCfg.IDGenerator, wantCfg.IDGenerator; got != want {
    93  			t.Fatalf("testId = %d, testName = %s: config.IDGenerator = %#v; want %#v", i, tt.name, got, want)
    94  		}
    95  		if got, want := gotCfg.MaxAttributesPerSpan, wantCfg.MaxAttributesPerSpan; got != want {
    96  			t.Fatalf("testId = %d, testName = %s: config.MaxAttributesPerSpan = %#v; want %#v", i, tt.name, got, want)
    97  		}
    98  		if got, want := gotCfg.MaxLinksPerSpan, wantCfg.MaxLinksPerSpan; got != want {
    99  			t.Fatalf("testId = %d, testName = %s: config.MaxLinksPerSpan = %#v; want %#v", i, tt.name, got, want)
   100  		}
   101  		if got, want := gotCfg.MaxAnnotationEventsPerSpan, wantCfg.MaxAnnotationEventsPerSpan; got != want {
   102  			t.Fatalf("testId = %d, testName = %s: config.MaxAnnotationEventsPerSpan = %#v; want %#v", i, tt.name, got, want)
   103  		}
   104  		if got, want := gotCfg.MaxMessageEventsPerSpan, wantCfg.MaxMessageEventsPerSpan; got != want {
   105  			t.Fatalf("testId = %d, testName = %s: config.MaxMessageEventsPerSpan = %#v; want %#v", i, tt.name, got, want)
   106  		}
   107  
   108  	}
   109  }
   110  

View as plain text