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

zaptest/NewLogger: fail test on internal errors #566

Merged
merged 2 commits into from
Apr 9, 2018

Conversation

abhinav
Copy link
Collaborator

@abhinav abhinav commented Mar 26, 2018

Zap's errorOutput is used exclusively to log internal errors. If this
ever gets used in a test, something catastrophic happened and we should
fail the test.

This changes the logger returned by zaptest.NewLogger to implement this
behavior by passing a WriteSyncer to zap.ErrorOutput that marks the
test as failed upon being used.

To test this, we set up a test logger and replace its core with one that
will always fail to write.

@codecov
Copy link

codecov bot commented Mar 26, 2018

Codecov Report

Merging #566 into abg/expand-testingt will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@                   Coverage Diff                   @@
##           abg/expand-testingt     #566      +/-   ##
=======================================================
+ Coverage                97.48%   97.49%   +0.01%     
=======================================================
  Files                       40       40              
  Lines                     2026     2035       +9     
=======================================================
+ Hits                      1975     1984       +9     
  Misses                      43       43              
  Partials                     8        8
Impacted Files Coverage Δ
zaptest/logger.go 100% <100%> (ø) ⬆️

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 cdd3f0b...2b3338c. Read the comment docs.

assert.Equal(t.TB, msgs, t.Messages, "logged messages did not match")
}

func (t *testLogSpy) AssertFailed(v bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make this an unexported method and have methods AssertFailed() and AssertPassed() rather than a boolean argument?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense

// testLogSpy is a testing.TB that captures logged messages.
type testLogSpy struct {
testing.TB

mu sync.Mutex
mu sync.RWMutex
Copy link
Collaborator

Choose a reason for hiding this comment

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

optional/can follow up:
this spy is only used internally, and I don't think any of the tests are concurrent, so we might be able to just remove the mutex.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The spy is thread safe only because testing.TB is supposed to be thread-
safe, but yeah, I'll drop it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This expands the TestingT interface to a much larger subset of
`testing.TB`.

The following were left out:

- `Error` and `Log` were left out in favor of `Errorf` and `Logf`
- `Fatal*` methods were left out in favor of `Errorf` followed by
  `FailNow`
- `Skip*` methods were left out because our test logger shouldn't be
  skipping tests
- `Helper` was left out because not all supported verisons of Go have
  that
Zap's errorOutput is used exclusively to log internal errors. If this
ever gets used in a test, something catastrophic happened and we should
fail the test.

This changes the logger returned by zaptest.NewLogger to implement this
behavior by passing a `WriteSyncer` to `zap.ErrorOutput` that marks the
test as failed upon being used.

To test this, we set up a test logger and replace its core with one that
will always fail to write.
@abhinav abhinav changed the base branch from abg/expand-testingt to abg/test-logger April 9, 2018 18:15
@abhinav abhinav merged commit 2b3338c into abg/test-logger Apr 9, 2018
@abhinav abhinav deleted the abg/test-error-output branch April 9, 2018 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants