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

teatest: fix race condition #366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kalensk
Copy link

@kalensk kalensk commented Feb 8, 2025

I am running into a race condition where the final model would still be nil rather than the expected model even after the program completed the the done signal sent. This seems to fix it.

Send the final model ‘before’ sending the program is done signal, such that FinalModel() can correctly receive the final model from modelCh rather than returning the default tm.model which is null.

func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model {
	tm.waitDone(tb, opts)
	select {
	case m := <-tm.modelCh:
		tm.model = m
		return tm.model
	default:
		return tm.model
	}
}

Specifically, in the above code tm.waitDone(tb, opts) would wait for doneCh to receive true before selecting on the tm.modelCh which might not receive the final model since it is being sent 'after' the doneCh signal is sent. This fixes that ordering issue.

cc: @aymanbagabas

Send the final model 'before' sending the program is done signal, such
that `FinalModel()` can correctly receive the final model from `modelCh`
rather than returning the default `tm.model` which is nil.
@kalensk kalensk requested a review from caarlos0 as a code owner February 8, 2025 02:20
@kalensk
Copy link
Author

kalensk commented Feb 11, 2025

@caarlos0 Let me know if this makes sense or any other questions. Happy to try and help.

Copy link
Member

@aymanbagabas aymanbagabas left a comment

Choose a reason for hiding this comment

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

Could you write a test case covering this one?

@kalensk
Copy link
Author

kalensk commented Feb 11, 2025

Could you write a test case covering this one?

Unfortunately, I can't think of a test that would catch this without adding undo time to the tests or without other negative side effects. Can you?

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.

2 participants