| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package trace |
| 5 |
|
| 6 |
import ( |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/go-logr/logr" |
| 11 |
"github.com/go-logr/logr/funcr" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/internal/global" |
| 15 |
) |
| 16 |
|
| 17 |
func init() { |
| 18 |
} |
| 19 |
|
| 20 |
func TestAdd(t *testing.T) { |
| 21 |
q := newEvictedQueueLink(3) |
| 22 |
q.add(Link{}) |
| 23 |
q.add(Link{}) |
| 24 |
if wantLen, gotLen := 2, len(q.queue); wantLen != gotLen { |
| 25 |
t.Errorf("got queue length %d want %d", gotLen, wantLen) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func TestCopy(t *testing.T) { |
| 30 |
q := newEvictedQueueEvent(3) |
| 31 |
q.add(Event{Name: "value1"}) |
| 32 |
cp := q.copy() |
| 33 |
|
| 34 |
q.add(Event{Name: "value2"}) |
| 35 |
assert.Equal(t, []Event{{Name: "value1"}}, cp, "queue update modified copy") |
| 36 |
|
| 37 |
cp[0] = Event{Name: "value0"} |
| 38 |
assert.Equal(t, Event{Name: "value1"}, q.queue[0], "copy update modified queue") |
| 39 |
} |
| 40 |
|
| 41 |
func TestDropCount(t *testing.T) { |
| 42 |
q := newEvictedQueueEvent(3) |
| 43 |
|
| 44 |
var called int |
| 45 |
t.Cleanup(func(l logr.Logger) func() { |
| 46 |
return func() { global.SetLogger(l) } |
| 47 |
}(global.GetLogger())) |
| 48 |
global.SetLogger(funcr.New(func(string, string) { |
| 49 |
called++ |
| 50 |
}, funcr.Options{Verbosity: 1})) |
| 51 |
|
| 52 |
q.add(Event{Name: "value1"}) |
| 53 |
assert.Equal(t, 0, called, `"value1" logged as dropped`) |
| 54 |
q.add(Event{Name: "value2"}) |
| 55 |
assert.Equal(t, 0, called, `"value2" logged as dropped`) |
| 56 |
q.add(Event{Name: "value3"}) |
| 57 |
assert.Equal(t, 0, called, `"value3" logged as dropped`) |
| 58 |
q.add(Event{Name: "value1"}) |
| 59 |
assert.Equal(t, 1, called, `"value2" not logged as dropped`) |
| 60 |
q.add(Event{Name: "value4"}) |
| 61 |
assert.Equal(t, 1, called, `"value4" logged as dropped`) |
| 62 |
if wantLen, gotLen := 3, len(q.queue); wantLen != gotLen { |
| 63 |
t.Errorf("got queue length %d want %d", gotLen, wantLen) |
| 64 |
} |
| 65 |
if wantDropCount, gotDropCount := 2, q.droppedCount; wantDropCount != gotDropCount { |
| 66 |
t.Errorf("got drop count %d want %d", gotDropCount, wantDropCount) |
| 67 |
} |
| 68 |
wantArr := []Event{{Name: "value3"}, {Name: "value1"}, {Name: "value4"}} |
| 69 |
gotArr := q.copy() |
| 70 |
|
| 71 |
if wantLen, gotLen := len(wantArr), len(gotArr); gotLen != wantLen { |
| 72 |
t.Errorf("got array len %d want %d", gotLen, wantLen) |
| 73 |
} |
| 74 |
|
| 75 |
if !reflect.DeepEqual(gotArr, wantArr) { |
| 76 |
t.Errorf("got array = %#v; want %#v", gotArr, wantArr) |
| 77 |
} |
| 78 |
} |