forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_resolver_test.go
130 lines (113 loc) · 3.71 KB
/
context_resolver_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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
package aggregator
import (
// stdlib
"testing"
// 3p
"github.com/stretchr/testify/assert"
"github.com/DataDog/datadog-agent/pkg/aggregator/ckey"
"github.com/DataDog/datadog-agent/pkg/metrics"
)
func TestGenerateContextKey(t *testing.T) {
mSample := metrics.MetricSample{
Name: "my.metric.name",
Value: 1,
Mtype: metrics.GaugeType,
Tags: []string{"foo", "bar"},
Host: "metric-hostname",
SampleRate: 1,
}
contextKey := generateContextKey(&mSample)
assert.Equal(t, ckey.ContextKey{0xdd892472f57d5cf1, 0x62daaf0ccddbb629}, contextKey)
}
func TestTrackContext(t *testing.T) {
mSample1 := metrics.MetricSample{
Name: "my.metric.name",
Value: 1,
Mtype: metrics.GaugeType,
Tags: []string{"foo", "bar"},
SampleRate: 1,
}
mSample2 := metrics.MetricSample{
Name: "my.metric.name",
Value: 1,
Mtype: metrics.GaugeType,
Tags: []string{"foo", "bar", "baz"},
SampleRate: 1,
}
mSample3 := metrics.MetricSample{ // same as mSample2, with different Host
Name: "my.metric.name",
Value: 1,
Mtype: metrics.GaugeType,
Tags: []string{"foo", "bar", "baz"},
Host: "metric-hostname",
SampleRate: 1,
}
expectedContext1 := Context{
Name: mSample1.Name,
Tags: mSample1.Tags,
}
expectedContext2 := Context{
Name: mSample2.Name,
Tags: mSample2.Tags,
}
expectedContext3 := Context{
Name: mSample3.Name,
Tags: mSample3.Tags,
Host: mSample3.Host,
}
contextResolver := newContextResolver()
// Track the 2 contexts
contextKey1 := contextResolver.trackContext(&mSample1, 1)
contextKey2 := contextResolver.trackContext(&mSample2, 1)
contextKey3 := contextResolver.trackContext(&mSample3, 1)
// When we look up the 2 keys, they return the correct contexts
context1 := contextResolver.contextsByKey[contextKey1]
assert.Equal(t, expectedContext1, *context1)
context2 := contextResolver.contextsByKey[contextKey2]
assert.Equal(t, expectedContext2, *context2)
context3 := contextResolver.contextsByKey[contextKey3]
assert.Equal(t, expectedContext3, *context3)
unknownContextKey := ckey.ContextKey{0xffffffffffffffff, 0xffffffffffffffff}
_, ok := contextResolver.contextsByKey[unknownContextKey]
assert.False(t, ok)
}
func TestExpireContexts(t *testing.T) {
mSample1 := metrics.MetricSample{
Name: "my.metric.name",
Value: 1,
Mtype: metrics.GaugeType,
Tags: []string{"foo", "bar"},
SampleRate: 1,
}
mSample2 := metrics.MetricSample{
Name: "my.metric.name",
Value: 1,
Mtype: metrics.GaugeType,
Tags: []string{"foo", "bar", "baz"},
SampleRate: 1,
}
contextResolver := newContextResolver()
// Track the 2 contexts
contextKey1 := contextResolver.trackContext(&mSample1, 4)
contextKey2 := contextResolver.trackContext(&mSample2, 6)
// With an expireTimestap of 3, both contexts are still valid
assert.Len(t, contextResolver.expireContexts(3), 0)
_, ok1 := contextResolver.contextsByKey[contextKey1]
_, ok2 := contextResolver.contextsByKey[contextKey2]
assert.True(t, ok1)
assert.True(t, ok2)
// With an expireTimestap of 5, context 1 is expired
expiredContextKeys := contextResolver.expireContexts(5)
if assert.Len(t, expiredContextKeys, 1) {
assert.Equal(t, contextKey1, expiredContextKeys[0])
}
// context 1 is not tracked anymore, but context 2 still is
_, ok := contextResolver.contextsByKey[contextKey1]
assert.False(t, ok)
_, ok = contextResolver.contextsByKey[contextKey2]
assert.True(t, ok)
}