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

Improved tests for a logging exporter and jaeger exporter #377

Merged
merged 4 commits into from
Oct 7, 2019

Conversation

agneum
Copy link
Contributor

@agneum agneum commented Oct 3, 2019

Some improvements of tests for an issue #266

@tigrannajaryan
Copy link
Member

@agneum thank you for contributing! Please make sure the build passes and the maintainers will review the PR.

@agneum
Copy link
Contributor Author

agneum commented Oct 3, 2019

@tigrannajaryan Thank you, fixed

return
}
assert.NoError(t, err, "nil config")
assert.NotNil(t, got)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use require.NotNil here because got is used below and will crash the test when it is nil (assert does not stop test execution, require does).


// This is expected to fail.
err = got.ConsumeTraceData(context.Background(), consumerdata.TraceData{})
assert.Error(t, err)
})
}
}

func TestNewFails(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking there was no need to split TestNewFails as separate function to be able to use assert, but this looks OK to me and probably is a bit clearer than having positive and negative cases in one function so let's keep it.


// This is expected to fail.
err = got.ConsumeTraceData(context.Background(), consumerdata.TraceData{})
assert.Error(t, err)
})
}
}

func TestNewFails(t *testing.T) {
tests := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is only one test here the value of having an array of test definitions is unclear here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that other cases could be added here in the future. But you're right, for now there is only one. So I fixed this test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the principle of YAGNI here. :-)
We generally aim to have minimum amount of code that serves the purpose.

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.config, tt.args.httpAddress, tt.args.headers, tt.args.timeout)
assert.EqualError(t, err, "nil config")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes all test cases in tests (if we had more than one) will return the same error message. This may not be true. Generally unclear why we have tests array and loop through it if it is one element.

@@ -66,66 +66,81 @@ func TestCreateInstanceViaFactory(t *testing.T) {

func TestFactory_CreateTraceExporter(t *testing.T) {
tests := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, tests with single element.

t.Fatalf("Wanted nil got %v", err)
}
assert.NotNil(t, lte)
assert.NoError(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if err != nil {
t.Fatalf("Wanted nil got %v", err)
}
assert.NotNil(t, lte)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to use require instead of assert otherwise the test will crash if nil is returned. Same in other similar places.

@tigrannajaryan
Copy link
Member

@agneum thank you for working on this. Please make sure assert or require is chosen according to what is needed in the particular piece of code.

@pjanotti
Copy link
Contributor

pjanotti commented Oct 3, 2019

kicking another build to see if we get coverage numbers for this PR

@codecov-io
Copy link

codecov-io commented Oct 3, 2019

Codecov Report

Merging #377 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #377   +/-   ##
=======================================
  Coverage   74.81%   74.81%           
=======================================
  Files         115      115           
  Lines        6885     6885           
=======================================
  Hits         5151     5151           
  Misses       1478     1478           
  Partials      256      256
Impacted Files Coverage Δ
service/builder/receivers_builder.go 82.2% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e505d5...18a976d. Read the comment docs.

@agneum
Copy link
Contributor Author

agneum commented Oct 4, 2019

@tigrannajaryan Thank you for a review. I've fixed all mistakes. Could you please check it one more time?

timeout time.Duration
}

func TestNew(t *testing.T) {
tests := []struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one also is a single test case, let's remove the tests. It can be added later if there are multiple similar tests.

if got == nil {
return
}
assert.NoError(t, err, "nil config")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third parameter of NoError looks incorrect to me. If I remember correctly it is the message to print when assertion fails. This assertion is not about config, why does it print "nil config"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it is a wrong usage. Fixed

Copy link
Member

@tigrannajaryan tigrannajaryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thank you @agneum

@tigrannajaryan tigrannajaryan merged commit 6db8fae into open-telemetry:master Oct 7, 2019
hughesjj pushed a commit to hughesjj/opentelemetry-collector that referenced this pull request Apr 27, 2023
Troels51 pushed a commit to Troels51/opentelemetry-collector that referenced this pull request Jul 5, 2024
swiatekm pushed a commit to swiatekm/opentelemetry-collector that referenced this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants