Skip to content

fix: race condition in cookie test#9659

Merged
sspaink merged 2 commits intomasterfrom
fixcookie
Sep 2, 2021
Merged

fix: race condition in cookie test#9659
sspaink merged 2 commits intomasterfrom
fixcookie

Conversation

@sspaink
Copy link
Copy Markdown
Contributor

@sspaink sspaink commented Aug 20, 2021

The tests in cookie_test.go are still failing randomly (link to circleci failed Mac build on master), I thought I had resolved this issue in this pr #9608. I believe the problem is due to a race condition, where the go-routine running the ticker loop to authenticate again isn't guaranteed to be called before checking the count.

This pr adds a sync.WaitGroup and a context.Context with cancel, to guarantee that the ticker loop has finished running before checking the count. Also moved the authentication renewal loop to a separate function so that the tests can pass ticker and then call ticker.Close(). Refactored the tests away from using the assert function as well to reduce repeated code.

@telegraf-tiger telegraf-tiger bot added the feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin label Aug 20, 2021
@sspaink sspaink added fix pr to fix corresponding bug ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. and removed feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin labels Aug 20, 2021
@sspaink sspaink requested a review from reimda August 26, 2021 21:59
// continual auth renewal if set
if c.Renewal > 0 {
ticker := clock.Ticker(time.Duration(c.Renewal))
go c.authRenewal(context.Background(), ticker, log)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The background context is never going to get cancelled, I assume this is by design here. In the unit test you use a context with cancel to be able to cleanly stop the authRenewal goroutine.

Using the background context here confused me for a few minutes. Maybe it would be good to add a comment explaining that it's only cancelled in tests, so background is appropriate in Start()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added a comment explaining its use, you are right this context was just intended for tests.

@reimda
Copy link
Copy Markdown
Contributor

reimda commented Aug 26, 2021

@jh125486 I think you wrote this earlier this year. How does this fix for intermittent unit test failures look to you?

@jh125486
Copy link
Copy Markdown
Contributor

jh125486 commented Sep 1, 2021

@jh125486 I think you wrote this earlier this year. How does this fix for intermittent unit test failures look to you?

Looks good, but this is honestly the first time I've seen/used circleci, so I'm by no ones an expert on this.

@sspaink sspaink merged commit 167b6e0 into master Sep 2, 2021
@sspaink sspaink deleted the fixcookie branch September 2, 2021 05:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix pr to fix corresponding bug ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants