diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocal.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocal.java index 4eed57c4e84..d3d6489f994 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocal.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocal.java @@ -58,7 +58,9 @@ public static void removeAll() { Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex); if (v != null && v != InternalThreadLocalMap.UNSET) { Set> variablesToRemove = (Set>) v; - for (InternalThreadLocal tlv : variablesToRemove) { + InternalThreadLocal[] variablesToRemoveArray = + variablesToRemove.toArray(new InternalThreadLocal[variablesToRemove.size()]); + for (InternalThreadLocal tlv : variablesToRemoveArray) { tlv.remove(threadLocalMap); } } diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocalTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocalTest.java index b0a8c28c294..401906ee328 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocalTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/InternalThreadLocalTest.java @@ -61,6 +61,32 @@ public void run() { Thread.sleep(2000); } + @Test + public void testRemoveAll() throws InterruptedException { + final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); + internalThreadLocal.set(1); + Assert.assertTrue("set failed", internalThreadLocal.get() == 1); + + final InternalThreadLocal internalThreadLocalString = new InternalThreadLocal(); + internalThreadLocalString.set("value"); + Assert.assertTrue("set failed", "value".equals(internalThreadLocalString.get())); + + InternalThreadLocal.removeAll(); + Assert.assertTrue("removeAll failed!", internalThreadLocal.get() == null); + Assert.assertTrue("removeAll failed!", internalThreadLocalString.get() == null); + } + + @Test + public void testSize() throws InterruptedException { + final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); + internalThreadLocal.set(1); + Assert.assertTrue("size method is wrong!", InternalThreadLocal.size() == 1); + + final InternalThreadLocal internalThreadLocalString = new InternalThreadLocal(); + internalThreadLocalString.set("value"); + Assert.assertTrue("size method is wrong!", InternalThreadLocal.size() == 2); + } + @Test public void testSetAndGet() { final Integer testVal = 10; @@ -70,6 +96,33 @@ public void testSetAndGet() { Objects.equals(testVal, internalThreadLocal.get())); } + @Test + public void testRemove() { + final InternalThreadLocal internalThreadLocal = new InternalThreadLocal(); + internalThreadLocal.set(1); + Assert.assertTrue("get method false!", internalThreadLocal.get() == 1); + + internalThreadLocal.remove(); + Assert.assertTrue("remove failed!", internalThreadLocal.get() == null); + } + + @Test + public void testOnRemove() { + final Integer[] valueToRemove = {null}; + final InternalThreadLocal internalThreadLocal = new InternalThreadLocal() { + @Override + protected void onRemoval(Integer value) throws Exception { + //value calculate + valueToRemove[0] = value + 1; + } + }; + internalThreadLocal.set(1); + Assert.assertTrue("get method false!", internalThreadLocal.get() == 1); + + internalThreadLocal.remove(); + Assert.assertTrue("onRemove method failed!", valueToRemove[0] == 2); + } + @Test public void testMultiThreadSetAndGet() throws InterruptedException { final Integer testVal1 = 10; diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java new file mode 100644 index 00000000000..5d80023c5d9 --- /dev/null +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/threadlocal/NamedInternalThreadFactoryTest.java @@ -0,0 +1,36 @@ +/* + * 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.alibaba.dubbo.common.threadlocal; + +import org.junit.Assert; +import org.junit.Test; + +public class NamedInternalThreadFactoryTest { + + @Test + public void newThread() throws Exception { + NamedInternalThreadFactory namedInternalThreadFactory = new NamedInternalThreadFactory(); + Thread t = namedInternalThreadFactory.newThread(new Runnable() { + @Override + public void run() { + + } + }); + Assert.assertTrue("thread is not InternalThread", t.getClass().equals(InternalThread.class)); + } +} \ No newline at end of file