github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/telemetry/features_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package telemetry_test
    12  
    13  import (
    14  	"sync"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/server/telemetry"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  // TestGetCounterDoesNotRace ensures that concurrent calls to GetCounter for
    22  // the same feature always returns the same pointer. Even when this race was
    23  // possible this test would fail on every run but would fail rapidly under
    24  // stressrace.
    25  func TestGetCounterDoesNotRace(t *testing.T) {
    26  	const N = 100
    27  	var wg sync.WaitGroup
    28  	wg.Add(N)
    29  	counters := make([]telemetry.Counter, N)
    30  	for i := 0; i < N; i++ {
    31  		go func(i int) {
    32  			counters[i] = telemetry.GetCounter("test.foo")
    33  			wg.Done()
    34  		}(i)
    35  	}
    36  	wg.Wait()
    37  	counterSet := make(map[telemetry.Counter]struct{})
    38  	for _, c := range counters {
    39  		counterSet[c] = struct{}{}
    40  	}
    41  	require.Len(t, counterSet, 1)
    42  }