From 9172e40d20129895c7d696dff8adf087c3931c22 Mon Sep 17 00:00:00 2001 From: straybird Date: Mon, 12 Nov 2018 18:14:04 +0800 Subject: [PATCH] Bugfix for npe (#120) --- .../SofaTracerDigestReporterAsyncManager.java | 6 +- ...cerStatisticReporterCycleTimesManager.java | 5 +- ...aTracerDigestReporterAsyncManagerTest.java | 72 +++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/SofaTracerDigestReporterAsyncManagerTest.java diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/manager/SofaTracerDigestReporterAsyncManager.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/manager/SofaTracerDigestReporterAsyncManager.java index 2cffc844..ae30a2bd 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/manager/SofaTracerDigestReporterAsyncManager.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/manager/SofaTracerDigestReporterAsyncManager.java @@ -40,8 +40,10 @@ public static AsyncCommonDigestAppenderManager getSofaTracerDigestReporterAsyncM if (asyncCommonDigestAppenderManager == null) { synchronized (SofaTracerDigestReporterAsyncManager.class) { if (asyncCommonDigestAppenderManager == null) { - asyncCommonDigestAppenderManager = new AsyncCommonDigestAppenderManager(1024); - asyncCommonDigestAppenderManager.start("NetworkAppender"); + AsyncCommonDigestAppenderManager localManager = new AsyncCommonDigestAppenderManager( + 1024); + localManager.start("NetworkAppender"); + asyncCommonDigestAppenderManager = localManager; } } } diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/stat/manager/SofaTracerStatisticReporterCycleTimesManager.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/stat/manager/SofaTracerStatisticReporterCycleTimesManager.java index b54a2236..1f9f5175 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/stat/manager/SofaTracerStatisticReporterCycleTimesManager.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/stat/manager/SofaTracerStatisticReporterCycleTimesManager.java @@ -64,8 +64,9 @@ public static SofaTracerStatisticReporterManager getSofaTracerStatisticReporterM if (existedManager == null) { synchronized (cycleTimesManager) { if (cycleTimesManager.get(cycleTime) == null) { - existedManager = new SofaTracerStatisticReporterManager(cycleTime); - cycleTimesManager.put(cycleTime, existedManager); + cycleTimesManager.put(cycleTime, new SofaTracerStatisticReporterManager( + cycleTime)); + existedManager = cycleTimesManager.get(cycleTime); } } } diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/SofaTracerDigestReporterAsyncManagerTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/SofaTracerDigestReporterAsyncManagerTest.java new file mode 100644 index 00000000..2080b9cd --- /dev/null +++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/digest/SofaTracerDigestReporterAsyncManagerTest.java @@ -0,0 +1,72 @@ +/* + * 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 com.alipay.common.tracer.core.reporter.digest; + +import com.alipay.common.tracer.core.appender.manager.AsyncCommonDigestAppenderManager; +import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.mockito.Mockito.mock; + +/** + * + * @author abby.zh + * @version $Id: SofaTracerDigestReporterAsyncManagerTest.java, v 0.1 2018/11/12 4:55 PM abby.zh Exp $ + * @since 3.0.1 + */ +public class SofaTracerDigestReporterAsyncManagerTest { + + private SofaTracerSpan sofaTracerSpan; + + @Before + public void before() { + this.sofaTracerSpan = mock(SofaTracerSpan.class); + } + + @Test + public void testGetSofaTracerDigestReporterAsyncManager() throws Exception{ + AtomicInteger npeCount = new AtomicInteger(); + AtomicInteger successCount = new AtomicInteger(); + int testTimes = 1000; + int threadCount = 100; + CountDownLatch latch = new CountDownLatch(testTimes); + for (int times = 0 ; times < testTimes; times ++) { + Executors.newFixedThreadPool(threadCount).execute(() -> { + try { + AsyncCommonDigestAppenderManager sofaTracerDigestReporterAsyncManager = + SofaTracerDigestReporterAsyncManager.getSofaTracerDigestReporterAsyncManager(); + sofaTracerDigestReporterAsyncManager.append(sofaTracerSpan); + successCount.getAndIncrement(); + }catch (NullPointerException e){ + npeCount.getAndIncrement(); + }finally { + latch.countDown(); + } + }); + } + latch.await(); + Assert.assertEquals(0, npeCount.get()); + Assert.assertEquals(testTimes, successCount.get()); + } +}