Skip to content

Commit d09a0cc

Browse files
authored
[processor/deltatocumulativeprocessor] Adjust equal test to not compare functions (#38008)
#### Description With the collector now on Go 1.23 I'm working on adding iterator support for pdata at open-telemetry/opentelemetry-collector#12380 but the EqualT_test fails when comparing the iterators returned by All. Since the reflect value of an iter.Seq/iter.Seq2 is reflect.Func, they are not comparable in the usual sense. The go-cmp/cmp is still on Go 1.21 so it does not know about iterators. There is `x/exp/xiter.Equal` but it is still an open proposal. See https://go.googlesource.com/go/+/81c66e71d480ae2372b7eea4bcdf600b50fdd5e1/src/reflect/deepequal.go#158. Examples of the test failures: ```txt expo_test.go:203: Attributes().All(): 0xd1b400 != 0xd1b400 expo_test.go:203: Exemplars().All(): 0xe2be20 != 0xe2be20 ``` This PR adjusts the equal logic to skip the equality check for functions. #### Link to tracking issue For open-telemetry/opentelemetry-collector#12380
1 parent 22e75c8 commit d09a0cc

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

processor/deltatocumulativeprocessor/internal/data/datatest/equal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func New(tb testing.TB) T {
3131
// - exactly zero input arguments
3232
// - exactly one return value
3333
// - does not start with 'Append'
34+
// - does not start with 'Clone'
35+
// - does not return a function
3436
//
3537
// If this yields differences, those are reported and the test fails.
3638
// If the compared values are [pmetric.ExponentialHistogramDataPoint], then
@@ -78,6 +80,13 @@ func equal(tb testing.TB, want, got any, name string) bool {
7880
if mw.Type().NumIn() != 0 || mw.Type().NumOut() != 1 {
7981
continue
8082
}
83+
84+
// skip equality check for methods returning functions
85+
ret := mw.Type().Out(0)
86+
if ret.Kind() == reflect.Func {
87+
continue
88+
}
89+
8190
// Append(Empty) fails above heuristic, exclude it
8291
if strings.HasPrefix(mname, "Append") || strings.HasPrefix(mname, "Clone") {
8392
continue

processor/deltatocumulativeprocessor/internal/data/datatest/equal_test.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package datatest
55

66
import (
77
"fmt"
8+
"iter"
89
"path/filepath"
910
"runtime"
1011
"strconv"
@@ -35,11 +36,43 @@ func ExampleT_Equal() {
3536
is.Equal(want, got)
3637

3738
// Output:
38-
// equal_test.go:35: Negative().BucketCounts().AsRaw(): [1 1 2] != [4]
39-
// equal_test.go:35: Negative().BucketCounts().Len(): 3 != 1
40-
// equal_test.go:35: Positive().BucketCounts().AsRaw(): [1 1 2] != [4]
41-
// equal_test.go:35: Positive().BucketCounts().Len(): 3 != 1
42-
// equal_test.go:35: Scale(): 0 != 1
39+
// equal_test.go:36: Negative().BucketCounts().AsRaw(): [1 1 2] != [4]
40+
// equal_test.go:36: Negative().BucketCounts().Len(): 3 != 1
41+
// equal_test.go:36: Positive().BucketCounts().AsRaw(): [1 1 2] != [4]
42+
// equal_test.go:36: Positive().BucketCounts().Len(): 3 != 1
43+
// equal_test.go:36: Scale(): 0 != 1
44+
}
45+
46+
type structFunc struct {
47+
a int
48+
}
49+
50+
func (s structFunc) Get() int {
51+
return s.a
52+
}
53+
54+
func (s structFunc) Func() func() {
55+
return func() {}
56+
}
57+
58+
// iter.Seq is a reflect.Func
59+
func (s structFunc) Seq() iter.Seq[int] {
60+
return func(_ func(v int) bool) {
61+
}
62+
}
63+
64+
// iter.Seq2 is a reflect.Func
65+
func (s structFunc) Seq2() iter.Seq2[int, string] {
66+
return func(_ func(k int, v string) bool) {
67+
}
68+
}
69+
70+
func TestEqualMethodIgnoreFuncReturnType(t *testing.T) {
71+
is := datatest.New(t)
72+
s := structFunc{a: 42}
73+
want := any(s)
74+
got := any(s)
75+
is.Equal(want, got)
4376
}
4477

4578
func TestNone(*testing.T) {}

0 commit comments

Comments
 (0)