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

Assert that ActionListener.runBefore/runAfter are executed once #93143

Merged
merged 3 commits into from
Jan 23, 2023

Conversation

tlrx
Copy link
Member

@tlrx tlrx commented Jan 23, 2023

I've recently encountered a bug (illustrated by a test in this pull request) where a ActionListener.runAfter listener got called multiple times because of an unexpected exception was thrown in a wrapped listener.

Asserting that the runAfter or releaseAfter is called only once would have helped a lot to catch the bug. We don't enforce listeners (or delegating listeners) to be called once and some of them are designed on purpose to be called multiple times, yet I think we can expect runBefore/runAfter to be used to release resources and as such they should be run once.

I remain open to contrary opinions of course.

@tlrx tlrx added >non-issue :Core/Infra/Core Core issues without another label v8.7.0 labels Jan 23, 2023
Copy link
Contributor

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -452,6 +467,14 @@ public void onFailure(Exception e) {
super.onFailure(e);
}

private boolean assertNotExecuted() {
if (Assertions.ENABLED) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can't get here if assertions are disabled I think

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, thanks! e62d927

@@ -454,6 +457,63 @@ public void onFailure(Exception e) {
assertThat(exReference.get(), instanceOf(IllegalStateException.class));
}

public void testRunBeforeThrowsAssertionErrorIfExecutedMoreThanOnce() {
assumeTrue("test only works with assertions enabled", Assertions.ENABLED);
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh TIL it's possible to run tests with assertions disabled! I wouldn't be surprised if this didn't work in lots of other ways too tho.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep...

Copy link
Contributor

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

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

Sorry forgot to press the button.

FWIW I think the way ActionListener#wrap sometimes calls both onResponse and onFailure is kinda evil. Useful sometimes, but evil nonetheless.

@tlrx tlrx marked this pull request as ready for review January 23, 2023 11:38
@tlrx
Copy link
Member Author

tlrx commented Jan 23, 2023

@elasticmachine run update branch

@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Jan 23, 2023
@tlrx
Copy link
Member Author

tlrx commented Jan 23, 2023

@elasticmachine update branch

@tlrx tlrx merged commit 294978c into elastic:main Jan 23, 2023
@tlrx tlrx deleted the assert-run-after-before-executed-once branch January 23, 2023 12:28
@tlrx
Copy link
Member Author

tlrx commented Jan 23, 2023

Thanks David

FWIW I think the way ActionListener#wrap sometimes calls both onResponse and onFailure is kinda evil. Useful sometimes, but evil nonetheless.

++, something to be aware of

DaveCTurner added a commit to DaveCTurner/elasticsearch that referenced this pull request Jan 23, 2023
Alternative approach to elastic#93143, avoiding adding overhead in production.
elasticsearchmachine pushed a commit that referenced this pull request Jan 24, 2023
Alternative approach to #93143, avoiding adding overhead in production.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label >non-issue Team:Core/Infra Meta label for core/infra team v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants