Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿啄debugIT
transmittable-thread-local
提交
9450809a
T
transmittable-thread-local
项目概览
阿啄debugIT
/
transmittable-thread-local
与 Fork 源项目一致
从无法访问的项目Fork
通知
8
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
transmittable-thread-local
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9450809a
编写于
11月 05, 2018
作者:
oldratlee
🔥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add DisableInheritableForkJoinWorkerThreadFactory and unit test
上级
ef6fec36
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
403 addition
and
82 deletion
+403
-82
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactory.java
...adpool/DisableInheritableForkJoinWorkerThreadFactory.java
+18
-0
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactoryWrapper.java
...DisableInheritableForkJoinWorkerThreadFactoryWrapper.java
+36
-0
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableThreadFactory.java
...ibaba/ttl/threadpool/DisableInheritableThreadFactory.java
+6
-1
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableThreadFactoryWrapper.java
...tl/threadpool/DisableInheritableThreadFactoryWrapper.java
+4
-0
src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java
src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java
+2
-0
src/main/java/com/alibaba/ttl/threadpool/TtlForkJoinPool.java
...main/java/com/alibaba/ttl/threadpool/TtlForkJoinPool.java
+70
-0
src/test/java/com/alibaba/support/junit/conditional/IsAgentRunOrBelowJava7.kt
...ibaba/support/junit/conditional/IsAgentRunOrBelowJava7.kt
+10
-0
src/test/java/com/alibaba/ttl/InheritableTest.kt
src/test/java/com/alibaba/ttl/InheritableTest.kt
+203
-64
src/test/java/com/alibaba/ttl/threadpool/TtlExecutorsTest.kt
src/test/java/com/alibaba/ttl/threadpool/TtlExecutorsTest.kt
+22
-17
src/test/java/com/alibaba/ttl/threadpool/TtlForkJoinPoolTest.kt
...st/java/com/alibaba/ttl/threadpool/TtlForkJoinPoolTest.kt
+32
-0
未找到文件。
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactory.java
0 → 100644
浏览文件 @
9450809a
package
com.alibaba.ttl.threadpool
;
import
javax.annotation.Nonnull
;
import
java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
;
/**
* Disable inheritable {@link ForkJoinWorkerThreadFactory}.
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @since 2.10.1
*/
public
interface
DisableInheritableForkJoinWorkerThreadFactory
extends
ForkJoinWorkerThreadFactory
{
/**
* Unwrap {@link DisableInheritableThreadFactory} to the original/underneath one.
*/
@Nonnull
ForkJoinWorkerThreadFactory
unwrap
();
}
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableForkJoinWorkerThreadFactoryWrapper.java
0 → 100644
浏览文件 @
9450809a
package
com.alibaba.ttl.threadpool
;
import
com.alibaba.ttl.TransmittableThreadLocal
;
import
javax.annotation.Nonnull
;
import
java.util.concurrent.ForkJoinPool
;
import
java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
;
import
java.util.concurrent.ForkJoinWorkerThread
;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
* @since 2.10.1
*/
class
DisableInheritableForkJoinWorkerThreadFactoryWrapper
implements
DisableInheritableForkJoinWorkerThreadFactory
{
final
ForkJoinWorkerThreadFactory
threadFactory
;
public
DisableInheritableForkJoinWorkerThreadFactoryWrapper
(
@Nonnull
ForkJoinWorkerThreadFactory
threadFactory
)
{
this
.
threadFactory
=
threadFactory
;
}
@Override
public
ForkJoinWorkerThread
newThread
(
ForkJoinPool
pool
)
{
final
Object
backup
=
TransmittableThreadLocal
.
Transmitter
.
clear
();
try
{
return
threadFactory
.
newThread
(
pool
);
}
finally
{
TransmittableThreadLocal
.
Transmitter
.
restore
(
backup
);
}
}
@Nonnull
@Override
public
ForkJoinWorkerThreadFactory
unwrap
()
{
return
threadFactory
;
}
}
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableThreadFactory.java
浏览文件 @
9450809a
...
...
@@ -4,11 +4,16 @@ import javax.annotation.Nonnull;
import
java.util.concurrent.ThreadFactory
;
/**
* Disable inheritable
thread factory
.
* Disable inheritable
{@link ThreadFactory}
.
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @see ThreadFactory
* @since 2.10.0
*/
public
interface
DisableInheritableThreadFactory
extends
ThreadFactory
{
/**
* Unwrap {@link DisableInheritableThreadFactory} to the original/underneath one.
*/
@Nonnull
ThreadFactory
unwrap
();
}
src/main/java/com/alibaba/ttl/threadpool/DisableInheritableThreadFactoryWrapper.java
浏览文件 @
9450809a
...
...
@@ -5,6 +5,10 @@ import com.alibaba.ttl.TransmittableThreadLocal;
import
javax.annotation.Nonnull
;
import
java.util.concurrent.ThreadFactory
;
/**
* @author Jerry Lee (oldratlee at gmail dot com)
* @since 2.10.0
*/
class
DisableInheritableThreadFactoryWrapper
implements
DisableInheritableThreadFactory
{
final
ThreadFactory
threadFactory
;
...
...
src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java
浏览文件 @
9450809a
...
...
@@ -25,6 +25,8 @@ import java.util.concurrent.*;
* @see java.util.concurrent.Executors
* @see java.util.concurrent.CompletionService
* @see java.util.concurrent.ExecutorCompletionService
* @see ThreadFactory
* @see Executors#defaultThreadFactory()
* @since 0.9.0
*/
public
final
class
TtlExecutors
{
...
...
src/main/java/com/alibaba/ttl/threadpool/TtlForkJoinPool.java
0 → 100644
浏览文件 @
9450809a
package
com.alibaba.ttl.threadpool
;
import
javax.annotation.Nullable
;
import
java.util.concurrent.ForkJoinPool
;
import
java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
;
/**
* Factory Utils for getting TTL wrapper of {@link ForkJoinWorkerThreadFactory}.
* <p>
* all method is {@code null}-safe, when input parameter(eg: {@link ForkJoinWorkerThreadFactory}) is {@code null}, return {@code null}.
*
* @author Jerry Lee (oldratlee at gmail dot com)
* @see ForkJoinPool
* @see ForkJoinWorkerThreadFactory
* @see ForkJoinPool#defaultForkJoinWorkerThreadFactory
* @since 2.10.1
*/
public
class
TtlForkJoinPool
{
/**
* Wrapper of {@link ForkJoinWorkerThreadFactory}, disable inheritable.
*
* @param threadFactory input thread factory
* @see DisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.1
*/
@Nullable
public
static
ForkJoinWorkerThreadFactory
getDisableInheritableForkJoinWorkerThreadFactory
(
@Nullable
ForkJoinWorkerThreadFactory
threadFactory
)
{
if
(
threadFactory
==
null
||
isDisableInheritableForkJoinWorkerThreadFactory
(
threadFactory
))
return
threadFactory
;
return
new
DisableInheritableForkJoinWorkerThreadFactoryWrapper
(
threadFactory
);
}
/**
* Wrapper of {@link ForkJoinPool#defaultForkJoinWorkerThreadFactory}, disable inheritable.
*
* @see #getDisableInheritableForkJoinWorkerThreadFactory(ForkJoinWorkerThreadFactory)
* @since 2.10.1
*/
@Nullable
public
static
ForkJoinWorkerThreadFactory
getDefaultDisableInheritableForkJoinWorkerThreadFactory
()
{
return
getDisableInheritableForkJoinWorkerThreadFactory
(
ForkJoinPool
.
defaultForkJoinWorkerThreadFactory
);
}
/**
* check the {@link ForkJoinWorkerThreadFactory} is {@link DisableInheritableForkJoinWorkerThreadFactory} or not.
*
* @see DisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.1
*/
public
static
boolean
isDisableInheritableForkJoinWorkerThreadFactory
(
@Nullable
ForkJoinWorkerThreadFactory
threadFactory
)
{
return
threadFactory
instanceof
DisableInheritableForkJoinWorkerThreadFactory
;
}
/**
* Unwrap {@link DisableInheritableForkJoinWorkerThreadFactory} to the original/underneath one.
*
* @see DisableInheritableForkJoinWorkerThreadFactory
* @since 2.10.1
*/
@Nullable
public
static
ForkJoinWorkerThreadFactory
unwrap
(
@Nullable
ForkJoinWorkerThreadFactory
threadFactory
)
{
if
(!
isDisableInheritableForkJoinWorkerThreadFactory
(
threadFactory
))
return
threadFactory
;
return
((
DisableInheritableForkJoinWorkerThreadFactoryWrapper
)
threadFactory
).
unwrap
();
}
private
TtlForkJoinPool
()
{
}
}
src/test/java/com/alibaba/support/junit/conditional/IsAgentRunOrBelowJava7.kt
0 → 100644
浏览文件 @
9450809a
package
com.alibaba.support.junit.conditional
import
com.alibaba.support.junit.conditional.ConditionalIgnoreRule.IgnoreCondition
/**
* @see [Getting Java version at runtime](https://stackoverflow.com/a/23706899/922688)
*/
class
IsAgentRunOrBelowJava7
:
IgnoreCondition
{
override
fun
isSatisfied
():
Boolean
=
IsAgentRun
().
isSatisfied
||
BelowJava7
().
isSatisfied
}
src/test/java/com/alibaba/ttl/InheritableTest.kt
浏览文件 @
9450809a
package
com.alibaba.ttl
import
com.alibaba.support.junit.conditional.BelowJava7
import
com.alibaba.support.junit.conditional.ConditionalIgnoreRule
import
com.alibaba.support.junit.conditional.ConditionalIgnoreRule.ConditionalIgnore
import
com.alibaba.support.junit.conditional.IsAgentRun
import
com.alibaba.support.junit.conditional.IsAgentRunOrBelowJava7
import
com.alibaba.ttl.threadpool.TtlExecutors
import
com.alibaba.ttl.threadpool.TtlForkJoinPool
import
org.junit.Assert.assertEquals
import
org.junit.Assert.assertNull
import
org.junit.Rule
...
...
@@ -11,6 +14,7 @@ import org.junit.Test
import
java.util.*
import
java.util.concurrent.Callable
import
java.util.concurrent.Executors
import
java.util.concurrent.ForkJoinPool
private
const
val
hello
=
"hello"
private
val
defaultValue
=
"${Date()} ${Math.random()}"
...
...
@@ -20,110 +24,245 @@ class InheritableTest {
@JvmField
val
rule
=
ConditionalIgnoreRule
()
// ===================================================
// Executors
// ===================================================
@Test
fun
inheritable
()
{
fun
inheritable
_Executors
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
()
val
ttl
=
TransmittableThreadLocal
<
String
?
>()
ttl
.
set
(
hello
)
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
try
{
val
ttl
=
TransmittableThreadLocal
<
String
?
>()
ttl
.
set
(
hello
)
// get "hello" value is transmitted by InheritableThreadLocal function!
// NOTE: newCachedThreadPool create thread lazily
assertEquals
(
hello
,
threadPool
.
submit
(
callable
).
get
())
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
// get "hello" value is transmitted by InheritableThreadLocal function!
// NOTE: Executors.newCachedThreadPool create thread lazily
assertEquals
(
hello
,
threadPool
.
submit
(
callable
).
get
())
threadPool
.
shutdown
()
// current thread's TTL must be exist
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
disableDisableInheritableThreadFactory
()
{
fun
disable
Inheritable_Executors_
DisableInheritableThreadFactory
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
(
TtlExecutors
.
getDefaultDisableInheritableThreadFactory
())
val
ttl
=
TransmittableThreadLocal
<
String
?
>()
ttl
.
set
(
hello
)
try
{
val
ttl
=
TransmittableThreadLocal
<
String
?
>()
ttl
.
set
(
hello
)
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertNull
(
threadPool
.
submit
(
callable
).
get
())
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
disableInheritable_Executors_TtlDisableInheritableWithInitialValue
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
()
try
{
val
ttl
=
object
:
TransmittableThreadLocal
<
String
?>()
{
override
fun
childValue
(
parentValue
:
String
?):
String
?
=
initialValue
()
}
ttl
.
set
(
hello
)
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertNull
(
threadPool
.
submit
(
callable
).
get
())
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertNull
(
threadPool
.
submit
(
callable
).
get
())
threadPool
.
shutdown
()
// current thread's TTL must be exist
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
disableInheritable_Executors_TtlDefaultValue_TtlDisableInheritableWithInitialValue
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
()
try
{
val
ttl
=
object
:
TransmittableThreadLocal
<
String
>()
{
override
fun
initialValue
():
String
=
defaultValue
override
fun
childValue
(
parentValue
:
String
):
String
=
initialValue
()
}
ttl
.
set
(
hello
)
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertEquals
(
defaultValue
,
threadPool
.
submit
(
callable
).
get
())
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
disable
DisableInheritableThreadFactory_TTL_with_i
nitialValue
()
{
fun
disable
Inheritable_Executors_TtlDefaultValue_DisableInheritableThreadFactory_TtlWithI
nitialValue
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
(
TtlExecutors
.
getDefaultDisableInheritableThreadFactory
())
val
ttl
=
object
:
TransmittableThreadLocal
<
String
>()
{
override
fun
initialValue
():
String
=
defaultValue
override
fun
childValue
(
parentValue
:
String
):
String
=
initialValue
()
try
{
val
ttl
=
object
:
TransmittableThreadLocal
<
String
>()
{
override
fun
initialValue
():
String
=
defaultValue
override
fun
childValue
(
parentValue
:
String
):
String
=
initialValue
()
}
ttl
.
set
(
hello
)
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertEquals
(
defaultValue
,
threadPool
.
submit
(
callable
).
get
())
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
ttl
.
set
(
hello
)
}
// ===================================================
// ForkJoinPool
// ===================================================
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
@Test
@ConditionalIgnore
(
condition
=
BelowJava7
::
class
)
fun
inheritable_ForkJoinPool
()
{
val
threadPool
=
ForkJoinPool
(
4
)
try
{
val
ttl
=
TransmittableThreadLocal
<
String
?
>()
ttl
.
set
(
hello
)
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertEquals
(
defaultValue
,
threadPool
.
submit
(
callable
).
get
())
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
// get "hello" value is transmitted by InheritableThreadLocal function!
// NOTE: Executors.newCachedThreadPool create thread lazily
assertEquals
(
hello
,
threadPool
.
submit
(
callable
).
get
())
threadPool
.
shutdown
()
// current thread's TTL must be exist
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
disableInheritable
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
()
val
ttl
=
object
:
TransmittableThreadLocal
<
String
?>()
{
override
fun
childValue
(
parentValue
:
String
?):
String
?
=
initialValue
()
@ConditionalIgnore
(
condition
=
IsAgentRunOrBelowJava7
::
class
)
fun
disableInheritable_ForkJoinPool_DisableInheritableForkJoinWorkerThreadFactory
()
{
val
threadPool
=
ForkJoinPool
(
4
,
TtlForkJoinPool
.
getDefaultDisableInheritableForkJoinWorkerThreadFactory
(),
null
,
false
)
try
{
val
ttl
=
TransmittableThreadLocal
<
String
?
>()
ttl
.
set
(
hello
)
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertNull
(
threadPool
.
submit
(
callable
).
get
())
// current thread's TTL must be exist when using DisableInheritableForkJoinWorkerThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
ttl
.
set
(
hello
)
}
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
@Test
@ConditionalIgnore
(
condition
=
IsAgentRunOrBelowJava7
::
class
)
fun
disableInheritable_ForkJoinPool_TtlDisableInheritableWithInitialValue
()
{
val
threadPool
=
ForkJoinPool
(
4
)
try
{
val
ttl
=
object
:
TransmittableThreadLocal
<
String
?>()
{
override
fun
childValue
(
parentValue
:
String
?):
String
?
=
initialValue
()
}
ttl
.
set
(
hello
)
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertNull
(
threadPool
.
submit
(
callable
).
get
())
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertNull
(
threadPool
.
submit
(
callable
).
get
())
threadPool
.
shutdown
()
// current thread's TTL must be exist
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
disableInheritable_TTL_with_initialValue
()
{
val
threadPool
=
Executors
.
newCachedThreadPool
()
val
ttl
=
object
:
TransmittableThreadLocal
<
String
>()
{
override
fun
initialValue
():
String
=
defaultValue
override
fun
childValue
(
parentValue
:
String
):
String
=
initialValue
()
@ConditionalIgnore
(
condition
=
IsAgentRunOrBelowJava7
::
class
)
fun
disableInheritable_ForkJoinPool_TtlDefaultValue_TtlDisableInheritableWithInitialValue
()
{
val
threadPool
=
ForkJoinPool
(
4
)
try
{
val
ttl
=
object
:
TransmittableThreadLocal
<
String
>()
{
override
fun
initialValue
():
String
=
defaultValue
override
fun
childValue
(
parentValue
:
String
):
String
=
initialValue
()
}
ttl
.
set
(
hello
)
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertEquals
(
defaultValue
,
threadPool
.
submit
(
callable
).
get
())
// current thread's TTL must be exist
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
ttl
.
set
(
hello
)
}
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
@Test
@ConditionalIgnore
(
condition
=
IsAgentRunOrBelowJava7
::
class
)
fun
disableInheritable_ForkJoinPool_TtlDefaultValue_DisableInheritableForkJoinWorkerThreadFactory_TtlWithInitialValue
()
{
val
threadPool
=
ForkJoinPool
(
4
,
TtlForkJoinPool
.
getDefaultDisableInheritableForkJoinWorkerThreadFactory
(),
null
,
false
)
try
{
val
ttl
=
object
:
TransmittableThreadLocal
<
String
>()
{
override
fun
initialValue
():
String
=
defaultValue
override
fun
childValue
(
parentValue
:
String
):
String
=
initialValue
()
}
ttl
.
set
(
hello
)
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertEquals
(
defaultValue
,
threadPool
.
submit
(
callable
).
get
())
val
callable
=
Callable
{
ttl
.
get
()
}
// NO TtlWrapper(TtlCallable) here!!
// current thread's TTL must be exist when using DisableInheritableThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
// when ttl agent is loaded, Callable is wrapped when submit,
// so here value is "hello" transmitted by TtlCallable wrapper
// IGNORE this test case when TtlAgent is run.
assertEquals
(
defaultValue
,
threadPool
.
submit
(
callable
).
get
())
threadPool
.
shutdown
()
// current thread's TTL must be exist when using DisableInheritableForkJoinWorkerThreadFactory
assertEquals
(
hello
,
ttl
.
get
())
}
finally
{
threadPool
.
shutdown
()
}
}
}
src/test/java/com/alibaba/ttl/threadpool/TtlExecutorsTest.kt
浏览文件 @
9450809a
package
com.alibaba.ttl.threadpool
import
com.alibaba.support.junit.conditional.ConditionalIgnoreRule
import
com.alibaba.support.junit.conditional.ConditionalIgnoreRule.ConditionalIgnore
import
com.alibaba.support.junit.conditional.IsAgentRun
import
com.alibaba.noTtlAgentRun
import
com.alibaba.ttl.threadpool.TtlExecutors.*
import
org.junit.Assert.*
import
org.junit.Rule
import
org.junit.Test
import
java.util.concurrent.Executor
import
java.util.concurrent.Executors.newScheduledThreadPool
import
java.util.concurrent.ThreadFactory
/**
* @author Jerry Lee (oldratlee at gmail dot com)
*/
class
TtlExecutorsTest
{
@Rule
@JvmField
val
rule
=
ConditionalIgnoreRule
()
@Test
@ConditionalIgnore
(
condition
=
IsAgentRun
::
class
)
fun
test_common
()
{
fun
test_common_executors
()
{
val
newScheduledThreadPool
=
newScheduledThreadPool
(
3
)
getTtlExecutor
(
newScheduledThreadPool
).
let
{
assertTrue
(
it
is
ExecutorTtlWrapper
)
assert
True
(
isTtlWrapper
(
it
))
if
(
noTtlAgentRun
())
assertTrue
(
it
is
ExecutorTtlWrapper
)
assert
Equals
(
noTtlAgentRun
(),
isTtlWrapper
(
it
))
assertSame
(
newScheduledThreadPool
,
unwrap
(
it
))
}
getTtlExecutorService
(
newScheduledThreadPool
).
let
{
assertTrue
(
it
is
ExecutorServiceTtlWrapper
)
assert
True
(
isTtlWrapper
(
it
))
if
(
noTtlAgentRun
())
assertTrue
(
it
is
ExecutorServiceTtlWrapper
)
assert
Equals
(
noTtlAgentRun
(),
isTtlWrapper
(
it
))
assertSame
(
newScheduledThreadPool
,
unwrap
(
it
))
}
getTtlScheduledExecutorService
(
newScheduledThreadPool
).
let
{
assertTrue
(
it
is
ScheduledExecutorServiceTtlWrapper
)
assert
True
(
isTtlWrapper
(
it
))
if
(
noTtlAgentRun
())
assertTrue
(
it
is
ScheduledExecutorServiceTtlWrapper
)
assert
Equals
(
noTtlAgentRun
(),
isTtlWrapper
(
it
))
assertSame
(
newScheduledThreadPool
,
unwrap
(
it
))
}
val
threadFactory
=
ThreadFactory
{
Thread
(
it
)
}
getDisableInheritableThreadFactory
(
threadFactory
).
let
{
assertTrue
(
it
is
DisableInheritableThreadFactory
)
assertTrue
(
isDisableInheritableThreadFactory
(
it
))
assertSame
(
threadFactory
,
unwrap
(
it
))
}
}
@Test
fun
test_null
()
{
fun
test_null
_executors
()
{
assertNull
(
getTtlExecutor
(
null
))
assertNull
(
getTtlExecutorService
(
null
))
assertNull
(
getTtlScheduledExecutorService
(
null
))
assertFalse
(
isTtlWrapper
(
null
))
assertNull
(
unwrap
(
null
))
assertNull
(
unwrap
<
Executor
>
(
null
))
}
}
src/test/java/com/alibaba/ttl/threadpool/TtlForkJoinPoolTest.kt
0 → 100644
浏览文件 @
9450809a
package
com.alibaba.ttl.threadpool
import
com.alibaba.support.junit.conditional.BelowJava7
import
com.alibaba.support.junit.conditional.ConditionalIgnoreRule
import
org.junit.Assert.*
import
org.junit.Rule
import
org.junit.Test
import
java.util.concurrent.ForkJoinPool
class
TtlForkJoinPoolTest
{
@Rule
@JvmField
val
rule
=
ConditionalIgnoreRule
()
@Test
@ConditionalIgnoreRule
.
ConditionalIgnore
(
condition
=
BelowJava7
::
class
)
fun
test_common_ForkJoinPool
()
{
TtlForkJoinPool
.
getDefaultDisableInheritableForkJoinWorkerThreadFactory
().
let
{
assertTrue
(
it
is
DisableInheritableForkJoinWorkerThreadFactory
)
assertTrue
(
TtlForkJoinPool
.
isDisableInheritableForkJoinWorkerThreadFactory
(
it
))
assertSame
(
ForkJoinPool
.
defaultForkJoinWorkerThreadFactory
,
TtlForkJoinPool
.
unwrap
(
it
))
}
}
@Test
@ConditionalIgnoreRule
.
ConditionalIgnore
(
condition
=
BelowJava7
::
class
)
fun
test_null_ForkJoinPool
()
{
assertFalse
(
TtlForkJoinPool
.
isDisableInheritableForkJoinWorkerThreadFactory
(
null
))
assertNull
(
TtlForkJoinPool
.
unwrap
(
null
as
?
ForkJoinPool
.
ForkJoinWorkerThreadFactory
))
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录