-
Notifications
You must be signed in to change notification settings - Fork 579
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
minsev: Refactor unit tests #5869
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,81 +56,68 @@ func (p *processor) ForceFlush(ctx context.Context) error { | |
return p.ReturnErr | ||
} | ||
|
||
func (p *processor) Reset() { | ||
p.OnEmitCalls = p.OnEmitCalls[:0] | ||
p.EnabledCalls = p.EnabledCalls[:0] | ||
p.ShutdownCalls = p.ShutdownCalls[:0] | ||
p.ForceFlushCalls = p.ForceFlushCalls[:0] | ||
} | ||
|
||
func TestLogProcessorOnEmit(t *testing.T) { | ||
t.Run("Passthrough", func(t *testing.T) { | ||
wrapped := &processor{ReturnErr: assert.AnError} | ||
|
||
p := NewLogProcessor(wrapped, api.SeverityTrace1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
for _, sev := range severities { | ||
wrapped := &processor{ReturnErr: assert.AnError} | ||
p := NewLogProcessor(wrapped, api.SeverityTrace1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
|
||
r.SetSeverity(sev) | ||
assert.ErrorIs(t, p.OnEmit(ctx, *r), assert.AnError, sev.String()) | ||
|
||
if assert.Lenf(t, wrapped.OnEmitCalls, 1, "Record with severity %s not passed-through", sev) { | ||
assert.Equal(t, ctx, wrapped.OnEmitCalls[0].Ctx, sev.String()) | ||
assert.Equal(t, *r, wrapped.OnEmitCalls[0].Record, sev.String()) | ||
} | ||
wrapped.Reset() | ||
} | ||
}) | ||
|
||
t.Run("Dropped", func(t *testing.T) { | ||
wrapped := &processor{ReturnErr: assert.AnError} | ||
|
||
p := NewLogProcessor(wrapped, api.SeverityFatal4+1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
for _, sev := range severities { | ||
wrapped := &processor{ReturnErr: assert.AnError} | ||
p := NewLogProcessor(wrapped, api.SeverityFatal4+1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
|
||
r.SetSeverity(sev) | ||
assert.NoError(t, p.OnEmit(ctx, *r), assert.AnError, sev.String()) | ||
|
||
if !assert.Lenf(t, wrapped.OnEmitCalls, 0, "Record with severity %s passed-through", sev) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This if increases the https://en.m.wikipedia.org/wiki/Cyclomatic_complexity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I gotcha. It seems a bit like the cyclomatic complexity is being over applied here, but if that is truly the desire, there is no need for the assert.Lenf(t, wrapped.OnEmitCalls, 0, "Record with severity %s passed-through", sev)
wrapped.Reset() That would avoid I'm still not sold that this metric is really serving to better the code with type of change though. |
||
wrapped.Reset() | ||
} | ||
assert.Lenf(t, wrapped.OnEmitCalls, 0, "Record with severity %s passed-through", sev) | ||
} | ||
}) | ||
} | ||
|
||
func TestLogProcessorEnabled(t *testing.T) { | ||
t.Run("Passthrough", func(t *testing.T) { | ||
wrapped := &processor{} | ||
|
||
p := NewLogProcessor(wrapped, api.SeverityTrace1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
for _, sev := range severities { | ||
wrapped := &processor{} | ||
p := NewLogProcessor(wrapped, api.SeverityTrace1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
|
||
r.SetSeverity(sev) | ||
assert.True(t, p.Enabled(ctx, *r), sev.String()) | ||
|
||
if assert.Lenf(t, wrapped.EnabledCalls, 1, "Record with severity %s not passed-through", sev) { | ||
assert.Equal(t, ctx, wrapped.EnabledCalls[0].Ctx, sev.String()) | ||
assert.Equal(t, *r, wrapped.EnabledCalls[0].Record, sev.String()) | ||
} | ||
wrapped.Reset() | ||
} | ||
}) | ||
|
||
t.Run("NotEnabled", func(t *testing.T) { | ||
wrapped := &processor{} | ||
|
||
p := NewLogProcessor(wrapped, api.SeverityFatal4+1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
for _, sev := range severities { | ||
wrapped := &processor{} | ||
p := NewLogProcessor(wrapped, api.SeverityFatal4+1) | ||
ctx := context.Background() | ||
r := &log.Record{} | ||
|
||
r.SetSeverity(sev) | ||
assert.False(t, p.Enabled(ctx, *r), sev.String()) | ||
|
||
if !assert.Lenf(t, wrapped.EnabledCalls, 0, "Record with severity %s passed-through", sev) { | ||
wrapped.Reset() | ||
} | ||
assert.Lenf(t, wrapped.EnabledCalls, 0, "Record with severity %s passed-through", sev) | ||
} | ||
}) | ||
} | ||
|
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.
Isn't this allocating every iteration instead of once?
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.
Is it a problem? It is just a unit test (not even integration test). It is not a benchmark and it does not make the test execution noticeable longer.
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.
How does this line of reasoning not apply to changes submitted in this PR in general? This seems to be a subjective change with functionally equivalent code being produced. Am I missing why this shouldn't be evaluated on these merits?
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 reduces the cyclomatic complexity, the number lines of code, API of test double. I find this simpler, more readable and maintainable than requiring future developers to think when the processor needs to be reset. I do not find that simplifying test code is subjective.
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.
Right, and I am pointing out that your version, that you point out will be copied, has memory issues that should not be introduced.
I'm still not following how your subjective feelings of simplicity are more valid to the criticism of "Is it a problem? It is just a unit test (not even integration test). It is not a benchmark and it does not make the test execution noticeable longer."
Are these changes just subjective feel? If so I think performance evaluations like the one provided in this comment thread need to be evaluated.
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 removed Reset method (API) from processor type ( which is a test double)
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 not sure you need a benchmark to see the inefficient memory design of the test.
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.
Just to have some quote https://wiki.c2.com/?StructuredProgrammingWithGoToStatements
I just think we do need to care about performance in this scenario.
I do not think this is this 3% 😉
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.
Sure, but if you replace "speed" with "code quality" with what you quoted, the same applies to the changes being presented in this PR.
Like I said above, I'm not sure any of this discussion if really justified given the functional test coverage does not change with this PR.
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.
From https://en.wikipedia.org/wiki/Code_refactoring:
Feel free to close the PR if you are against the change. As the codeowner I think you the right to do it.
I created the PR as part of my work on #5861.