Debian Codegraph @

otelcol/buffered_core_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelcol
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "go.uber.org/zap/zapcore"
12 )
13
14 func Test_bufferedCore_Level(t *testing.T) {
15 bc := newBufferedCore(zapcore.InfoLevel)
16 assert.Equal(t, zapcore.InfoLevel, bc.Level())
17 }
18
19 func Test_bufferedCore_Check(t *testing.T) {
20 t.Run("check passed", func(t *testing.T) {
21 bc := newBufferedCore(zapcore.InfoLevel)
22 e := zapcore.Entry{
23 Level: zapcore.InfoLevel,
24 }
25 expected := &zapcore.CheckedEntry{}
26 expected = expected.AddCore(e, bc)
27 ce := bc.Check(e, nil)
28 assert.Equal(t, expected, ce)
29 })
30
31 t.Run("check did not pass", func(t *testing.T) {
32 bc := newBufferedCore(zapcore.InfoLevel)
33 e := zapcore.Entry{
34 Level: zapcore.DebugLevel,
35 }
36 ce := bc.Check(e, nil)
37 assert.Nil(t, ce)
38 })
39 }
40
41 func Test_bufferedCore_With(t *testing.T) {
42 bc := newBufferedCore(zapcore.InfoLevel)
43 bc.logsTaken = true
44 bc.context = []zapcore.Field{
45 {Key: "original", String: "context"},
46 }
47 inputs := []zapcore.Field{
48 {Key: "test", String: "passed"},
49 }
50 expected := []zapcore.Field{
51 {Key: "original", String: "context"},
52 {Key: "test", String: "passed"},
53 }
54 newBC := bc.With(inputs)
55 assert.Equal(t, expected, newBC.(*bufferedCore).context)
56 assert.True(t, newBC.(*bufferedCore).logsTaken)
57 }
58
59 func Test_bufferedCore_Write(t *testing.T) {
60 bc := newBufferedCore(zapcore.InfoLevel)
61 e := zapcore.Entry{
62 Level: zapcore.DebugLevel,
63 Message: "test",
64 }
65 fields := []zapcore.Field{
66 {Key: "field1", String: "value1"},
67 }
68 err := bc.Write(e, fields)
69 require.NoError(t, err)
70
71 expected := loggedEntry{
72 e,
73 fields,
74 }
75 require.Len(t, bc.logs, 1)
76 require.Equal(t, expected, bc.logs[0])
77 }
78
79 func Test_bufferedCore_Sync(t *testing.T) {
80 bc := newBufferedCore(zapcore.InfoLevel)
81 assert.NoError(t, bc.Sync())
82 }
83
84 func Test_bufferedCore_TakeLogs(t *testing.T) {
85 bc := newBufferedCore(zapcore.InfoLevel)
86 e := zapcore.Entry{
87 Level: zapcore.DebugLevel,
88 Message: "test",
89 }
90 fields := []zapcore.Field{
91 {Key: "field1", String: "value1"},
92 }
93 err := bc.Write(e, fields)
94 require.NoError(t, err)
95
96 expected := []loggedEntry{
97 {
98 e,
99 fields,
100 },
101 }
102 assert.Equal(t, expected, bc.TakeLogs())
103 assert.Nil(t, bc.logs)
104
105 require.Error(t, bc.Write(e, fields))
106 assert.Nil(t, bc.TakeLogs())
107 }