/*
 * 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.rocketmq.broker;

import static org.apache.rocketmq.store.stats.BrokerStatsManager.TOPIC_PUT_NUMS;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.rocketmq.common.ThreadFactoryImpl;
import org.apache.rocketmq.store.stats.BrokerStatsManager;
import org.junit.Before;
import org.junit.Test;

public class BrokerStatsManagerTest {

    private BrokerStatsManager brokerStatsManager;
    private ThreadPoolExecutor executor;
    @Before
    public void init() {
        brokerStatsManager = new BrokerStatsManager("DefaultCluster");
        executor = new ThreadPoolExecutor(100, 200, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1000), new ThreadFactoryImpl("testMultiThread"));
    }

    @Test
    public void test_getAndCreateStatsItem_multiThread() throws InterruptedException {

        for(int i =0; i < 10000; i++) {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    brokerStatsManager.incTopicPutNums("topicTest", 2, 1);
                }
            });
        }
        Thread.sleep(5000);
        System.out.println(brokerStatsManager.getStatsItem(TOPIC_PUT_NUMS,"topicTest").getValue());
    }
}
@@ -913,16 +913,61 @@
 UT_NUMS;
+%0Aimport static org.junit.Assert.assertEquals;
 %0A%0Aimport
@@ -1094,16 +1094,63 @@
 meUnit;%0A
+import java.util.concurrent.atomic.AtomicLong;%0A
 import o
@@ -1270,22 +1270,21 @@
 g.junit.
-Before
+After
 ;%0Aimport
@@ -1436,42 +1436,281 @@
 or;%0A
+%0A
     @
-Before%0A    public void init()
+Test%0A    public void test_getAndCreateStatsItem_multiThread() throws InterruptedException %7B%0A        for (int i = 0; i %3C 5; i++) %7B%0A            assertEquals(20000L, test_unit().longValue());%0A        %7D%0A    %7D%0A%0A    public AtomicLong test_unit() throws InterruptedException
  %7B%0A 
@@ -1852,16 +1852,32 @@
 SECONDS,
+%0A               
  new Arr
@@ -1906,16 +1906,17 @@
 le%3E(1000
+0
 ), new T
@@ -1957,113 +1957,8 @@
 ));%0A
-    %7D%0A%0A    @Test%0A    public void test_getAndCreateStatsItem_multiThread() throws InterruptedException %7B%0A%0A
     
@@ -1964,24 +1964,25 @@
      for
+ 
 (int i =
 0; i %3C 1
@@ -1973,16 +1973,17 @@
 (int i =
+ 
 0; i %3C 1
@@ -2213,32 +2213,157 @@
   %7D);%0A        %7D%0A
+        while (true) %7B%0A            if (executor.getCompletedTaskCount() == 10000) %7B%0A                break;%0A            %7D%0A    
         Thread.s
@@ -2367,17 +2367,17 @@
 d.sleep(
-5
+1
 000);%0A  
@@ -2386,27 +2386,25 @@
     
-System.out.println(
+%7D%0A        return 
 brok
@@ -2446,16 +2446,17 @@
 UT_NUMS,
+ 
 %22topicTe
@@ -2466,22 +2466,97 @@
 ).getValue()
+;%0A    %7D%0A%0A    @After%0A    public void shutdown() %7B%0A        executor.shutdown(
 );%0A    %7D%0A%7D

rewrite unit test BrokerStatsManagerTest.java