-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathreceiver_leak_test.go
More file actions
120 lines (112 loc) · 3.44 KB
/
Copy pathreceiver_leak_test.go
File metadata and controls
120 lines (112 loc) · 3.44 KB
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
115
116
117
118
119
120
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package mbreceiver
import (
"errors"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"github.com/elastic/beats/v7/x-pack/otel/oteltest"
)
func TestLeak(t *testing.T) {
monitorSocket := genSocketPath()
var monitorHost string
if runtime.GOOS == "windows" {
monitorHost = "npipe:///" + filepath.Base(monitorSocket)
} else {
monitorHost = "unix://" + monitorSocket
}
config := Config{
Beatconfig: map[string]any{
"metricbeat": map[string]any{
"modules": []map[string]any{
{
"module": "system",
"enabled": true,
"period": "1s",
"processes": []string{".*"},
"metricsets": []string{"cpu"},
},
},
},
"output": map[string]any{
"otelconsumer": map[string]any{},
},
"logging": map[string]any{
"level": "debug",
"selectors": []string{
"*",
},
},
"path.home": t.TempDir(),
"http.enabled": true,
"http.host": monitorHost,
"processors": []map[string]any{
{
"add_host_metadata": map[string]any{
"when.not.contains.tags": "forwarded",
},
},
},
},
}
factory := NewFactory()
t.Run("healthy consumer", func(t *testing.T) {
defer oteltest.VerifyNoLeaks(t)
var consumeLogs oteltest.DummyConsumer
startAndStopReceiver(t, factory, &consumeLogs, &config)
})
t.Run("unhealthy consumer", func(t *testing.T) {
defer oteltest.VerifyNoLeaks(t)
consumeLogs := oteltest.DummyConsumer{ConsumeError: errors.New("cannot publish data")}
startAndStopReceiver(t, factory, &consumeLogs, &config)
})
}
// StartAndStopReceiver creates a receiver using the provided parameters, starts it, verifies that the expected logs
// are output, then shuts it down, and verifies the logs again.
func startAndStopReceiver(t *testing.T, factory receiver.Factory, consumer consumer.Logs, config component.Config) {
t.Helper()
var receiverSettings receiver.Settings
observedCore, observedLogs := observer.New(zapcore.DebugLevel)
receiverSettings.Logger = zap.New(observedCore)
receiverSettings.ID = component.NewIDWithName(factory.Type(), "r1")
rec, err := factory.CreateLogs(t.Context(), receiverSettings, config, consumer)
require.NoError(t, err)
require.NoError(t, rec.Start(t.Context(), componenttest.NewNopHost()))
if !assert.Eventually(t,
func() bool {
return observedLogs.FilterMessageSnippet("system/cpu will start after").Len() >= 1
},
60*time.Second,
1*time.Second,
"receiver not started") {
for _, logLine := range observedLogs.TakeAll() {
t.Log(logLine)
}
t.Fatalf("receiver didn't start, see logs above")
}
require.NoError(t, rec.Shutdown(t.Context()))
if !assert.Eventually(t,
func() bool {
return observedLogs.FilterMessageSnippet("http: Server closed").Len() >= 1
},
60*time.Second,
1*time.Second,
"receiver not stopped") {
for _, logLine := range observedLogs.TakeAll() {
t.Log(logLine)
}
t.Fatalf("receiver didn't stop, see logs above")
}
}