2019-10-29 22:27:22 +02:00
|
|
|
// Copyright 2019, OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package counter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-06 20:08:40 +02:00
|
|
|
"os"
|
2019-10-29 22:27:22 +02:00
|
|
|
"testing"
|
2020-01-06 20:08:40 +02:00
|
|
|
"unsafe"
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
2020-01-06 20:08:40 +02:00
|
|
|
ottest "go.opentelemetry.io/otel/internal/testing"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/aggregator/test"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const count = 100
|
|
|
|
|
2020-01-06 20:08:40 +02:00
|
|
|
// Ensure struct alignment prior to running tests.
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
fields := []ottest.FieldOffset{
|
|
|
|
{
|
|
|
|
Name: "Aggregator.current",
|
|
|
|
Offset: unsafe.Offsetof(Aggregator{}.current),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Aggregator.checkpoint",
|
|
|
|
Offset: unsafe.Offsetof(Aggregator{}.checkpoint),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if !ottest.Aligned8Byte(fields, os.Stderr) {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
func TestCounterMonotonic(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg := New()
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
descriptor := test.NewAggregatorTest(export.CounterKind, profile.NumberKind, false)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
sum := core.Number(0)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
sum.AddNumber(profile.NumberKind, x)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg, x, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
agg.Checkpoint(ctx, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
asum, err := agg.Sum()
|
|
|
|
require.Equal(t, sum, asum, "Same sum - monotonic")
|
|
|
|
require.Nil(t, err)
|
2019-10-29 22:27:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCounterMonotonicNegative(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg := New()
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
descriptor := test.NewAggregatorTest(export.CounterKind, profile.NumberKind, false)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg, profile.Random(-1), descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sum := profile.Random(+1)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg, sum, descriptor)
|
|
|
|
agg.Checkpoint(ctx, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
asum, err := agg.Sum()
|
|
|
|
require.Equal(t, sum, asum, "Same sum - monotonic")
|
|
|
|
require.Nil(t, err)
|
2019-10-29 22:27:22 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCounterNonMonotonic(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg := New()
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
descriptor := test.NewAggregatorTest(export.CounterKind, profile.NumberKind, true)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
sum := core.Number(0)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
y := profile.Random(-1)
|
|
|
|
sum.AddNumber(profile.NumberKind, x)
|
|
|
|
sum.AddNumber(profile.NumberKind, y)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg, x, descriptor)
|
|
|
|
test.CheckedUpdate(t, agg, y, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
agg.Checkpoint(ctx, descriptor)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
asum, err := agg.Sum()
|
|
|
|
require.Equal(t, sum, asum, "Same sum - monotonic")
|
|
|
|
require.Nil(t, err)
|
2019-10-29 22:27:22 +02:00
|
|
|
})
|
|
|
|
}
|
2019-10-31 07:15:27 +02:00
|
|
|
|
|
|
|
func TestCounterMerge(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
test.RunProfiles(t, func(t *testing.T, profile test.Profile) {
|
|
|
|
agg1 := New()
|
|
|
|
agg2 := New()
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
descriptor := test.NewAggregatorTest(export.CounterKind, profile.NumberKind, false)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
|
|
|
sum := core.Number(0)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
x := profile.Random(+1)
|
|
|
|
sum.AddNumber(profile.NumberKind, x)
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedUpdate(t, agg1, x, descriptor)
|
|
|
|
test.CheckedUpdate(t, agg2, x, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
agg1.Checkpoint(ctx, descriptor)
|
|
|
|
agg2.Checkpoint(ctx, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
test.CheckedMerge(t, agg1, agg2, descriptor)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
sum.AddNumber(descriptor.NumberKind(), sum)
|
2019-10-31 07:15:27 +02:00
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
asum, err := agg1.Sum()
|
|
|
|
require.Equal(t, sum, asum, "Same sum - monotonic")
|
|
|
|
require.Nil(t, err)
|
2019-10-31 07:15:27 +02:00
|
|
|
})
|
|
|
|
}
|