ruanhang1993 opened a new pull request #17556:
URL: https://github.com/apache/flink/pull/17556
## What is the purpose of the change
This pull request provides some junit5 extensions to replace the existed
junit4 rules.
## Brief change log
- Add corresponding extensions for the existed junit4 rules. The
extensions are named in a format of `xxxxExtension.java`, and replace the word
_Resource_ to _Extension_ if the rule's filename is like `xxxResource.java`,
e.g. `MiniClusterResource.java` corresponds to `MiniClusterExtension.java`.
- Add `CustomExtension`, `AllCallbackWrapper` and `EachCallbackWrapper`
for the rules that is both used as `@Rule` and `@ClassRule`.
- Add retry extension in junit5, whose behavior is different from the
`RetryRule`.
- Add `TestLoggerExtension` to replace `TestLogger`
## Verifying this change
This change is already covered by existing tests.
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): (no)
- The public API, i.e., is any changed class annotated with
`@Public(Evolving)`: (no)
- The serializers: (no)
- The runtime per-record code paths (performance sensitive): (no)
- Anything that affects deployment or recovery: JobManager (and its
components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
- The S3 file system connector: (no)
## Documentation
- Does this pull request introduce a new feature? (no)
- If yes, how is the feature documented? (not applicable)
## Change Details
### 1. How to change junit4 rules to junit5 extension
A `TestRule` could be used as a `@Rule` or a `@ClassRule` in Junit4. We
could make use of `BeforeEachCallback` and `AfterEachCallback` to rewrite
`@Rule`, and use `BeforeAllCallback` and `AfterAllCallback` to rewrite
`@ClassRule`.
So if the TestRule is only used as a `@Rule`, the new extension class will
implement `BeforeEachCallback` and `AfterEachCallback`. if the TestRule is only
used as a `@ClassRule`, the new extension class will implement
`BeforeAllCallback` and `AfterAllCallback`.
There are some rules used both as a `@Rule` and a `@ClassRule` in flink. But
a extension in junit5 can not implement both afterCallback and eachCallback,
which will lead to some odd behaviors.
So if a TestRule is used both as a `@Rule` and a `@ClassRule`, the new
extension should implement the provided `CustomExtension` interface. And it
should be used with a `AllCallbackWrapper` or `EachCallbackWrapper`, and
annotated with `@RegisterExtension`. A use case has already provided in
`flink-runtime/src/test/java/org/apache/flink/runtime/taskmanager/TaskCancelAsyncProducerConsumerITCase.java`.
### 2. The changed behavior in `RetryExtension` compared to `RetryRule`
If we want to execute a test multiple times in Junit5, we need implement
`TestTemplateInvocationContextProvider` and annotate a test by `@TestTemplate`.
And use a `TestExecutionExceptionHandler` to decide a test whether should be
executed.
In this way, the number of retry tests in Junit5 is different from junit4.
Because retry tests in Junit5 will plan to do test multiple times at the
beginning, it will skip the following tests if has reached some condition, like
succeed or throwing some unexpected exception.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]