Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in InternalThreadLocal and provides unit test #1803

Merged
merged 31 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0807761
SerializerFactory 获取Serializer时,锁住整个hashmap,导致整个过程被block
carryxyh Jan 10, 2018
65438b4
单元测试。保证一个class只有一个serializer和deserializer。单线程和多线程测试
carryxyh Jan 10, 2018
7291963
增加线程数 50 模拟多个线程来获取serializer和deserializer
carryxyh Jan 10, 2018
ed2ff0f
当cores线程数全都使用的情况下,默认线程池会把任务放入到队列中。队列满则再创建线程(总数不会超过Max线程数)
carryxyh Jan 11, 2018
f97db80
当cores线程数全都使用的情况下,默认线程池会把任务放入到队列中。队列满则再创建线程(总数不会超过Max线程数)
carryxyh Jan 11, 2018
a224bb0
Merge remote-tracking branch 'upstream/master'
carryxyh Jan 12, 2018
49aecfb
补全单元测试,测试扩展是否生效
carryxyh Jan 17, 2018
561397f
Merge remote-tracking branch 'upstream/master'
carryxyh Feb 7, 2018
60c2993
错误命名
carryxyh Feb 7, 2018
af29757
Merge remote-tracking branch 'upstream/master'
carryxyh Feb 26, 2018
4be8d61
增加@Override注解
carryxyh Feb 27, 2018
5cfe610
修复单元测试
carryxyh Feb 27, 2018
7830079
Merge remote-tracking branch 'upstream/master'
carryxyh Apr 8, 2018
a1a42c0
Merge remote-tracking branch 'upstream/master'
carryxyh Apr 25, 2018
be1418f
remove enhanced
carryxyh Apr 25, 2018
9653553
remove enhanced
carryxyh Apr 25, 2018
e28aa17
Merge remote-tracking branch 'upstream/master'
carryxyh May 5, 2018
dc41214
Faster ThreadLocal impl in internal use
carryxyh May 5, 2018
11373cf
add License
carryxyh May 5, 2018
6d4102c
fix ci failed
carryxyh May 5, 2018
c831e2c
fix ci failed
carryxyh May 5, 2018
3a055d2
fix ci failed
carryxyh May 5, 2018
722b757
fix ci failed
carryxyh May 5, 2018
3a2fad7
fix ci failed
carryxyh May 5, 2018
85249a3
Merge remote-tracking branch 'upstream/master' into fastthreadlocal
carryxyh May 14, 2018
ffac33f
remove author info
carryxyh May 14, 2018
c3ec317
fix destroy method
carryxyh May 14, 2018
1e38ad8
fix bug at method size.
carryxyh May 14, 2018
2348b1b
Merge remote-tracking branch 'upstream/master' into fastthreadlocal
carryxyh May 15, 2018
0f1d2f0
Unit test for InternalThreadLocal
carryxyh May 15, 2018
64bc20c
Unit test for InternalThreadLocal
carryxyh May 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static void removeAll() {
Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
if (v != null && v != InternalThreadLocalMap.UNSET) {
Set<InternalThreadLocal<?>> variablesToRemove = (Set<InternalThreadLocal<?>>) v;
for (InternalThreadLocal<?> tlv : variablesToRemove) {
InternalThreadLocal<?>[] variablesToRemoveArray =
variablesToRemove.toArray(new InternalThreadLocal[variablesToRemove.size()]);
for (InternalThreadLocal<?> tlv : variablesToRemoveArray) {
tlv.remove(threadLocalMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ public void run() {
Thread.sleep(2000);
}

@Test
public void testRemoveAll() throws InterruptedException {
final InternalThreadLocal<Integer> internalThreadLocal = new InternalThreadLocal<Integer>();
internalThreadLocal.set(1);
Assert.assertTrue("set failed", internalThreadLocal.get() == 1);

final InternalThreadLocal<String> internalThreadLocalString = new InternalThreadLocal<String>();
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<Integer> internalThreadLocal = new InternalThreadLocal<Integer>();
internalThreadLocal.set(1);
Assert.assertTrue("size method is wrong!", InternalThreadLocal.size() == 1);

final InternalThreadLocal<String> internalThreadLocalString = new InternalThreadLocal<String>();
internalThreadLocalString.set("value");
Assert.assertTrue("size method is wrong!", InternalThreadLocal.size() == 2);
}

@Test
public void testSetAndGet() {
final Integer testVal = 10;
Expand All @@ -70,6 +96,33 @@ public void testSetAndGet() {
Objects.equals(testVal, internalThreadLocal.get()));
}

@Test
public void testRemove() {
final InternalThreadLocal<Integer> internalThreadLocal = new InternalThreadLocal<Integer>();
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<Integer> internalThreadLocal = new InternalThreadLocal<Integer>() {
@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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}