-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockoutput.go
53 lines (45 loc) · 1.21 KB
/
mockoutput.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
package mockoutput
import (
"github.com/gesemaya/k6/metrics"
)
// New exists so that the usage from tests avoids repetition, i.e. is
// mockoutput.New() instead of &mockoutput.MockOutput{}
func New() *MockOutput {
return &MockOutput{}
}
// MockOutput can be used in tests to mock an actual output.
type MockOutput struct {
SampleContainers []metrics.SampleContainer
Samples []metrics.Sample
DescFn func() string
StartFn func() error
StopFn func() error
}
// AddMetricSamples just saves the results in memory.
func (mo *MockOutput) AddMetricSamples(scs []metrics.SampleContainer) {
mo.SampleContainers = append(mo.SampleContainers, scs...)
for _, sc := range scs {
mo.Samples = append(mo.Samples, sc.GetSamples()...)
}
}
// Description calls the supplied DescFn callback, if available.
func (mo *MockOutput) Description() string {
if mo.DescFn != nil {
return mo.DescFn()
}
return "mock"
}
// Start calls the supplied StartFn callback, if available.
func (mo *MockOutput) Start() error {
if mo.StartFn != nil {
return mo.StartFn()
}
return nil
}
// Stop calls the supplied StopFn callback, if available.
func (mo *MockOutput) Stop() error {
if mo.StopFn != nil {
return mo.StopFn()
}
return nil
}