1// Copyright The OpenTelemetry 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
15package metric_test
16
17import (
18	"context"
19	"math/rand"
20	"testing"
21	"time"
22
23	"github.com/stretchr/testify/require"
24
25	"go.opentelemetry.io/otel/metric"
26	"go.opentelemetry.io/otel/metric/number"
27	"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
28)
29
30func TestStressInt64Histogram(t *testing.T) {
31	desc := metric.NewDescriptor("some_metric", metric.ValueRecorderInstrumentKind, number.Int64Kind)
32
33	alloc := histogram.New(2, &desc, []float64{25, 50, 75})
34	h, ckpt := &alloc[0], &alloc[1]
35
36	ctx, cancelFunc := context.WithCancel(context.Background())
37	defer cancelFunc()
38	go func() {
39		rnd := rand.New(rand.NewSource(time.Now().Unix()))
40		for {
41			select {
42			case <-ctx.Done():
43				return
44			default:
45				_ = h.Update(ctx, number.NewInt64Number(rnd.Int63()%100), &desc)
46			}
47		}
48	}()
49
50	startTime := time.Now()
51	for time.Since(startTime) < time.Second {
52		require.NoError(t, h.SynchronizedMove(ckpt, &desc))
53
54		b, _ := ckpt.Histogram()
55		c, _ := ckpt.Count()
56
57		var realCount int64
58		for _, c := range b.Counts {
59			v := int64(c)
60			realCount += v
61		}
62
63		if realCount != c {
64			t.Fail()
65		}
66	}
67}
68