...

Source file src/cmd/vendor/golang.org/x/telemetry/counter/countertest/countertest.go

Documentation: cmd/vendor/golang.org/x/telemetry/counter/countertest

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // countertest provides testing utilities for counters.
     6  // This package cannot be used except for testing.
     7  package countertest
     8  
     9  import (
    10  	"sync"
    11  
    12  	"golang.org/x/telemetry/counter"
    13  	ic "golang.org/x/telemetry/internal/counter"
    14  	"golang.org/x/telemetry/internal/telemetry"
    15  )
    16  
    17  var (
    18  	openedMu sync.Mutex
    19  	opened   bool
    20  )
    21  
    22  // SupportedPlatform reports if this platform supports Open()
    23  const SupportedPlatform = !telemetry.DisabledOnPlatform
    24  
    25  func isOpen() bool {
    26  	openedMu.Lock()
    27  	defer openedMu.Unlock()
    28  	return opened
    29  }
    30  
    31  // Open enables telemetry data writing to disk.
    32  // This is supposed to be called once during the program execution
    33  // (i.e. typically in TestMain), and must not be used with
    34  // golang.org/x/telemetry/counter.Open.
    35  func Open(telemetryDir string) {
    36  	openedMu.Lock()
    37  	defer openedMu.Unlock()
    38  	if opened {
    39  		panic("Open was called more than once")
    40  	}
    41  	telemetry.Default = telemetry.NewDir(telemetryDir)
    42  
    43  	// TODO(rfindley): reinstate test coverage with counter rotation enabled.
    44  	// Before the [counter.Open] and [counter.OpenAndRotate] APIs were split,
    45  	// this called counter.Open (which rotated!).
    46  	counter.Open()
    47  	opened = true
    48  }
    49  
    50  // ReadCounter reads the given counter.
    51  func ReadCounter(c *counter.Counter) (count uint64, _ error) {
    52  	return ic.Read(c)
    53  }
    54  
    55  // ReadStackCounter reads the given StackCounter.
    56  func ReadStackCounter(c *counter.StackCounter) (stackCounts map[string]uint64, _ error) {
    57  	return ic.ReadStack(c)
    58  }
    59  
    60  // ReadFile reads the counters and stack counters from the given file.
    61  func ReadFile(name string) (counters, stackCounters map[string]uint64, _ error) {
    62  	return ic.ReadFile(name)
    63  }
    64  

View as plain text