|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package streams_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" |
| 12 | + exp "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/streams" |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/data" |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/streams" |
| 15 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/deltatocumulativeprocessor/internal/testdata/random" |
| 16 | +) |
| 17 | + |
| 18 | +func TestLimit(t *testing.T) { |
| 19 | + sum := random.Sum() |
| 20 | + |
| 21 | + items := make(exp.HashMap[data.Number]) |
| 22 | + lim := streams.Limit(items, 10) |
| 23 | + |
| 24 | + ids := make([]identity.Stream, 10) |
| 25 | + |
| 26 | + // write until limit must work |
| 27 | + for i := 0; i < 10; i++ { |
| 28 | + id, dp := sum.Stream() |
| 29 | + ids[i] = id |
| 30 | + err := lim.Store(id, dp) |
| 31 | + require.NoError(t, err) |
| 32 | + } |
| 33 | + |
| 34 | + // one over limit must be rejected |
| 35 | + { |
| 36 | + id, dp := sum.Stream() |
| 37 | + err := lim.Store(id, dp) |
| 38 | + want := streams.ErrLimit(10) |
| 39 | + require.ErrorAs(t, err, &want) |
| 40 | + require.True(t, streams.AtLimit(err)) |
| 41 | + } |
| 42 | + |
| 43 | + // after removing one, must be accepted again |
| 44 | + { |
| 45 | + lim.Delete(ids[0]) |
| 46 | + |
| 47 | + id, dp := sum.Stream() |
| 48 | + err := lim.Store(id, dp) |
| 49 | + require.NoError(t, err) |
| 50 | + } |
| 51 | +} |
0 commit comments