forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_test.go
114 lines (90 loc) · 2.28 KB
/
simple_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package log_test
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/sdk/log"
)
type exporter struct {
records []log.Record
exportCalled bool
shutdownCalled bool
forceFlushCalled bool
}
func (e *exporter) Export(_ context.Context, r []log.Record) error {
e.records = r
e.exportCalled = true
return nil
}
func (e *exporter) Shutdown(context.Context) error {
e.shutdownCalled = true
return nil
}
func (e *exporter) ForceFlush(context.Context) error {
e.forceFlushCalled = true
return nil
}
func TestSimpleProcessorOnEmit(t *testing.T) {
e := new(exporter)
s := log.NewSimpleProcessor(e)
var r log.Record
r.SetSeverityText("test")
nextR, _ := s.OnEmit(context.Background(), r)
require.True(t, e.exportCalled, "exporter Export not called")
assert.Equal(t, []log.Record{r}, e.records)
assert.Equal(t, r, nextR)
}
func TestSimpleProcessorEnabled(t *testing.T) {
s := log.NewSimpleProcessor(nil)
assert.True(t, s.Enabled(context.Background(), log.Record{}))
}
func TestSimpleProcessorShutdown(t *testing.T) {
e := new(exporter)
s := log.NewSimpleProcessor(e)
_ = s.Shutdown(context.Background())
require.True(t, e.shutdownCalled, "exporter Shutdown not called")
}
func TestSimpleProcessorForceFlush(t *testing.T) {
e := new(exporter)
s := log.NewSimpleProcessor(e)
_ = s.ForceFlush(context.Background())
require.True(t, e.forceFlushCalled, "exporter ForceFlush not called")
}
func TestSimpleProcessorConcurrentSafe(t *testing.T) {
const goRoutineN = 10
var wg sync.WaitGroup
wg.Add(goRoutineN)
var r log.Record
r.SetSeverityText("test")
ctx := context.Background()
s := log.NewSimpleProcessor(nil)
for i := 0; i < goRoutineN; i++ {
go func() {
defer wg.Done()
_, _ = s.OnEmit(ctx, r)
_ = s.Enabled(ctx, r)
_ = s.Shutdown(ctx)
_ = s.ForceFlush(ctx)
}()
}
wg.Wait()
}
func BenchmarkSimpleProcessorOnEmit(b *testing.B) {
var r log.Record
r.SetSeverityText("test")
ctx := context.Background()
s := log.NewSimpleProcessor(nil)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
var out error
for pb.Next() {
r, out = s.OnEmit(ctx, r)
}
_ = out
})
}