forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect_reply_test.go
185 lines (167 loc) · 4.71 KB
/
connect_reply_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"encoding/json"
"testing"
"time"
)
func TestCreateFullTxnNameBasic(t *testing.T) {
emptyReply := ConnectReplyDefaults()
tcs := []struct {
input string
background bool
expect string
}{
{"", true, "WebTransaction/Go/"},
{"/", true, "WebTransaction/Go/"},
{"hello", true, "WebTransaction/Go/hello"},
{"/hello", true, "WebTransaction/Go/hello"},
{"", false, "OtherTransaction/Go/"},
{"/", false, "OtherTransaction/Go/"},
{"hello", false, "OtherTransaction/Go/hello"},
{"/hello", false, "OtherTransaction/Go/hello"},
}
for _, tc := range tcs {
if out := CreateFullTxnName(tc.input, emptyReply, tc.background); out != tc.expect {
t.Error(tc.input, tc.background, out, tc.expect)
}
}
}
func TestCreateFullTxnNameURLRulesIgnore(t *testing.T) {
js := `[{
"match_expression":".*zip.*$",
"ignore":true
}]`
reply := ConnectReplyDefaults()
err := json.Unmarshal([]byte(js), &reply.URLRules)
if nil != err {
t.Fatal(err)
}
if out := CreateFullTxnName("/zap/zip/zep", reply, true); out != "" {
t.Error(out)
}
}
func TestCreateFullTxnNameTxnRulesIgnore(t *testing.T) {
js := `[{
"match_expression":"^WebTransaction/Go/zap/zip/zep$",
"ignore":true
}]`
reply := ConnectReplyDefaults()
err := json.Unmarshal([]byte(js), &reply.TxnNameRules)
if nil != err {
t.Fatal(err)
}
if out := CreateFullTxnName("/zap/zip/zep", reply, true); out != "" {
t.Error(out)
}
}
func TestCalculateApdexThreshold(t *testing.T) {
reply := ConnectReplyDefaults()
threshold := CalculateApdexThreshold(reply, "WebTransaction/Go/hello")
if threshold != 500*time.Millisecond {
t.Error("default apdex threshold", threshold)
}
reply = ConnectReplyDefaults()
reply.ApdexThresholdSeconds = 1.3
reply.KeyTxnApdex = map[string]float64{
"WebTransaction/Go/zip": 2.2,
"WebTransaction/Go/zap": 2.3,
}
threshold = CalculateApdexThreshold(reply, "WebTransaction/Go/hello")
if threshold != 1300*time.Millisecond {
t.Error(threshold)
}
threshold = CalculateApdexThreshold(reply, "WebTransaction/Go/zip")
if threshold != 2200*time.Millisecond {
t.Error(threshold)
}
}
func TestIsTrusted(t *testing.T) {
for _, test := range []struct {
id int
trusted string
expected bool
}{
{1, `[]`, false},
{1, `[2, 3]`, false},
{1, `[1]`, true},
{1, `[1, 2, 3]`, true},
} {
trustedAccounts := make(TrustedAccountSet)
if err := json.Unmarshal([]byte(test.trusted), &trustedAccounts); err != nil {
t.Fatal(err)
}
if actual := trustedAccounts.IsTrusted(test.id); test.expected != actual {
t.Errorf("failed asserting whether %d is trusted by %v: expected %v; got %v", test.id, test.trusted, test.expected, actual)
}
}
}
func BenchmarkDefaultRules(b *testing.B) {
js := `{"url_rules":[
{
"match_expression":".*\\.(ace|arj|ini|txt|udl|plist|css|gif|ico|jpe?g|js|png|swf|woff|caf|aiff|m4v|mpe?g|mp3|mp4|mov)$",
"replacement":"/*.\\1",
"ignore":false,
"eval_order":1000,
"terminate_chain":true,
"replace_all":false,
"each_segment":false
},
{
"match_expression":"^[0-9][0-9a-f_,.-]*$",
"replacement":"*",
"ignore":false,
"eval_order":1001,
"terminate_chain":false,
"replace_all":false,
"each_segment":true
},
{
"match_expression":"^(.*)/[0-9][0-9a-f_,-]*\\.([0-9a-z][0-9a-z]*)$",
"replacement":"\\1/.*\\2",
"ignore":false,
"eval_order":1002,
"terminate_chain":false,
"replace_all":false,
"each_segment":false
}
]}`
reply := ConnectReplyDefaults()
err := json.Unmarshal([]byte(js), &reply)
if nil != err {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if out := CreateFullTxnName("/myEndpoint", reply, true); out != "WebTransaction/Go/myEndpoint" {
b.Error(out)
}
}
}
func TestNegativeHarvestLimits(t *testing.T) {
// Test that negative harvest event limits will cause a connect error.
// Harvest event limits are never expected to be negative: This is just
// extra defensiveness.
_, err := UnmarshalConnectReply([]byte(`{"return_value":{
"event_harvest_config": {
"harvest_limits": {
"error_event_data": -1
}
}
}}`), PreconnectReply{})
if err == nil {
t.Fatal("expected error missing")
}
}
func TestDefaultEventHarvestConfigJSON(t *testing.T) {
js, err := json.Marshal(DefaultEventHarvestConfig(MaxTxnEvents))
if err != nil {
t.Error(err)
}
expect := `{"report_period_ms":60000,"harvest_limits":{"analytic_event_data":10000,"custom_event_data":10000,"log_event_data":10000,"error_event_data":100}}`
if string(js) != expect {
t.Errorf("DefaultEventHarvestConfig does not match expected valued:\nExpected:\t%s\nActual:\t\t%s", expect, string(js))
}
}