Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace testReaderHarness with testify suite #2915

Merged
merged 2 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sdk/metric/manual_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric/reader"

import (
"testing"

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

func TestManualReader(t *testing.T) {
testReaderHarness(t, func() Reader { return NewManualReader() })
suite.Run(t, &readerTestSuite{Factory: NewManualReader})
}
109 changes: 51 additions & 58 deletions sdk/metric/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,62 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric/reader"

import (
"context"
"testing"

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

"go.opentelemetry.io/otel/sdk/metric/export"
)

type readerFactory func() Reader

func testReaderHarness(t *testing.T, f readerFactory) {
t.Run("ErrorForNotRegistered", func(t *testing.T) {
r := f()
ctx := context.Background()

_, err := r.Collect(ctx)
require.ErrorIs(t, err, ErrReaderNotRegistered)

// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(ctx)
})

t.Run("Producer", func(t *testing.T) {
r := f()
r.register(testProducer{})
ctx := context.Background()

m, err := r.Collect(ctx)
assert.NoError(t, err)
assert.Equal(t, testMetrics, m)

// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(ctx)
})

t.Run("CollectAfterShutdown", func(t *testing.T) {
r := f()
r.register(testProducer{})
require.NoError(t, r.Shutdown(context.Background()))

m, err := r.Collect(context.Background())
assert.ErrorIs(t, err, ErrReaderShutdown)
assert.Equal(t, export.Metrics{}, m)
})

t.Run("ShutdownTwice", func(t *testing.T) {
r := f()
r.register(testProducer{})
require.NoError(t, r.Shutdown(context.Background()))

assert.ErrorIs(t, r.Shutdown(context.Background()), ErrReaderShutdown)
})

t.Run("MultipleForceFlush", func(t *testing.T) {
r := f()
r.register(testProducer{})
ctx := context.Background()
require.NoError(t, r.ForceFlush(ctx))
assert.NoError(t, r.ForceFlush(ctx))

// Ensure Reader is allowed clean up attempt.
_ = r.Shutdown(ctx)
})
type readerTestSuite struct {
suite.Suite

Factory func() Reader
Reader Reader
}

func (ts *readerTestSuite) SetupTest() {
ts.Reader = ts.Factory()
}

func (ts *readerTestSuite) TearDownTest() {
// Ensure Reader is allowed attempt to clean up.
_ = ts.Reader.Shutdown(context.Background())
}

func (ts *readerTestSuite) TestErrorForNotRegistered() {
_, err := ts.Reader.Collect(context.Background())
ts.ErrorIs(err, ErrReaderNotRegistered)
}

func (ts *readerTestSuite) TestProducer() {
ts.Reader.register(testProducer{})
m, err := ts.Reader.Collect(context.Background())
ts.NoError(err)
ts.Equal(testMetrics, m)
}

func (ts *readerTestSuite) TestCollectAfterShutdown() {
ctx := context.Background()
ts.Reader.register(testProducer{})
ts.Require().NoError(ts.Reader.Shutdown(ctx))

m, err := ts.Reader.Collect(ctx)
ts.ErrorIs(err, ErrReaderShutdown)
ts.Equal(export.Metrics{}, m)
}

func (ts *readerTestSuite) TestShutdownTwice() {
ctx := context.Background()
ts.Reader.register(testProducer{})
ts.Require().NoError(ts.Reader.Shutdown(ctx))
ts.ErrorIs(ts.Reader.Shutdown(ctx), ErrReaderShutdown)
}

func (ts *readerTestSuite) TestMultipleForceFlush() {
ctx := context.Background()
ts.Reader.register(testProducer{})
ts.Require().NoError(ts.Reader.ForceFlush(ctx))
ts.NoError(ts.Reader.ForceFlush(ctx))
}

var testMetrics = export.Metrics{
Expand Down