patsonluk opened a new pull request, #1457: URL: https://github.com/apache/solr/pull/1457
https://issues.apache.org/jira/browse/SOLR-16696 # Description Proposing a new `Breakpoint` feature in `CommonTestInjection` which acts similar to breakpoints in IDE. This allows blocking/checking conditions at the injection point, which could be useful for race condition emulation and exception verification. # Solution Added a new interface `CommonTestInjection$Breakpoint`, with a single method `executeAndResume`. 2 new methods are introduced in `CommonTestInjection`. `injectBreakpoint(breakpointKey)` should be added in the actual code flow guarded by `assert`, for example `assert injectBreakpoint(MyClass.getClass().getName())`. `assert` will make sure the breakpoint logic would only be evaluated during testing. The actual breakpoint is set by test cases which calls `setBreakpoint(breakpointKey, breakpointImpl)`, the breakpoint implementation would implement `executeAndResume` method. Example usages: ### Race condition In the `RaceClass` instance: ``` public void racingMethod() { //for example a race condition can arise if raceMethod() is invoked and methodA() is called before methodB() assert CommonTestInjection.injectBreakpoint(RaceClass.class.getName()); methodB(); } ``` in unit test case: ``` CommonTestInjection.setBreakpoint(RaceClass.class.getName(), () -> methodA()); //... raceClass.racingMethod(); ``` ### Caught exception verification In the target class: ``` public void myMethod() { try { //... } catch (SomeException e) { assert CommonTestInjection.injectBreakpoint("SomeExceptionCheck"); //verify the SomeException was indeed thrown and caught //do not re-throw the exception } } ``` In unit test case: ``` AtomicBoolean exceptionSeen = new AtomicBoolean(false); CommonTestInjection.setBreakpoint("SomeExceptionCheck", () -> exceptionSeen.set(true)) instanace.myMethod(); assertTrue(exceptionSeen.get()) ``` # Tests No extra tests are added. # Checklist Please review the following and check all that apply: - [x] I have reviewed the guidelines for [How to Contribute](https://wiki.apache.org/solr/HowToContribute) and my code conforms to the standards described there to the best of my ability. - [x] I have created a Jira issue and added the issue ID to my pull request title. - [ ] I have given Solr maintainers [access](https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork) to contribute to my PR branch. (optional but recommended) - [x] I have developed this patch against the `main` branch. - [x] I have run `./gradlew check`. - [ ] I have added tests for my changes. - [ ] I have added documentation for the [Reference Guide](https://github.com/apache/solr/tree/main/solr/solr-ref-guide) -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
