diff --git a/go.mod b/go.mod index 0ce1589..038f583 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module github.com/newrelic/newrelic-telemetry-sdk-go go 1.13 - -require github.com/stretchr/testify v1.6.1 diff --git a/telemetry/config_test.go b/telemetry/config_test.go index eea7ea5..92db3ac 100644 --- a/telemetry/config_test.go +++ b/telemetry/config_test.go @@ -7,9 +7,6 @@ import ( "bytes" "strings" "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestConfigAPIKey(t *testing.T) { @@ -127,15 +124,21 @@ 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) { @@ -143,15 +146,21 @@ func TestConfigEventsURL(t *testing.T) { // 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) { diff --git a/telemetry/events_batch_test.go b/telemetry/events_batch_test.go index 275bddb..b3e533f 100644 --- a/telemetry/events_batch_test.go +++ b/telemetry/events_batch_test.go @@ -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" ) @@ -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) { @@ -42,17 +53,23 @@ 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}]`) @@ -60,7 +77,9 @@ func TestEventsPayloadSplit(t *testing.T) { // 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}]`) } @@ -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"}, }, }} @@ -84,7 +103,7 @@ func TestEventsJSON(t *testing.T) { }, { "eventType":"testEvent", - "timestamp":`+testTimeString+`, + "timestamp":1417136460000, "zip":"zap" } ]`) diff --git a/telemetry/events_integration_test.go b/telemetry/events_integration_test.go deleted file mode 100644 index 81ca9c0..0000000 --- a/telemetry/events_integration_test.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2019 New Relic Corporation. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 -// +build integration - -package telemetry - -import ( - "context" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestEvent(t *testing.T) { - t.Parallel() - - cfg := NewIntegrationTestConfig(t) - - h, err := NewHarvester(cfg) - assert.NoError(t, err) - assert.NotNil(t, h) - - err = h.RecordEvent(Event{ - EventType: "testEvent", - Attributes: map[string]interface{}{ - "zip": "zap", - }, - }) - assert.NoError(t, err) - - h.HarvestNow(context.Background()) -} - -func TestEventBatch(t *testing.T) { - t.Parallel() - - cfg := NewIntegrationTestConfig(t) - - h, err := NewHarvester(cfg) - assert.NoError(t, err) - assert.NotNil(t, h) - - // Batch up a few events - for x := 0; x < 10; x++ { - err = h.RecordEvent(Event{EventType: "testEvent", Attributes: map[string]interface{}{"zip": "zap", "count": x}}) - assert.NoError(t, err) - } - - h.HarvestNow(context.Background()) -} diff --git a/telemetry/events_test.go b/telemetry/events_test.go index bb99412..c29a73e 100644 --- a/telemetry/events_test.go +++ b/telemetry/events_test.go @@ -1,31 +1,38 @@ // 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) { @@ -33,8 +40,9 @@ func TestEvent(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", @@ -43,7 +51,9 @@ func TestEvent(t *testing.T) { "zip": "zap", }, }) - assert.NoError(t, err) + if err != nil { + t.Fatal(err) + } expect := `[{ "eventType":"testEvent", @@ -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", @@ -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", @@ -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", @@ -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, @@ -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) { @@ -132,5 +150,7 @@ func TestRecordEventNilHarvester(t *testing.T) { }, }) - assert.NoError(t, err) + if err != nil { + t.Fatal(err) + } } diff --git a/telemetry/harvester_test.go b/telemetry/harvester_test.go index 511e84d..efb051d 100644 --- a/telemetry/harvester_test.go +++ b/telemetry/harvester_test.go @@ -18,7 +18,6 @@ import ( "time" "github.com/newrelic/newrelic-telemetry-sdk-go/internal" - "github.com/stretchr/testify/assert" ) // compactJSONString removes the whitespace from a JSON string. This function @@ -65,7 +64,9 @@ func TestHarvesterRecordSpan(t *testing.T) { }, }) - assert.NoError(t, err) + if err != nil { + t.Fatal(err) + } } func TestHarvestErrorLogger(t *testing.T) { diff --git a/telemetry/utilities_test.go b/telemetry/utilities_test.go index 1f9413d..15edaeb 100644 --- a/telemetry/utilities_test.go +++ b/telemetry/utilities_test.go @@ -8,8 +8,6 @@ import ( "fmt" "testing" "time" - - "github.com/stretchr/testify/assert" ) func TestJSONString(t *testing.T) { @@ -57,14 +55,20 @@ func TestJSONOrString(t *testing.T) { } } +func checkMinDuration(t *testing.T, expected time.Duration, actual time.Duration) { + if expected != actual { + t.Errorf("\nexpect=%s\nactual=%s\n", expected, actual) + } +} + func TestMinDuration(t *testing.T) { t.Parallel() t1 := time.Duration(1) t2 := time.Duration(5) - assert.Equal(t, t1, minDuration(t1, t2)) - assert.Equal(t, t1, minDuration(t1, t1)) - assert.Equal(t, t1, minDuration(t2, t1)) - assert.Equal(t, t2, minDuration(t2, t2)) + checkMinDuration(t, t1, minDuration(t1, t2)) + checkMinDuration(t, t1, minDuration(t1, t1)) + checkMinDuration(t, t1, minDuration(t2, t1)) + checkMinDuration(t, t2, minDuration(t2, t2)) }