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

Add tests for dlq_handler.go #6071

Merged
merged 3 commits into from
Jun 4, 2024

Conversation

fimanishi
Copy link
Member

@fimanishi fimanishi commented May 29, 2024

What changed?
tests for dlq_handler.go. Moved getInterval function to dlqHandleImpl to allow testing

Why?
improve unit test coverage

How did you test it?
unit tests

Potential risks
getInterval behavior change due to change of location definition

Release notes

Documentation Changes

@fimanishi fimanishi force-pushed the tests-for-dlq_handler branch 2 times, most recently from 6c754a8 to 767144e Compare May 29, 2024 22:17
What changed?
tests for dlq_handler.go. Moved getInterval function to dlqHandleImpl to allow testing

Why?
improve unit test coverage

How did you test it?
unit tests

Potential risks

Release notes

Documentation Changes
Copy link

codecov bot commented May 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.36%. Comparing base (919f416) to head (f04d44a).
Report is 36 commits behind head on master.

Additional details and impacted files
Files Coverage Δ
service/history/replication/dlq_handler.go 100.00% <100.00%> (+44.51%) ⬆️

... and 49 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

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

@coveralls
Copy link

coveralls commented May 30, 2024

Pull Request Test Coverage Report for Build 018fdeff-1562-4c21-8049-dfdd6139a4ee

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 2049 unchanged lines in 42 files lost coverage.
  • Overall coverage decreased (-0.3%) to 69.384%

Files with Coverage Reduction New Missed Lines %
service/history/queue/timer_queue_processor_base.go 1 78.28%
common/task/weighted_round_robin_task_scheduler.go 2 89.05%
common/dynamicconfig/constants.go 2 99.05%
common/persistence/taskManager.go 2 74.49%
common/persistence/visibility_single_manager.go 2 99.33%
common/persistence/nosql/utils.go 2 60.0%
service/history/task/task.go 3 84.81%
service/matching/poller/history.go 3 74.0%
common/persistence/wrappers/errorinjectors/utils.go 3 93.7%
service/matching/tasklist/task_gc.go 3 92.11%
Totals Coverage Status
Change from base Build 018fdeeb-1615-4507-ae03-236a4f5abcab: -0.3%
Covered Lines: 102228
Relevant Lines: 147337

💛 - Coveralls


for _, tc := range tests {
s.T().Run(tc.name, func(t *testing.T) {
s.messageHandler.status = tc.status
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 also validate no goroutines leaked after stopping.

defer goleak.VerifyNone(t)

}
}

func (s *dlqHandlerSuite) TestStop() {
Copy link
Member

Choose a reason for hiding this comment

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

what is the point of this test given that above already covers start/stop flow?

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed


func (s *dlqHandlerSuite) TestEmitDLQSizeMetricsLoop_StopsWhenDoneSignalReceived() {
s.messageHandler.status = common.DaemonStatusStarted
go s.messageHandler.emitDLQSizeMetricsLoop()
Copy link
Member

Choose a reason for hiding this comment

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

validate these goroutines are terminated via defer goleak.VerifyNone(t)


func (s *dlqHandlerSuite) TestEmitDLQSizeMetricsLoop_FetchesAndEmitsMetricsPeriodically() {
emissionNumber := 2
interval := 1 * time.Second
Copy link
Member

Choose a reason for hiding this comment

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

you can lower this to 100ms or something to lower the overall test run time

Copy link
Member Author

Choose a reason for hiding this comment

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

Made an attempt to use timeSource and Advance. Let me know what you think. Had to make some changes to the methods to avoid using sleep, as suggested by @3vilhamster

Copy link
Member

@3vilhamster 3vilhamster left a comment

Choose a reason for hiding this comment

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

Every time.Sleep in our tests is increasing testing time and can introduce flackiness to tests on CI. Please, avoid using it.

}

timer := time.NewTimer(getInterval())
timer := time.NewTimer(r.getInterval())
Copy link
Member

Choose a reason for hiding this comment

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

I recommend using clock.TimeSource to mimic time-related behaviour.

It allows to have the same behavior for prod and be able to mimic time changes in tests without any time delays.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I've made and attempt to fix that. Unfortunately the timer was set inside of the method being called by the goroutine, so I had to make further changes in order to be able to use Advance without using sleep. Let me know what you think.


go s.messageHandler.emitDLQSizeMetricsLoop(timer)

// Advance time to trigger the next emission
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for doing that. It looks better now.
However, I recommend adding a call

mockTimeSource.BlockUntil(1)

To ensure the goroutine was started and is blocked on the awaiting timer.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is awesome. I was looking for something like that. Thank you for all the suggestions, really appreciate it!

Copy link
Member

@3vilhamster 3vilhamster left a comment

Choose a reason for hiding this comment

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

Thank you for injecting timeSource. I think it is better now.

@fimanishi fimanishi merged commit 93130b2 into cadence-workflow:master Jun 4, 2024
20 checks passed
@fimanishi fimanishi deleted the tests-for-dlq_handler branch June 4, 2024 16:04
timl3136 pushed a commit to timl3136/cadence that referenced this pull request Jun 6, 2024
* Add tests for dlq_handler.go

What changed?
tests for dlq_handler.go. Moved getInterval function to dlqHandleImpl to allow testing

Why?
improve unit test coverage

How did you test it?
unit tests

Potential risks

Release notes

Documentation Changes
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.

4 participants