Skip to content

Commit

Permalink
Additional Testing covering Application Function Calls
Browse files Browse the repository at this point in the history
  • Loading branch information
iamemilio committed Jun 28, 2022
1 parent d916ec5 commit 92a4a37
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
7 changes: 7 additions & 0 deletions v3/internal/integrationsupport/integrationsupport.go
Expand Up @@ -93,6 +93,13 @@ var DTEnabledCfgFn = func(cfg *newrelic.Config) {
cfg.DistributedTracer.Enabled = true
}

var AppLogEnabledCfgFn = func(cfg *newrelic.Config) {
cfg.Enabled = false
cfg.ApplicationLogging.Enabled = true
cfg.ApplicationLogging.Forwarding.Enabled = true
cfg.ApplicationLogging.Metrics.Enabled = true
}

// SampleEverythingReplyFn is a reusable ConnectReply function that samples everything
var SampleEverythingReplyFn = func(reply *internal.ConnectReply) {
reply.SetSampleEverything()
Expand Down
2 changes: 1 addition & 1 deletion v3/newrelic/expect_implementation.go
Expand Up @@ -203,7 +203,7 @@ func expectCustomEvents(v internal.Validator, cs *customEvents, expect []interna

func expectLogEvents(v internal.Validator, events *logEvents, expect []internal.WantLog) {
if len(events.logs) != len(expect) {
v.Error("number of events does not match", len(events.logs), len(expect))
v.Error("actual number of events does not match what is expected", len(events.logs), len(expect))
return
}

Expand Down
97 changes: 97 additions & 0 deletions v3/newrelic/internal_app_test.go
Expand Up @@ -4,10 +4,13 @@
package newrelic

import (
"context"
"errors"
"fmt"
"testing"
"time"

"github.com/newrelic/go-agent/v3/internal"
)

func TestConnectBackoff(t *testing.T) {
Expand Down Expand Up @@ -71,3 +74,97 @@ func TestConfigOptionError(t *testing.T) {
t.Error("app not nil")
}
}

const (
SampleAppName = "my app"
)

// ExpectApp combines Application and Expect, for use in validating data in test apps
type ExpectApp struct {
internal.Expect
*Application
}

// NewTestApp creates an ExpectApp with the given ConnectReply function and Config function
func NewTestApp(replyfn func(*internal.ConnectReply), cfgFn ...ConfigOption) ExpectApp {
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 ExpectApp{
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,
})

txn := testApp.StartTransaction("test transaction")
ctx := NewContext(context.Background(), txn)

// gather linking metadata values for test verification
metadata := txn.GetTraceMetadata()
spanID := metadata.SpanID
traceID := metadata.TraceID

testApp.Application.RecordLog(LogData{
Severity: "Warn",
Message: "Test Message With Transaction",
Timestamp: time,
Context: ctx,
})

txn.End()

testApp.ExpectLogEvents(t, []internal.WantLog{
{
Severity: "Debug",
Message: "Test Message",
Timestamp: time,
},
{
Severity: "Warn",
Message: "Test Message With Transaction",
Timestamp: time,
SpanID: spanID,
TraceID: traceID,
},
})
}

0 comments on commit 92a4a37

Please sign in to comment.