Debian Codegraph @

metric/noop/noop_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package noop // import "go.opentelemetry.io/otel/metric/noop"
5
6 import (
7 "reflect"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11
12 "go.opentelemetry.io/otel/metric"
13 )
14
15 func TestImplementationNoPanics(t *testing.T) {
16 // Check that if type has an embedded interface and that interface has
17 // methods added to it than the No-Op implementation implements them.
18 t.Run("MeterProvider", assertAllExportedMethodNoPanic(
19 reflect.ValueOf(MeterProvider{}),
20 reflect.TypeFor[metric.MeterProvider](),
21 ))
22 t.Run("Meter", assertAllExportedMethodNoPanic(
23 reflect.ValueOf(Meter{}),
24 reflect.TypeFor[metric.Meter](),
25 ))
26 t.Run("Observer", assertAllExportedMethodNoPanic(
27 reflect.ValueOf(Observer{}),
28 reflect.TypeFor[metric.Observer](),
29 ))
30 t.Run("Registration", assertAllExportedMethodNoPanic(
31 reflect.ValueOf(Registration{}),
32 reflect.TypeFor[metric.Registration](),
33 ))
34 t.Run("Int64Counter", assertAllExportedMethodNoPanic(
35 reflect.ValueOf(Int64Counter{}),
36 reflect.TypeFor[metric.Int64Counter](),
37 ))
38 t.Run("Float64Counter", assertAllExportedMethodNoPanic(
39 reflect.ValueOf(Float64Counter{}),
40 reflect.TypeFor[metric.Float64Counter](),
41 ))
42 t.Run("Int64UpDownCounter", assertAllExportedMethodNoPanic(
43 reflect.ValueOf(Int64UpDownCounter{}),
44 reflect.TypeFor[metric.Int64UpDownCounter](),
45 ))
46 t.Run("Float64UpDownCounter", assertAllExportedMethodNoPanic(
47 reflect.ValueOf(Float64UpDownCounter{}),
48 reflect.TypeFor[metric.Float64UpDownCounter](),
49 ))
50 t.Run("Int64Histogram", assertAllExportedMethodNoPanic(
51 reflect.ValueOf(Int64Histogram{}),
52 reflect.TypeFor[metric.Int64Histogram](),
53 ))
54 t.Run("Float64Histogram", assertAllExportedMethodNoPanic(
55 reflect.ValueOf(Float64Histogram{}),
56 reflect.TypeFor[metric.Float64Histogram](),
57 ))
58 t.Run("Int64Gauge", assertAllExportedMethodNoPanic(
59 reflect.ValueOf(Int64Gauge{}),
60 reflect.TypeFor[metric.Int64Gauge](),
61 ))
62 t.Run("Float64Gauge", assertAllExportedMethodNoPanic(
63 reflect.ValueOf(Float64Gauge{}),
64 reflect.TypeFor[metric.Float64Gauge](),
65 ))
66 t.Run("Int64ObservableCounter", assertAllExportedMethodNoPanic(
67 reflect.ValueOf(Int64ObservableCounter{}),
68 reflect.TypeFor[metric.Int64ObservableCounter](),
69 ))
70 t.Run("Float64ObservableCounter", assertAllExportedMethodNoPanic(
71 reflect.ValueOf(Float64ObservableCounter{}),
72 reflect.TypeFor[metric.Float64ObservableCounter](),
73 ))
74 t.Run("Int64ObservableGauge", assertAllExportedMethodNoPanic(
75 reflect.ValueOf(Int64ObservableGauge{}),
76 reflect.TypeFor[metric.Int64ObservableGauge](),
77 ))
78 t.Run("Float64ObservableGauge", assertAllExportedMethodNoPanic(
79 reflect.ValueOf(Float64ObservableGauge{}),
80 reflect.TypeFor[metric.Float64ObservableGauge](),
81 ))
82 t.Run("Int64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
83 reflect.ValueOf(Int64ObservableUpDownCounter{}),
84 reflect.TypeFor[metric.Int64ObservableUpDownCounter](),
85 ))
86 t.Run("Float64ObservableUpDownCounter", assertAllExportedMethodNoPanic(
87 reflect.ValueOf(Float64ObservableUpDownCounter{}),
88 reflect.TypeFor[metric.Float64ObservableUpDownCounter](),
89 ))
90 t.Run("Int64Observer", assertAllExportedMethodNoPanic(
91 reflect.ValueOf(Int64Observer{}),
92 reflect.TypeFor[metric.Int64Observer](),
93 ))
94 t.Run("Float64Observer", assertAllExportedMethodNoPanic(
95 reflect.ValueOf(Float64Observer{}),
96 reflect.TypeFor[metric.Float64Observer](),
97 ))
98 }
99
100 func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) {
101 return func(t *testing.T) {
102 for n := 0; n < rType.NumMethod(); n++ {
103 mType := rType.Method(n)
104 if !mType.IsExported() {
105 t.Logf("ignoring unexported %s", mType.Name)
106 continue
107 }
108 m := rVal.MethodByName(mType.Name)
109 if !m.IsValid() {
110 t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name)
111 }
112
113 numIn := mType.Type.NumIn()
114 if mType.Type.IsVariadic() {
115 numIn--
116 }
117 args := make([]reflect.Value, numIn)
118 for i := range args {
119 aType := mType.Type.In(i)
120 args[i] = reflect.New(aType).Elem()
121 }
122
123 assert.NotPanicsf(t, func() {
124 _ = m.Call(args)
125 }, "%s.%s", rVal.Type().Name(), mType.Name)
126 }
127 }
128 }
129
130 func TestNewMeterProvider(t *testing.T) {
131 mp := NewMeterProvider()
132 assert.Equal(t, MeterProvider{}, mp)
133 meter := mp.Meter("")
134 assert.Equal(t, Meter{}, meter)
135 }