Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Remove testify dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfoote committed Nov 19, 2020
1 parent 16d0ef2 commit c2401ad
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 119 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module github.com/newrelic/newrelic-telemetry-sdk-go

go 1.13

require github.com/stretchr/testify v1.6.1
39 changes: 24 additions & 15 deletions telemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"bytes"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigAPIKey(t *testing.T) {
Expand Down Expand Up @@ -127,31 +124,43 @@ func TestConfigSpansURL(t *testing.T) {

// We get the default
h, err := NewHarvester(configTesting)
require.NoError(t, err)
require.NotNil(t, h)
assert.Equal(t, defaultSpanURL, h.config.spanURL())
if nil == h || err != nil {
t.Fatal(h, err)
}
if u := h.config.spanURL(); u != defaultSpanURL {
t.Fatal(u)
}

// The override config option works
h, err = NewHarvester(configTesting, ConfigSpansURLOverride("span-url-override"))
require.NoError(t, err)
require.NotNil(t, h)
assert.Equal(t, "span-url-override", h.config.spanURL())
if nil == h || err != nil {
t.Fatal(h, err)
}
if u := h.config.spanURL(); u != "span-url-override" {
t.Fatal(u)
}
}

func TestConfigEventsURL(t *testing.T) {
t.Parallel()

// We get the default
h, err := NewHarvester(configTesting)
require.NoError(t, err)
require.NotNil(t, h)
assert.Equal(t, defaultEventURL, h.config.eventURL())
if nil == h || err != nil {
t.Fatal(h, err)
}
if u := h.config.eventURL(); u != defaultEventURL {
t.Fatal(u)
}

// The override config option works
h, err = NewHarvester(configTesting, ConfigEventsURLOverride("event-url-override"))
require.NoError(t, err)
require.NotNil(t, h)
assert.Equal(t, "event-url-override", h.config.eventURL())
if nil == h || err != nil {
t.Fatal(h, err)
}
if u := h.config.eventURL(); u != "event-url-override" {
t.Fatal(u)
}
}

func TestConfigUserAgent(t *testing.T) {
Expand Down
61 changes: 40 additions & 21 deletions telemetry/events_batch_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Copyright 2019 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build unit

package telemetry

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"time"

"github.com/newrelic/newrelic-telemetry-sdk-go/internal"
)
Expand All @@ -18,22 +15,36 @@ func testEventBatchJSON(t testing.TB, batch *eventBatch, expect string) {
if th, ok := t.(interface{ Helper() }); ok {
th.Helper()
}
reqs, err := newRequests(batch, "apiKey", defaultEventURL, "userAgent")
require.NoError(t, err)
require.Equal(t, 1, len(reqs))

reqs, err := newRequests(batch, "apiKey", defaultSpanURL, "userAgent")
if nil != err {
t.Fatal(err)
}
if len(reqs) != 1 {
t.Fatal(reqs)
}
req := reqs[0]
assert.Equal(t, compactJSONString(expect), string(req.UncompressedBody))
actual := string(req.UncompressedBody)
compact := compactJSONString(expect)
if actual != compact {
t.Errorf("\nexpect=%s\nactual=%s\n", compact, actual)
}

body, err := ioutil.ReadAll(req.Request.Body)
req.Request.Body.Close()
require.NoError(t, err)

assert.Equal(t, req.compressedBodyLength, len(body))

if err != nil {
t.Fatal("unable to read body", err)
}
if len(body) != req.compressedBodyLength {
t.Error("compressed body length mismatch",
len(body), req.compressedBodyLength)
}
uncompressed, err := internal.Uncompress(body)
require.NoError(t, err)
assert.Equal(t, string(req.UncompressedBody), string(uncompressed))
if err != nil {
t.Fatal("unable to uncompress body", err)
}
if string(uncompressed) != string(req.UncompressedBody) {
t.Error("request JSON mismatch", string(uncompressed), string(req.UncompressedBody))
}
}

func TestEventsPayloadSplit(t *testing.T) {
Expand All @@ -42,25 +53,33 @@ func TestEventsPayloadSplit(t *testing.T) {
// test len 0
ev := &eventBatch{}
split := ev.split()
assert.Nil(t, split)
if split != nil {
t.Error(split)
}

// test len 1
ev = &eventBatch{Events: []Event{{EventType: "a"}}}
split = ev.split()
assert.Nil(t, split)
if split != nil {
t.Error(split)
}

// test len 2
ev = &eventBatch{Events: []Event{{EventType: "a"}, {EventType: "b"}}}
split = ev.split()
assert.Equal(t, 2, len(split))
if len(split) != 2 {
t.Error("split into incorrect number of slices", len(split))
}

testEventBatchJSON(t, split[0].(*eventBatch), `[{"eventType":"a","timestamp":-6795364578871}]`)
testEventBatchJSON(t, split[1].(*eventBatch), `[{"eventType":"b","timestamp":-6795364578871}]`)

// test len 3
ev = &eventBatch{Events: []Event{{EventType: "a"}, {EventType: "b"}, {EventType: "c"}}}
split = ev.split()
assert.Equal(t, 2, len(split))
if len(split) != 2 {
t.Error("split into incorrect number of slices", len(split))
}
testEventBatchJSON(t, split[0].(*eventBatch), `[{"eventType":"a","timestamp":-6795364578871}]`)
testEventBatchJSON(t, split[1].(*eventBatch), `[{"eventType":"b","timestamp":-6795364578871},{"eventType":"c","timestamp":-6795364578871}]`)
}
Expand All @@ -72,7 +91,7 @@ func TestEventsJSON(t *testing.T) {
{}, // Empty
{ // with everything
EventType: "testEvent",
Timestamp: testTimestamp,
Timestamp: time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC),
Attributes: map[string]interface{}{"zip": "zap"},
},
}}
Expand All @@ -84,7 +103,7 @@ func TestEventsJSON(t *testing.T) {
},
{
"eventType":"testEvent",
"timestamp":`+testTimeString+`,
"timestamp":1417136460000,
"zip":"zap"
}
]`)
Expand Down
50 changes: 0 additions & 50 deletions telemetry/events_integration_test.go

This file was deleted.

66 changes: 43 additions & 23 deletions telemetry/events_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
// Copyright 2019 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build unit

package telemetry

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func testHarvesterEvents(t testing.TB, h *Harvester, expect string) {
reqs := h.swapOutEvents()
if expect != "null" {
require.NotNil(t, reqs)
if nil == reqs {
if expect != "null" {
t.Error("nil spans", expect)
}
return
}

if len(reqs) != 1 {
t.Fatal(reqs)
}
if u := reqs[0].Request.URL.String(); u != defaultEventURL {
t.Fatal(u)
}
require.Equal(t, 1, len(reqs))
require.Equal(t, defaultEventURL, reqs[0].Request.URL.String())

js := reqs[0].UncompressedBody
actual := string(js)
if th, ok := t.(interface{ Helper() }); ok {
th.Helper()
}
assert.Equal(t, compactJSONString(expect), actual)
compactExpect := compactJSONString(expect)
if compactExpect != actual {
t.Errorf("\nexpect=%s\nactual=%s\n", compactExpect, actual)
}
}

func TestEvent(t *testing.T) {
t.Parallel()

tm := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
h, err := NewHarvester(configTesting)
assert.NoError(t, err)
assert.NotNil(t, h)
if nil == h || err != nil {
t.Fatal(h, err)
}

err = h.RecordEvent(Event{
EventType: "testEvent",
Expand All @@ -43,7 +51,9 @@ func TestEvent(t *testing.T) {
"zip": "zap",
},
})
assert.NoError(t, err)
if err != nil {
t.Fatal(err)
}

expect := `[{
"eventType":"testEvent",
Expand All @@ -59,8 +69,9 @@ func TestEventInvalidAttribute(t *testing.T) {

tm := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
h, err := NewHarvester(configTesting)
assert.NoError(t, err)
assert.NotNil(t, h)
if nil == h || err != nil {
t.Fatal(h, err)
}

err = h.RecordEvent(Event{
EventType: "testEvent",
Expand All @@ -70,7 +81,9 @@ func TestEventInvalidAttribute(t *testing.T) {
"nil-gets-removed": nil,
},
})
assert.NoError(t, err)
if err != nil {
t.Fatal(err)
}

expect := `[{
"eventType":"testEvent",
Expand All @@ -85,8 +98,9 @@ func TestRecordEventZeroTime(t *testing.T) {
t.Parallel()

h, err := NewHarvester(configTesting)
assert.NoError(t, err)
assert.NotNil(t, h)
if nil == h || err != nil {
t.Fatal(h, err)
}

err = h.RecordEvent(Event{
EventType: "testEvent",
Expand All @@ -96,15 +110,18 @@ func TestRecordEventZeroTime(t *testing.T) {
},
})

assert.NoError(t, err)
if err != nil {
t.Fatal(err)
}
}
func TestRecordEventEmptyType(t *testing.T) {
t.Parallel()

tm := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
h, err := NewHarvester(configTesting)
assert.NoError(t, err)
assert.NotNil(t, h)
if nil == h || err != nil {
t.Fatal(h, err)
}

err = h.RecordEvent(Event{
Timestamp: tm,
Expand All @@ -114,8 +131,9 @@ func TestRecordEventEmptyType(t *testing.T) {
},
})

assert.Error(t, err)
assert.Equal(t, errEventTypeUnset, err)
if err != errEventTypeUnset {
t.Fatal(h, err)
}
}

func TestRecordEventNilHarvester(t *testing.T) {
Expand All @@ -132,5 +150,7 @@ func TestRecordEventNilHarvester(t *testing.T) {
},
})

assert.NoError(t, err)
if err != nil {
t.Fatal(err)
}
}
Loading

0 comments on commit c2401ad

Please sign in to comment.