lhotari opened a new pull request, #23892:
URL: https://github.com/apache/pulsar/pull/23892

   ### Motivation
   
   In some Pulsar tests, there's a need to inject special behaviors such as 
delays or failures.
   The current solutions require using reflection and replacing instances, 
which results in hard to maintain test code.
   
   ### Modifications
   
   - Add a solution that can intercept topic, subscription and dispatcher 
instances in tests.
   - Refactor production code so that there are hooks for intercepting the 
instances in subclasses
   - Add a helper method for creating a Mockito spy for a dispatcher instance.
   
   example usage for spying on a dispatcher instance:
   
   ```java
       @Override
       protected void doInitConf() throws Exception {
           super.doInitConf();
           BrokerTestInterceptor.INSTANCE.configure(conf);
       }
   
       @AfterMethod(alwaysRun = true)
       protected void resetInterceptors() throws Exception {
           BrokerTestInterceptor.INSTANCE.reset();
       }
   
       @Test
       public void testSomething() throws Exception {
           
BrokerTestInterceptor.INSTANCE.applyDispatcherSpyDecorator(PersistentDispatcherMultipleConsumers.class,
                   spy -> {
                       doAnswer(invocation -> {
                           List<Entry> entries = invocation.getArgument(0);
                           log.info("intercepted readEntriesComplete with {} 
entries", entries.size());
                           return invocation.callRealMethod();
                       }).when(spy).readEntriesComplete(any(), any());
                   }
           );
      ...
   ```
   
   ### Documentation
   
   <!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
   
   - [ ] `doc` <!-- Your PR contains doc changes. -->
   - [ ] `doc-required` <!-- Your PR changes impact docs and you will update 
later -->
   - [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
   - [ ] `doc-complete` <!-- Docs have been already added -->


-- 
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]

Reply via email to