forked from atlassian/gostatsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures_test.go
67 lines (57 loc) · 1.53 KB
/
fixtures_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
package gostatsd
import (
"context"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// testContext returns a context that will timeout and fail the test if not canceled. Used to
// enforce a timeout on tests.
func testContext(t *testing.T) (context.Context, func()) {
ctxTest, completeTest := context.WithTimeout(context.Background(), 1100*time.Millisecond)
go func() {
after := time.NewTimer(1 * time.Second)
select {
case <-ctxTest.Done():
after.Stop()
case <-after.C:
require.Fail(t, "test timed out")
}
}()
return ctxTest, completeTest
}
type capturingHandler struct {
mu sync.Mutex
m []*Metric
e []*Event
}
func (ch *capturingHandler) EstimatedTags() int {
return 0
}
func (ch *capturingHandler) DispatchMetrics(ctx context.Context, metrics []*Metric) {
ch.mu.Lock()
defer ch.mu.Unlock()
for _, m := range metrics {
m.DoneFunc = nil // Clear DoneFunc because it contains non-predictable variable data which interferes with the tests
ch.m = append(ch.m, m)
}
}
// DispatchMetricMap re-dispatches a metric map through capturingHandler.DispatchMetrics
func (ch *capturingHandler) DispatchMetricMap(ctx context.Context, mm *MetricMap) {
mm.DispatchMetrics(ctx, ch)
}
func (ch *capturingHandler) DispatchEvent(ctx context.Context, e *Event) {
ch.mu.Lock()
defer ch.mu.Unlock()
ch.e = append(ch.e, e)
}
func (ch *capturingHandler) WaitForEvents() {
}
func (ch *capturingHandler) GetMetrics() []*Metric {
ch.mu.Lock()
defer ch.mu.Unlock()
m := make([]*Metric, len(ch.m))
copy(m, ch.m)
return m
}