| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/counter/counter_test.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package counter |
| 8 |
|
| 9 |
import ( |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestNextExporterID(t *testing.T) { |
| 15 |
SetExporterID(0) |
| 16 |
|
| 17 |
var expected int64 |
| 18 |
for range 10 { |
| 19 |
id := NextExporterID() |
| 20 |
if id != expected { |
| 21 |
t.Errorf("NextExporterID() = %d; want %d", id, expected) |
| 22 |
} |
| 23 |
expected++ |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestSetExporterID(t *testing.T) { |
| 28 |
SetExporterID(0) |
| 29 |
|
| 30 |
prev := SetExporterID(42) |
| 31 |
if prev != 0 { |
| 32 |
t.Errorf("SetExporterID(42) returned %d; want 0", prev) |
| 33 |
} |
| 34 |
|
| 35 |
id := NextExporterID() |
| 36 |
if id != 42 { |
| 37 |
t.Errorf("NextExporterID() = %d; want 42", id) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestNextExporterIDConcurrentSafe(t *testing.T) { |
| 42 |
SetExporterID(0) |
| 43 |
|
| 44 |
const goroutines = 100 |
| 45 |
const increments = 10 |
| 46 |
|
| 47 |
var wg sync.WaitGroup |
| 48 |
wg.Add(goroutines) |
| 49 |
|
| 50 |
for range goroutines { |
| 51 |
go func() { |
| 52 |
defer wg.Done() |
| 53 |
for range increments { |
| 54 |
NextExporterID() |
| 55 |
} |
| 56 |
}() |
| 57 |
} |
| 58 |
|
| 59 |
wg.Wait() |
| 60 |
|
| 61 |
expected := int64(goroutines * increments) |
| 62 |
if id := NextExporterID(); id != expected { |
| 63 |
t.Errorf("NextExporterID() = %d; want %d", id, expected) |
| 64 |
} |
| 65 |
} |