-
Notifications
You must be signed in to change notification settings - Fork 183
/
output.go
135 lines (116 loc) · 3.47 KB
/
output.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package neotest
import (
"sync"
"time"
"github.com/signalfx/golib/datapoint"
"github.com/signalfx/golib/event"
"github.com/signalfx/golib/trace"
"github.com/signalfx/signalfx-agent/internal/monitors/types"
)
// TestOutput can be used in place of the normal monitor outut to provide a
// simpler way of testing monitor output.
type TestOutput struct {
dpChan chan *datapoint.Datapoint
eventChan chan *event.Event
spanChan chan *trace.Span
dimPropChan chan *types.DimProperties
// Use a lock since monitors are allowed to use output from multiple
// threads.
lock sync.Mutex
}
// NewTestOutput creates a new initialized TestOutput instance
func NewTestOutput() *TestOutput {
return &TestOutput{
dpChan: make(chan *datapoint.Datapoint, 1000),
eventChan: make(chan *event.Event, 1000),
spanChan: make(chan *trace.Span, 1000),
dimPropChan: make(chan *types.DimProperties, 1000),
}
}
// SendDatapoint accepts a datapoint and sticks it in a buffered queue
func (to *TestOutput) SendDatapoint(dp *datapoint.Datapoint) {
to.dpChan <- dp
}
// SendEvent accepts an event and sticks it in a buffered queue
func (to *TestOutput) SendEvent(event *event.Event) {
to.eventChan <- event
}
// SendSpan accepts a trace span and sticks it in a buffered queue
func (to *TestOutput) SendSpan(span *trace.Span) {
to.spanChan <- span
}
// SendDimensionProps accepts a dim prop update and sticks it in a buffered queue
func (to *TestOutput) SendDimensionProps(dimProps *types.DimProperties) {
to.dimPropChan <- dimProps
}
// AddExtraDimension is a noop here
func (to *TestOutput) AddExtraDimension(key, value string) {}
// RemoveExtraDimension is a noop here
func (to *TestOutput) RemoveExtraDimension(key string) {}
// FlushDatapoints returns all of the datapoints injected into the channel so
// far.
func (to *TestOutput) FlushDatapoints() []*datapoint.Datapoint {
var out []*datapoint.Datapoint
for {
select {
case dp := <-to.dpChan:
out = append(out, dp)
default:
return out
}
}
}
// FlushEvents returns all of the datapoints injected into the channel so
// far.
func (to *TestOutput) FlushEvents() []*event.Event {
var out []*event.Event
for {
select {
case event := <-to.eventChan:
out = append(out, event)
default:
return out
}
}
}
// WaitForDPs will keep pulling datapoints off of the internal queue until it
// either gets the expected count or waitSeconds seconds have elapsed. It then
// returns those datapoints. It will never return more than 'count' datapoints.
func (to *TestOutput) WaitForDPs(count, waitSeconds int) []*datapoint.Datapoint {
var dps []*datapoint.Datapoint
timeout := time.After(time.Duration(waitSeconds) * time.Second)
loop:
for {
select {
case dp := <-to.dpChan:
dps = append(dps, dp)
if len(dps) >= count {
break loop
}
case <-timeout:
break loop
}
}
return dps
}
// WaitForDimensionProps will keep pulling dimension property updates off of
// the internal queue until it either gets the expected count or waitSeconds
// seconds have elapsed. It then returns those dimension property updates. It
// will never return more than 'count' objects.
func (to *TestOutput) WaitForDimensionProps(count, waitSeconds int) []*types.DimProperties {
var dps []*types.DimProperties
timeout := time.After(time.Duration(waitSeconds) * time.Second)
loop:
for {
select {
case dp := <-to.dimPropChan:
dps = append(dps, dp)
if len(dps) >= count {
break loop
}
case <-timeout:
break loop
}
}
return dps
}