Reject @BeforeMethod without singleThreaded=true#15756
Reject @BeforeMethod without singleThreaded=true#15756v-jizhang wants to merge 1 commit intoprestodb:masterfrom
Conversation
aweisberg
left a comment
There was a problem hiding this comment.
Some feedback on thread safety and questions about the regex
presto-benchmark-runner/src/test/java/com/facebook/presto/benchmark/retry/TestRetryDriver.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
This is immutable for the duration of the test so it can be static (and final?) and constructed in a @BeforeTest method.
There was a problem hiding this comment.
It can be static. To construct it @BeforeTest, it can't be final. I'll make the change. Thanks
There was a problem hiding this comment.
Another immutable thing. You can switch to @BeforeTest and make it static.
There was a problem hiding this comment.
Also immutable, static and @BeforeTest
There was a problem hiding this comment.
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).
src/checkstyle/presto-checks.xml
Outdated
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Could you use a simpler regex that is less likely to fail if the
@BeforeMethodis later in the class.
Such as check for theBeforeMethodimport (guaranteed to come first) and thensingleThreaded=True?
Good idea, I'll try that.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Could you use a simpler regex that is less likely to fail if the
@BeforeMethodis later in the class.
Such as check for theBeforeMethodimport (guaranteed to come first) and thensingleThreaded=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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
|
I still see two commits in the PR. It should be possible to |
@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.
|
@aweisberg Squashed. Thanks |
|
@pettyjamesm do you mind merging this? |
There was a problem hiding this comment.
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. |
|
Replaced by #15914 |
@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