Skip to content

Commit

Permalink
Use assert/require in test code of exporter (#286)
Browse files Browse the repository at this point in the history
Signed-off-by: Hui Kang <[email protected]>
  • Loading branch information
huikang authored and Paulo Janotti committed Aug 23, 2019
1 parent 99ab342 commit d6094a8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
15 changes: 6 additions & 9 deletions consumer/consumererror/consumererror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,19 @@ package consumererror
import (
"errors"
"testing"

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

func TestPermanent(t *testing.T) {
err := errors.New("testError")
if IsPermanent(err) {
t.Fatalf("IsPermanent() = true, want false")
}
require.False(t, IsPermanent(err))

err = Permanent(err)
if !IsPermanent(err) {
t.Fatalf("IsPermanent() = false, want true")
}
require.True(t, IsPermanent(err))
}

func TestIsPermanent_NilError(t *testing.T) {
var err error
if IsPermanent(err) {
t.Fatalf("IsPermanent() = true, want false")
}
require.False(t, IsPermanent(err))
}
44 changes: 20 additions & 24 deletions exporter/exportertest/nop_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/require"

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/open-telemetry/opentelemetry-service/consumer/consumerdata"
Expand All @@ -28,12 +30,11 @@ func TestNopTraceExporter_NoErrors(t *testing.T) {
td := consumerdata.TraceData{
Spans: make([]*tracepb.Span, 7),
}
if err := nte.ConsumeTraceData(context.Background(), td); err != nil {
t.Fatalf("Wanted nil got error")
}
if nte.Name() != "nop_trace" {
t.Fatalf("Wanted nop_trace got %s", nte.Name())
}

err := nte.ConsumeTraceData(context.Background(), td)
require.Nil(t, err)

require.Equal(t, "nop_trace", nte.Name())
}

func TestNopTraceExporter_WithErrors(t *testing.T) {
Expand All @@ -42,25 +43,22 @@ func TestNopTraceExporter_WithErrors(t *testing.T) {
td := consumerdata.TraceData{
Spans: make([]*tracepb.Span, 7),
}
if got := nte.ConsumeTraceData(context.Background(), td); got != want {
t.Fatalf("Want %v Got %v", want, got)
}
if nte.Name() != "nop_trace" {
t.Fatalf("Wanted nop_trace got %s", nte.Name())
}

require.Equal(t, want, nte.ConsumeTraceData(context.Background(), td))

require.Equal(t, "nop_trace", nte.Name())
}

func TestNopMetricsExporter_NoErrors(t *testing.T) {
nme := NewNopMetricsExporter()
md := consumerdata.MetricsData{
Metrics: make([]*metricspb.Metric, 7),
}
if err := nme.ConsumeMetricsData(context.Background(), md); err != nil {
t.Fatalf("Wanted nil got error")
}
if nme.Name() != "nop_metrics" {
t.Fatalf("Wanted nop_metrics got %s", nme.Name())
}
err := nme.ConsumeMetricsData(context.Background(), md)

require.Nil(t, err)

require.Equal(t, "nop_metrics", nme.Name())
}

func TestNopMetricsExporter_WithErrors(t *testing.T) {
Expand All @@ -69,10 +67,8 @@ func TestNopMetricsExporter_WithErrors(t *testing.T) {
md := consumerdata.MetricsData{
Metrics: make([]*metricspb.Metric, 7),
}
if got := nme.ConsumeMetricsData(context.Background(), md); got != want {
t.Fatalf("Want %v Got %v", want, got)
}
if nme.Name() != "nop_metrics" {
t.Fatalf("Wanted nop_metrics got %s", nme.Name())
}

require.Equal(t, want, nme.ConsumeMetricsData(context.Background(), md))

require.Equal(t, "nop_metrics", nme.Name())
}
32 changes: 13 additions & 19 deletions exporter/exportertest/sink_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ package exportertest

import (
"context"
"reflect"
"testing"

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

metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
"github.com/open-telemetry/opentelemetry-service/consumer/consumerdata"
Expand All @@ -30,18 +32,14 @@ func TestSinkTraceExporter(t *testing.T) {
}
want := make([]consumerdata.TraceData, 0, 7)
for i := 0; i < 7; i++ {
if err := sink.ConsumeTraceData(context.Background(), td); err != nil {
t.Fatalf("Wanted nil got error")
}
err := sink.ConsumeTraceData(context.Background(), td)
require.Nil(t, err)
want = append(want, td)
}
got := sink.AllTraces()
if !reflect.DeepEqual(got, want) {
t.Errorf("Mismatches responses\nGot:\n\t%v\nWant:\n\t%v\n", got, want)
}
if sink.Name() != "sink_trace" {
t.Errorf("Wanted sink_trace got %s", sink.Name())
}
assert.Equal(t, want, got)

assert.Equal(t, "sink_trace", sink.Name())
}

func TestSinkMetricsExporter(t *testing.T) {
Expand All @@ -51,16 +49,12 @@ func TestSinkMetricsExporter(t *testing.T) {
}
want := make([]consumerdata.MetricsData, 0, 7)
for i := 0; i < 7; i++ {
if err := sink.ConsumeMetricsData(context.Background(), md); err != nil {
t.Fatalf("Wanted nil got error")
}
err := sink.ConsumeMetricsData(context.Background(), md)
require.Nil(t, err)
want = append(want, md)
}
got := sink.AllMetrics()
if !reflect.DeepEqual(got, want) {
t.Errorf("Mismatches responses\nGot:\n\t%v\nWant:\n\t%v\n", got, want)
}
if sink.Name() != "sink_metrics" {
t.Errorf("Wanted sink_metrics got %s", sink.Name())
}
assert.Equal(t, want, got)

assert.Equal(t, "sink_metrics", sink.Name())
}

0 comments on commit d6094a8

Please sign in to comment.