/
matchers.go
67 lines (62 loc) · 1.87 KB
/
matchers.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
package testhelpers
import (
"code.cloudfoundry.org/go-loggregator/v8/rpc/loggregator_v2"
"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)
type MetricAndValue struct {
Name string
Value int32
}
func MatchV2Metric(target MetricAndValue) types.GomegaMatcher {
return gomega.SatisfyAll(
gomega.WithTransform(func(source *loggregator_v2.Envelope) *loggregator_v2.Gauge {
return source.GetGauge()
}, gomega.Not(gomega.BeNil())),
gomega.WithTransform(func(source *loggregator_v2.Envelope) map[string]*loggregator_v2.GaugeValue {
return source.GetGauge().GetMetrics()
}, gomega.HaveKey(target.Name)),
)
}
func MatchV2MetricAndValue(target MetricAndValue) types.GomegaMatcher {
return gomega.SatisfyAll(
gomega.WithTransform(func(source *loggregator_v2.Envelope) *loggregator_v2.Gauge {
return source.GetGauge()
}, gomega.Not(gomega.BeNil())),
gomega.WithTransform(func(source *loggregator_v2.Envelope) map[string]*loggregator_v2.GaugeValue {
return source.GetGauge().GetMetrics()
}, gomega.HaveKey(target.Name)),
gomega.WithTransform(func(source *loggregator_v2.Envelope) int32 {
return int32(source.GetGauge().GetMetrics()[target.Name].Value)
}, gomega.Equal(target.Value)),
)
}
func TestMetricChan(receiversChan chan loggregator_v2.Ingress_BatchSenderServer) (chan *loggregator_v2.Envelope, chan struct{}) {
signalMetricsChan := make(chan struct{})
testMetricsChan := make(chan *loggregator_v2.Envelope)
go func() {
for {
select {
case receiver := <-receiversChan:
go func() {
for {
batch, err := receiver.Recv()
if err != nil {
return
}
for _, envelope := range batch.Batch {
select {
case testMetricsChan <- envelope:
case <-signalMetricsChan:
return
}
}
}
}()
case <-signalMetricsChan:
return
}
}
}()
return testMetricsChan, signalMetricsChan
}