-
Notifications
You must be signed in to change notification settings - Fork 1k
testing: de-globalize discard loggers #1165
Conversation
sdboyer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's have a helper func to do it, just so that the intent and pattern is entirely clear. makes it easy, too - just turn var discardLogger into func discardLogger() *log.Logger
Done |
| var ( | ||
| discardLogger = log.New(ioutil.Discard, "", 0) | ||
| ) | ||
| func discardLogger() *log.Logger { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about moving this to internal/test and exporting? That way it doesn't need to be defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kinda meh about that - tbh I was kinda meh about my original suggestion, but the idea of exporting it just makes it seem definitively unwise 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't care much about if we do it or not but out of curiosity what's wrong/unwise with exporting test.DiscardLogger() in our internal test package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a really subjective thing, which is why i sorta hesitate about it in the first place, but to my mind, exporting jumps the line between an innocuous helper and a recommended pattern.
ibrasho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What does this do / why do we need it?
This change replaces global discard
Loggervariables with per-test instances.We've developed a habit of using consolidated global vars to share
discardLoggers between tests (log.New(ioutil.Discard, "", 0)). This seems logical since calls are effectively no-ops, but the internals of theLoggerare still locking to serialize writes toioutil.Discard, which creates unnecessary coupling and synchronization between tests that could otherwise be completely independent and/or parallel.