Skip to content

Reject @BeforeMethod without singleThreaded=true#15756

Closed
v-jizhang wants to merge 1 commit intoprestodb:masterfrom
v-jizhang:checkstyle
Closed

Reject @BeforeMethod without singleThreaded=true#15756
v-jizhang wants to merge 1 commit intoprestodb:masterfrom
v-jizhang:checkstyle

Conversation

@v-jizhang
Copy link
Contributor

@BeforeMethod in a class that is not @test(singleThreaded=true) is
usually (always?) a bug. We should automatically find and reject them.

This change uses Regex negative LookBehind match to find @BeforeMethod
in a class that is not @test(singleThreaded=true). Only top 200 lines
of a class is scanned to save time and avoid potential stack overflow
if a file is too big. This works because @BeforeMethod should appear at
top a class definition.

Some violations have been caught and are fixed in this change too.

Resolves: #12044

== NO RELEASE NOTE ==

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Feb 25, 2021

CLA Signed

The committers are authorized under a signed CLA.

  • ✅ Jinlin Zhang (14197cd4222f1ac115d4380c00cc9a185b73941b)

@aweisberg aweisberg self-requested a review March 16, 2021 14:37
Copy link
Contributor

@aweisberg aweisberg left a comment

Choose a reason for hiding this comment

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

Some feedback on thread safety and questions about the regex

Copy link
Contributor

Choose a reason for hiding this comment

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

This is immutable for the duration of the test so it can be static (and final?) and constructed in a @BeforeTest method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can be static. To construct it @BeforeTest, it can't be final. I'll make the change. Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

Another immutable thing. You can switch to @BeforeTest and make it static.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also immutable, static and @BeforeTest

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this field. MockEventClient is not thread safe. Each test will need to pass this around on the stack or as a thread local (don't recommend that here).

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use a simpler regex that is less likely to fail if the @BeforeMethod is later in the class.
Such as check for the BeforeMethod import (guaranteed to come first) and then singleThreaded=True?

How reliable is this regex going to be? Seems like several places could have whitespace that aren't accounted for in the regex. I know our style checks forbid a lot of them, but I just want to check in and find out what you tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you use a simpler regex that is less likely to fail if the @BeforeMethod is later in the class.
Such as check for the BeforeMethod import (guaranteed to come first) and then singleThreaded=True?

Good idea, I'll try that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How reliable is this regex going to be? Seems like several places could have whitespace that aren't accounted for in the regex. I know our style checks forbid a lot of them, but I just want to check in and find out what you tested?

Yes, white spaces are checked by other style checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you use a simpler regex that is less likely to fail if the @BeforeMethod is later in the class.
Such as check for the BeforeMethod import (guaranteed to come first) and then singleThreaded=True?

This can't be a simple regex. if BeforeMethod import exists, the test class must be annotated by singleThreaded=True. But we check for violations, that is, BeforeMethod import exists, but singleThreaded=True does not. That is negative LookBefore.

Copy link
Contributor

Choose a reason for hiding this comment

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

If we can match on the acceptable method can we negate it with something like

    static String negateRegex(String regex) {
        return "(?!(?:" + regex + ")$).*";
    }

My regex skills are not that strong. Seems like negating a regex is generally possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like it is as complicated as negative lookbehind. And ".*" at the end consumes everything of a file, for a big file, the regex engine could raise a stack overflow exception.

Copy link
Contributor

@aweisberg aweisberg left a comment

Choose a reason for hiding this comment

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

Please squash the commits. I do appreciate changes during review being represented as commits (keep that up, some people on the project are unfamiliar with this practice and will ask you to squash and it is ok) instead of force pushes (much easier to review), but we try to only merge working versions without failing tests to master.

@v-jizhang
Copy link
Contributor Author

Please squash the commits. I do appreciate changes during review being represented as commits (keep that up, some people on the project are unfamiliar with this practice and will ask you to squash and it is ok) instead of force pushes (much easier to review), but we try to only merge working versions without failing tests to master.

Got a message from squash "(nothing to squash)Already up to date." That because I force pushed.

@aweisberg
Copy link
Contributor

I still see two commits in the PR. It should be possible to git log to get a hash and then git rebase -i <hash> to squash commits together and then force push.

@BeforeMethod in a class that is not @test(singleThreaded=true) is
usually (always?) a bug. We should automatically find and reject them.

This change uses Regex negative LookBehind match to find @BeforeMethod
in a class that is not @test(singleThreaded=true). Only top 200 lines
of a class is scanned to save time and avoid potential stack overflow
if a file is too big. This works because @BeforeMethod should appear at
top a class definition.

Some violations have been caught and are fixed in this change too.
@v-jizhang
Copy link
Contributor Author

@aweisberg Squashed. Thanks

@aweisberg aweisberg requested a review from pettyjamesm April 2, 2021 17:49
@aweisberg
Copy link
Contributor

@pettyjamesm do you mind merging this?

Copy link
Contributor

@pettyjamesm pettyjamesm left a comment

Choose a reason for hiding this comment

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

The test implementation changes to fix the usages of @BeforeMethod LGTM. The usage of a regex rule to enforce it though does seem kind of brittle and has some performance risks, ask you pointed out.

It looks like Trino implemented a similar check in trinodb/trino#5412 and trinodb/trino#5420 using a test listener instead, which seems like a good compromise.

@v-jizhang
Copy link
Contributor Author

The test implementation changes to fix the usages of @BeforeMethod LGTM. The usage of a regex rule to enforce it though does seem kind of brittle and has some performance risks, ask you pointed out.

It looks like Trino implemented a similar check in trinodb/trino#5412 and trinodb/trino#5420 using a test listener instead, which seems like a good compromise.

I'll take a look. Thanks.

@aweisberg
Copy link
Contributor

Replaced by #15914

@aweisberg aweisberg closed this Apr 7, 2021
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.

Reject @BeforeMethod annotations on tests without singleThreaded=true

3 participants