-
Notifications
You must be signed in to change notification settings - Fork 296
/
internal_app_test.go
145 lines (125 loc) · 3.12 KB
/
internal_app_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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package newrelic
import (
"errors"
"fmt"
"testing"
"time"
"github.com/newrelic/go-agent/v3/internal"
)
func TestConnectBackoff(t *testing.T) {
attempts := map[int]int{
0: 15,
2: 30,
5: 300,
6: 300,
100: 300,
-5: 300,
}
for k, v := range attempts {
if b := getConnectBackoffTime(k); b != v {
t.Error(fmt.Sprintf("Invalid connect backoff for attempt #%d:", k), v)
}
}
}
func TestNilApplication(t *testing.T) {
var app *Application
if txn := app.StartTransaction("name"); txn != nil {
t.Error(txn)
}
app.RecordCustomEvent("myEventType", map[string]interface{}{"zip": "zap"})
app.RecordCustomMetric("myMetric", 123.45)
if err := app.WaitForConnection(2 * time.Second); nil != err {
t.Error(err)
}
app.Shutdown(2 * time.Second)
}
func TestEmptyApplication(t *testing.T) {
app := &Application{}
if txn := app.StartTransaction("name"); txn != nil {
t.Error(txn)
}
app.RecordCustomEvent("myEventType", map[string]interface{}{"zip": "zap"})
app.RecordCustomMetric("myMetric", 123.45)
if err := app.WaitForConnection(2 * time.Second); nil != err {
t.Error(err)
}
app.Shutdown(2 * time.Second)
}
func TestConfigOptionError(t *testing.T) {
err := errors.New("myError")
app, got := NewApplication(
nil, // nil config options should be ignored
func(cfg *Config) {
cfg.Error = err
},
func(cfg *Config) {
t.Fatal("this config option should not be run")
},
)
if err != got {
t.Error("config option not returned", err, got)
}
if app != nil {
t.Error("app not nil")
}
}
const (
SampleAppName = "my app"
)
// expectApp combines Application and Expect, for use in validating data in test apps
type expectApplication struct {
internal.Expect
*Application
}
// newTestApp creates an ExpectApp with the given ConnectReply function and Config function
func newTestApp(replyfn func(*internal.ConnectReply), cfgFn ...ConfigOption) expectApplication {
cfgFn = append(cfgFn,
func(cfg *Config) {
// Prevent spawning app goroutines in tests.
if !cfg.ServerlessMode.Enabled {
cfg.Enabled = false
}
},
ConfigAppName(SampleAppName),
ConfigLicense(testLicenseKey),
)
app, err := NewApplication(cfgFn...)
if nil != err {
panic(err)
}
internal.HarvestTesting(app.Private, replyfn)
return expectApplication{
Expect: app.Private.(internal.Expect),
Application: app,
}
}
var sampleEverythingReplyFn = func(reply *internal.ConnectReply) {
reply.SetSampleEverything()
}
var configTestAppLogFn = func(cfg *Config) {
cfg.Enabled = false
cfg.ApplicationLogging.Enabled = true
cfg.ApplicationLogging.Forwarding.Enabled = true
cfg.ApplicationLogging.Metrics.Enabled = true
}
func TestRecordLog(t *testing.T) {
testApp := newTestApp(
sampleEverythingReplyFn,
configTestAppLogFn,
)
time := int64(timeToUnixMilliseconds(time.Now()))
testApp.Application.RecordLog(LogData{
Severity: "Debug",
Message: "Test Message",
Timestamp: time,
})
testApp.ExpectLogEvents(t, []internal.WantLog{
{
Severity: "Debug",
Message: "Test Message",
Timestamp: time,
},
})
}