You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/07/27 11:43:17 UTC

[GitHub] [pulsar] poorbarcode opened a new pull request, #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

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

   ### Motivation
   
   Such as `PersistentTopicTest`, `ServerCnxAuthorizationTest`, `PersistentDispatcherFailoverConsumerTest` and `MessageCumulativeAckTest` is very flaky, will throws `ClassCastException` like this: 
   
   ![class-case](https://user-images.githubusercontent.com/25195800/181237787-35e97d8d-6845-4b59-b031-8b27c853ea1f.png)
   
   I found the cause of the problem:  while executing `doReturn(pulsarResources).when(pulsar).getPulsarResources()`, Meta Store also accesses variable `PulsarService.getPulsarResources()` asynchronously in logic: `notification by zk-watcher`.
   
   
   ### Modifications
   
   Execute mock-cmd in `meta-thread`.
   
   ### Documentation
   
   Check the box below or label this PR directly.
   
   Need to update docs? 
   
   - [ ] `doc-required` 
   (Your PR needs to update docs and you will update later)
     
   - [x] `doc-not-needed` 
   (Please explain why)
     
   - [ ] `doc` 
   (Your PR contains doc changes)
   
   - [ ] `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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] mattisonchao commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
mattisonchao commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1197933592

   It looks like Mockito's bug, but I'm still not sure how Mockito causes these things. So, I'll try to find the root cause, and I'll post back here when I find out.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
poorbarcode commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1197481697

   /pulsarbot rerun-failure-checks


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] shibd commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
shibd commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1196679515

   @poorbarcode The same problem is happening here, can you take a look at it together?  https://github.com/apache/pulsar/issues/16666


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] lhotari commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
lhotari commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1376043402

   I don't see how this could fix the issue. Here's an example of a recent problem 
   
   ```
   Error:  Tests run: 45, Failures: 1, Errors: 0, Skipped: 32, Time elapsed: 41.251 s <<< FAILURE! - in org.apache.pulsar.broker.service.persistent.PersistentTopicStreamingDispatcherTest
     Error:  setup(org.apache.pulsar.broker.service.persistent.PersistentTopicStreamingDispatcherTest)  Time elapsed: 0.35 s  <<< FAILURE!
     org.mockito.exceptions.base.MockitoException: Unable to create mock instance of type 'ServerCnx'
     	at org.apache.pulsar.broker.BrokerTestUtil.spyWithClassAndConstructorArgsRecordingInvocations(BrokerTestUtil.java:61)
     	at org.apache.pulsar.broker.service.PersistentTopicTest.setup(PersistentTopicTest.java:233)
     	at org.apache.pulsar.broker.service.persistent.PersistentTopicStreamingDispatcherTest.setup(PersistentTopicStreamingDispatcherTest.java:34)
     	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
     	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
     	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
     	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69)
     	at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:361)
     	at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:296)
     	at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:823)
     	at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:590)
     	at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221)
     	at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
     	at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:969)
     	at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194)
     	at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
     	at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
     	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
     	at org.testng.TestRunner.privateRun(TestRunner.java:829)
     	at org.testng.TestRunner.run(TestRunner.java:602)
     	at org.testng.SuiteRunner.runTest(SuiteRunner.java:437)
     	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:431)
     	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:391)
     	at org.testng.SuiteRunner.run(SuiteRunner.java:330)
     	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
     	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
     	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256)
     	at org.testng.TestNG.runSuitesLocally(TestNG.java:1176)
     	at org.testng.TestNG.runSuites(TestNG.java:[1099](https://github.com/apache/pulsar/actions/runs/3875096798/jobs/6609869449#step:11:1100))
     	at org.testng.TestNG.run(TestNG.java:1067)
     	at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:135)
     	at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSingleClass(TestNGDirectoryTestSuite.java:112)
     	at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeLazy(TestNGDirectoryTestSuite.java:123)
     	at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:90)
     	at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:146)
     	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384)
     	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345)
     	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126)
     	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418)
     Caused by: org.mockito.creation.instance.InstantiationException: 
     Unable to create instance of 'ServerCnx'.
     Please ensure the target class has a constructor that matches these argument types: [org.apache.pulsar.broker.PulsarService] and executes cleanly.
     	... 41 more
     Caused by: java.lang.reflect.InvocationTargetException
     	at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.newInstance(InstrumentationMemberAccessor.java:198)
     	at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.newInstance(InstrumentationMemberAccessor.java:161)
     	at org.mockito.internal.util.reflection.ModuleMemberAccessor.newInstance(ModuleMemberAccessor.java:42)
     	at org.mockito.internal.creation.instance.ConstructorInstantiator.invokeConstructor(ConstructorInstantiator.java:70)
     	at org.mockito.internal.creation.instance.ConstructorInstantiator.withParams(ConstructorInstantiator.java:53)
     	at org.mockito.internal.creation.instance.ConstructorInstantiator.newInstance(ConstructorInstantiator.java:39)
     	at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.doCreateMock(InlineDelegateByteBuddyMockMaker.java:360)
     	at org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker.createMock(InlineDelegateByteBuddyMockMaker.java:330)
     	at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMock(InlineByteBuddyMockMaker.java:58)
     	at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:53)
     	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:84)
     	at org.mockito.Mockito.mock(Mockito.java:1964)
     	... 41 more
     Caused by: java.lang.ClassCastException: class org.apache.pulsar.broker.service.BrokerService cannot be cast to class org.apache.pulsar.broker.resources.PulsarResources (org.apache.pulsar.broker.service.BrokerService and org.apache.pulsar.broker.resources.PulsarResources are in unnamed module of loader 'app')
     	at org.apache.pulsar.broker.PulsarService.getPulsarResources(PulsarService.java:267)
     	at org.apache.pulsar.broker.service.TopicListService.<init>(TopicListService.java:103)
     	at org.apache.pulsar.broker.service.ServerCnx.<init>(ServerCnx.java:297)
     	at org.apache.pulsar.broker.service.ServerCnx.<init>(ServerCnx.java:255)
     	at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:732)
     	at org.mockito.internal.util.reflection.InstrumentationMemberAccessor$Dispatcher$ByteBuddy$NFN5n4xN.invokeWithArguments(Unknown Source)
     	at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.lambda$newInstance$0(InstrumentationMemberAccessor.java:191)
     	at org.mockito.internal.util.reflection.InstrumentationMemberAccessor.newInstance(InstrumentationMemberAccessor.java:188)
     	... 52 more
   ```
   happens in https://github.com/apache/pulsar/actions/runs/3875096798/jobs/6609869449#step:11:1088
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] Technoboy- commented on a diff in pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#discussion_r931207865


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceMockSupport.java:
##########
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker;
+
+import static org.mockito.Mockito.mock;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
+import org.apache.pulsar.metadata.impl.AbstractMetadataStore;
+import org.apache.pulsar.metadata.impl.ZKMetadataStore;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class PulsarServiceMockSupport {
+
+    /**
+     * see: https://github.com/apache/pulsar/pull/16821.
+     * While executing "doReturn(pulsarResources).when(pulsar).getPulsarResources()", Meta Store Thread also accesses
+     * variable PulsarService.getPulsarResources() asynchronously in logic: "notification by zk-watcher".
+     * So execute mock-cmd in meta-thread (The executor of MetaStore is a single thread pool executor, so all things
+     * will be thread safety).
+     * Note: If the MetaStore's executor is no longer single-threaded, should mock as single-threaded if you need to
+     * execute this method.
+     */
+    public static void mockPulsarServiceProps(final PulsarService pulsarService, Runnable mockTask)
+            throws ExecutionException, InterruptedException, TimeoutException {
+        final CompletableFuture<Void> mockGetPulsarResourceFuture = new CompletableFuture<>();
+        MetadataStoreExtended metadataStoreExtended = pulsarService.getLocalMetadataStore();

Review Comment:
   What is  `mockGetPulsarResourceFuture` used for ?



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] shibd commented on a diff in pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
shibd commented on code in PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#discussion_r931040970


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentTopicStreamingDispatcherTest.java:
##########
@@ -36,4 +36,4 @@ public void setup() throws Exception {
         pulsar.getConfiguration().setSystemTopicEnabled(false);
         pulsar.getConfiguration().setStreamingDispatch(true);
     }
-}
+}

Review Comment:
   useless changed.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
poorbarcode commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1196748811

   > Nice catch! I tested it locally and it can work.
   
   Thanks.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] shibd commented on a diff in pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
shibd commented on code in PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#discussion_r931051017


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java:
##########
@@ -162,7 +162,7 @@ public class PersistentTopicTest extends MockedBookKeeperTestCase {
     private SchemaRegistryService schemaRegistryService;
     private ManagedLedgerFactory mlFactoryMock;
     private ServerCnx serverCnx;
-    private MetadataStore store;
+    private ZKMetadataStore store;

Review Comment:
   This does not need change.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
poorbarcode commented on code in PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#discussion_r931065614


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java:
##########
@@ -162,7 +162,7 @@ public class PersistentTopicTest extends MockedBookKeeperTestCase {
     private SchemaRegistryService schemaRegistryService;
     private ManagedLedgerFactory mlFactoryMock;
     private ServerCnx serverCnx;
-    private MetadataStore store;
+    private ZKMetadataStore store;

Review Comment:
   Already fixed. Thanks



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] lhotari commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by "lhotari (via GitHub)" <gi...@apache.org>.
lhotari commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1403591300

   @poorbarcode This solution didn't fix the issue. Your analysis of the problem was correct. I have created a solution where the getters are no longer overridden using Mockito. The PR is #19323, please review.


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] codelipenghui merged pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
codelipenghui merged PR #16821:
URL: https://github.com/apache/pulsar/pull/16821


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
poorbarcode commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1197494244

   /pulsarbot rerun-failure-checks


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on a diff in pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
poorbarcode commented on code in PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#discussion_r931047840


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentTopicStreamingDispatcherTest.java:
##########
@@ -36,4 +36,4 @@ public void setup() throws Exception {
         pulsar.getConfiguration().setSystemTopicEnabled(false);
         pulsar.getConfiguration().setStreamingDispatch(true);
     }
-}
+}

Review Comment:
   Already fixed.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] poorbarcode commented on pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
poorbarcode commented on PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#issuecomment-1196691381

   Hi @shibd 
   
   > The same problem is happening here, can you take a look at it together? #16666
   
   Already included, it is `PersistentTopicTest`( the super class of `PersistentTopicStreamingDispatcherTest` ).


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [pulsar] Technoboy- commented on a diff in pull request #16821: [fix][flaky-test] Fix ClassCastException: BrokerService cannot be cast to class PulsarResources

Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #16821:
URL: https://github.com/apache/pulsar/pull/16821#discussion_r931211741


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/PulsarServiceMockSupport.java:
##########
@@ -0,0 +1,75 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker;
+
+import static org.mockito.Mockito.mock;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended;
+import org.apache.pulsar.metadata.impl.AbstractMetadataStore;
+import org.apache.pulsar.metadata.impl.ZKMetadataStore;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+@Slf4j
+public class PulsarServiceMockSupport {
+
+    /**
+     * see: https://github.com/apache/pulsar/pull/16821.
+     * While executing "doReturn(pulsarResources).when(pulsar).getPulsarResources()", Meta Store Thread also accesses
+     * variable PulsarService.getPulsarResources() asynchronously in logic: "notification by zk-watcher".
+     * So execute mock-cmd in meta-thread (The executor of MetaStore is a single thread pool executor, so all things
+     * will be thread safety).
+     * Note: If the MetaStore's executor is no longer single-threaded, should mock as single-threaded if you need to
+     * execute this method.
+     */
+    public static void mockPulsarServiceProps(final PulsarService pulsarService, Runnable mockTask)
+            throws ExecutionException, InterruptedException, TimeoutException {
+        final CompletableFuture<Void> mockGetPulsarResourceFuture = new CompletableFuture<>();
+        MetadataStoreExtended metadataStoreExtended = pulsarService.getLocalMetadataStore();

Review Comment:
   Got it.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org