Skip to content

Commit

Permalink
[fix] Fix flaky unit test in attributes_test.go (#6785)
Browse files Browse the repository at this point in the history
## Description of the changes
- This PR fixes a flaky test that was being caused because a map is not
iterated in order and hence the insertion order of elements in a
`pcommon.Map` is not guaranteed

## How was this change tested?
- CI

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `npm run lint` and `npm run test`

---------

Signed-off-by: Mahad Zaryab <[email protected]>
  • Loading branch information
mahadzaryab1 authored Feb 28, 2025
1 parent 07430f7 commit 06cc410
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions internal/jptrace/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package jptrace
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
)

Expand Down Expand Up @@ -55,47 +55,52 @@ func TestAttributesToMap(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := AttributesToMap(test.attributes)
assert.Equal(t, test.expected, result)
require.Equal(t, test.expected, result)
})
}
}

func TestMapToAttributes(t *testing.T) {
tests := []struct {
name string
tags map[string]string
expected pcommon.Map
name string
tags map[string]string
requireFn func(t *testing.T, result pcommon.Map)
}{
{
name: "empty map",
tags: map[string]string{},
expected: pcommon.NewMap(),
name: "empty map",
tags: map[string]string{},
requireFn: func(t *testing.T, result pcommon.Map) {
require.Equal(t, 0, result.Len(), "Expected map to be empty")
},
},
{
name: "single tag",
tags: map[string]string{"key1": "value1"},
expected: func() pcommon.Map {
m := pcommon.NewMap()
m.PutStr("key1", "value1")
return m
}(),
requireFn: func(t *testing.T, result pcommon.Map) {
val, exists := result.Get("key1")
require.True(t, exists)
require.Equal(t, "value1", val.Str())
},
},
{
name: "multiple tags",
tags: map[string]string{"key1": "value1", "key2": "value2"},
expected: func() pcommon.Map {
m := pcommon.NewMap()
m.PutStr("key1", "value1")
m.PutStr("key2", "value2")
return m
}(),
requireFn: func(t *testing.T, result pcommon.Map) {
val, exists := result.Get("key1")
require.True(t, exists)
require.Equal(t, "value1", val.Str())

val, exists = result.Get("key2")
require.True(t, exists)
require.Equal(t, "value2", val.Str())
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := MapToAttributes(test.tags)
assert.Equal(t, test.expected, result)
test.requireFn(t, result)
})
}
}

0 comments on commit 06cc410

Please sign in to comment.