Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿啄debugIT
transmittable-thread-local
提交
15e1d082
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,发现更多精彩内容 >>
提交
15e1d082
编写于
12月 11, 2018
作者:
R
rybalkinsd
提交者:
oldratlee
1月 08, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1. clear naming for wrapTtl/unwrapTtl
2. TtlDelegate backing property unification
上级
99d5bc21
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
73 addition
and
43 deletion
+73
-43
src/main/java/com/alibaba/ttl/TransmittableThreadLocalExt.kt
src/main/java/com/alibaba/ttl/TransmittableThreadLocalExt.kt
+10
-5
src/main/java/com/alibaba/ttl/TtlCallableExt.kt
src/main/java/com/alibaba/ttl/TtlCallableExt.kt
+16
-10
src/main/java/com/alibaba/ttl/TtlRunnableExt.kt
src/main/java/com/alibaba/ttl/TtlRunnableExt.kt
+9
-9
src/test/java/com/alibaba/ttl/TransmittableThreadLocalExtKtTest.kt
...java/com/alibaba/ttl/TransmittableThreadLocalExtKtTest.kt
+23
-4
src/test/java/com/alibaba/ttl/TtlCallableExtKtTest.kt
src/test/java/com/alibaba/ttl/TtlCallableExtKtTest.kt
+8
-8
src/test/java/com/alibaba/ttl/TtlRunnableExtKtTest.kt
src/test/java/com/alibaba/ttl/TtlRunnableExtKtTest.kt
+7
-7
未找到文件。
src/main/java/com/alibaba/ttl/TransmittableThreadLocalExt.kt
浏览文件 @
15e1d082
...
...
@@ -3,19 +3,24 @@ package com.alibaba.ttl
import
kotlin.reflect.KProperty
/**
* A shortcut method to instantiate [TransmittableThreadLocal].
*
* @since 2.11.0
*/
fun
<
T
>
ttl
()
=
TransmittableThreadLocal
<
T
?
>()
/**
* Kotlin Delegate for immutable/mutable values or properties.
*
* @param backingProperty is [InheritableThreadLocal] if provided
* or [TransmittableThreadLocal] by default
*
* @since 2.11.0
*/
class
TtlDelegate
<
T
>
{
private
var
_value
=
ttl
<
T
>()
operator
fun
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>):
T
?
=
_value
.
get
()
class
TtlDelegate
<
T
>(
private
val
backingProperty
:
InheritableThreadLocal
<
T
?
>
=
ttl
())
{
operator
fun
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>):
T
?
=
backingProperty
.
get
()
operator
fun
setValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>,
value
:
T
?)
{
_value
.
set
(
value
)
backingProperty
.
set
(
value
)
}
}
src/main/java/com/alibaba/ttl/TtlCallableExt.kt
浏览文件 @
15e1d082
...
...
@@ -2,6 +2,12 @@ package com.alibaba.ttl
import
java.util.concurrent.Callable
/**
* Invoke operator for [TtlCallable].
*
* @since 2.11.0
*/
@Throws
(
Exception
::
class
)
operator
fun
<
V
>
TtlCallable
<
V
>.
invoke
():
V
=
call
()
...
...
@@ -15,7 +21,7 @@ operator fun <V> TtlCallable<V>.invoke(): V = call()
*
* @since 2.11.0
*/
fun
<
V
>
Callable
<
V
>.
wrap
(
fun
<
V
>
Callable
<
V
>.
wrap
Ttl
(
releaseTtlValueReferenceAfterCall
:
Boolean
=
false
,
idempotent
:
Boolean
=
false
):
TtlCallable
<
V
>
=
TtlCallable
.
get
(
this
,
releaseTtlValueReferenceAfterCall
,
idempotent
)
!!
...
...
@@ -27,24 +33,24 @@ fun <V> Callable<V>.wrap(
* @param idempotent is idempotent or not. `true` will cover up bugs! **DO NOT** set, only when you know why.
* @return Wrapped list of [Callable]
*
* @see
#Callable::wrap
* @see
Callable.wrapTtl
* @since 2.11.0
*/
fun
<
V
>
List
<
Callable
<
V
>>.
wrap
(
fun
<
V
>
List
<
Callable
<
V
>>.
wrap
Ttl
(
releaseTtlValueReferenceAfterCall
:
Boolean
=
false
,
idempotent
:
Boolean
=
false
):
List
<
TtlCallable
<
V
>>
=
map
{
it
.
wrap
(
releaseTtlValueReferenceAfterCall
,
idempotent
)
}
):
List
<
TtlCallable
<
V
>>
=
map
{
it
.
wrap
Ttl
(
releaseTtlValueReferenceAfterCall
,
idempotent
)
}
/**
* Extension function to unwrap [TtlCallable] to the original/underneath one.
* <p>
* if input
`Callable` parameter is not a [TtlCallable] just return input `Callable`
.
* if input
[Callable] parameter is not a [TtlCallable] just return input [Callable]
.
* <p>
* so `callable.wrap
().unwrap()` will always return the same input `callable`
object.
* so `callable.wrap
Ttl().unwrapTtl()` will always return the same input [Callable]
object.
*
* @since 2.11.0
*/
fun
<
V
>
Callable
<
V
>.
unwrap
():
Callable
<
V
>
=
when
(
this
)
{
fun
<
V
>
Callable
<
V
>.
unwrap
Ttl
():
Callable
<
V
>
=
when
(
this
)
{
is
TtlCallable
<
V
>
->
getCallable
()
else
->
this
}
...
...
@@ -52,10 +58,10 @@ fun <V> Callable<V>.unwrap(): Callable<V> = when(this) {
/**
* Extension function to unwrap [TtlCallable] to the original/underneath one.
* <p>
* Invoke [unwrap] for each element in collection.
* Invoke [unwrap
Ttl
] for each element in collection.
* <p>
*
* @see
#Callable::unwrap
* @see
Callable.unwrapTtl
* @since 2.11.0
*/
fun
<
V
>
List
<
Callable
<
V
>>.
unwrap
()
:
List
<
Callable
<
V
>>
=
map
{
it
.
unwrap
()
}
fun
<
V
>
List
<
Callable
<
V
>>.
unwrap
Ttl
()
:
List
<
Callable
<
V
>>
=
map
{
it
.
unwrapTtl
()
}
src/main/java/com/alibaba/ttl/TtlRunnableExt.kt
浏览文件 @
15e1d082
...
...
@@ -11,7 +11,7 @@ package com.alibaba.ttl
*
* @since 2.11.0
*/
fun
Runnable
.
wrap
(
fun
Runnable
.
wrap
Ttl
(
releaseTtlValueReferenceAfterRun
:
Boolean
=
false
,
idempotent
:
Boolean
=
false
):
TtlRunnable
=
TtlRunnable
.
get
(
this
,
releaseTtlValueReferenceAfterRun
,
idempotent
)
!!
...
...
@@ -25,24 +25,24 @@ fun Runnable.wrap(
* **_Caution_**: `true` will cover up bugs! **DO NOT** set, only when you know why.
* @return Wrapped list of [Runnable]
*
* @see Runnable.wrap
* @see Runnable.wrap
Ttl
* @since 2.11.0
*/
fun
List
<
Runnable
>.
wrap
(
fun
List
<
Runnable
>.
wrap
Ttl
(
releaseTtlValueReferenceAfterRun
:
Boolean
=
false
,
idempotent
:
Boolean
=
false
):
List
<
TtlRunnable
>
=
map
{
it
.
wrap
(
releaseTtlValueReferenceAfterRun
,
idempotent
)
}
):
List
<
TtlRunnable
>
=
map
{
it
.
wrap
Ttl
(
releaseTtlValueReferenceAfterRun
,
idempotent
)
}
/**
* Extension function to unwrap [TtlRunnable] to the original/underneath one.
* <p>
* if input `Runnable` parameter is not a [TtlRunnable] just return input `Runnable`.
* <p>
* so `Runnable.wrap
().unwrap
()` will always return the same input `Runnable` object.
* so `Runnable.wrap
Ttl().unwrapTtl
()` will always return the same input `Runnable` object.
*
* @since 2.11.0
*/
fun
Runnable
.
unwrap
():
Runnable
=
when
(
this
)
{
fun
Runnable
.
unwrap
Ttl
():
Runnable
=
when
(
this
)
{
is
TtlRunnable
->
runnable
else
->
this
}
...
...
@@ -50,10 +50,10 @@ fun Runnable.unwrap(): Runnable = when (this) {
/**
* Extension function to unwrap [TtlRunnable] to the original/underneath one.
* <p>
* Invoke [Runnable.unwrap] for each element in collection.
* Invoke [Runnable.unwrap
Ttl
] for each element in collection.
* <p>
*
* @see Runnable.unwrap
* @see Runnable.unwrap
Ttl
* @since 2.11.0
*/
fun
List
<
Runnable
>.
unwrap
():
List
<
Runnable
>
=
map
{
it
.
unwrap
()
}
fun
List
<
Runnable
>.
unwrap
Ttl
():
List
<
Runnable
>
=
map
{
it
.
unwrapTtl
()
}
src/test/java/com/alibaba/ttl/TransmittableThreadLocalExtKtTest.kt
浏览文件 @
15e1d082
...
...
@@ -2,30 +2,49 @@ package com.alibaba.ttl
import
junit.framework.TestCase.assertEquals
import
junit.framework.TestCase.assertNotNull
import
junit.framework.TestCase.assertNull
import
org.junit.Test
import
kotlin.concurrent.thread
class
TransmittableThreadLocalExtKtTest
{
@Test
fun
`ttl
delegate`
()
{
fun
`
simple
ttl
delegate`
()
{
var
parent
by
TtlDelegate
<
String
>()
parent
=
"value-set-in-parent"
var
valueFromParent
:
String
?
=
null
thread
{
val
valueFromParent
=
parent
assertEquals
(
"value-set-in-parent"
,
valueFromParent
)
valueFromParent
=
parent
}.
join
()
assertEquals
(
"value-set-in-parent"
,
valueFromParent
)
}
@Test
fun
`ttl
nullable
delegate`
()
{
val
parent
by
TtlDelegate
<
Int
>()
var
result
:
Int
?
=
null
thread
{
result
=
parent
}.
join
()
assertNull
(
result
)
}
@Test
fun
`ttl
inherited
delegate`
()
{
val
inheritedTtl
=
object
:
InheritableThreadLocal
<
Int
?>()
{
}
var
parent
by
TtlDelegate
(
inheritedTtl
)
parent
=
42
var
result
:
Int
?
=
null
thread
{
assertNotNull
(
"value-set-in-parent"
,
parent
)
result
=
parent
}.
join
()
assertNotNull
(
result
)
}
}
src/test/java/com/alibaba/ttl/TtlCallableExtKtTest.kt
浏览文件 @
15e1d082
...
...
@@ -22,17 +22,17 @@ class TtlCallableExtKtTest {
val
rule
=
ConditionalIgnoreRule
()
@Test
fun
`callable
wrap
extension
function
`
()
{
fun
`callable
wrap
Ttl
extension
function
`
()
{
val
call
=
Call
(
"1"
)
val
ttlCallable
=
call
.
wrap
()
val
ttlCallable
=
call
.
wrap
Ttl
()
assertSame
(
call
,
ttlCallable
.
callable
)
}
@Test
fun
`callable
wrap
extension
function
multiple
times`
()
{
val
call
=
Call
(
"1"
).
wrap
()
fun
`callable
wrap
Ttl
extension
function
multiple
times`
()
{
val
call
=
Call
(
"1"
).
wrap
Ttl
()
try
{
call
.
wrap
()
call
.
wrap
Ttl
()
fail
()
}
catch
(
e
:
IllegalStateException
)
{
assertThat
<
String
>(
e
.
message
,
CoreMatchers
.
containsString
(
"Already TtlCallable"
))
...
...
@@ -41,8 +41,8 @@ class TtlCallableExtKtTest {
}
@Test
fun
`list
of
callable
wrap
extension
function`
()
{
val
callList
=
listOf
(
Call
(
"1"
),
Call
(
"2"
),
Call
(
"3"
)).
wrap
()
fun
`list
of
callable
wrap
Ttl
extension
function`
()
{
val
callList
=
listOf
(
Call
(
"1"
),
Call
(
"2"
),
Call
(
"3"
)).
wrap
Ttl
()
assertEquals
(
3
,
callList
.
size
)
callList
.
forEach
{
...
...
@@ -57,7 +57,7 @@ class TtlCallableExtKtTest {
val
call
=
Call
(
"1"
,
ttlInstances
)
val
ttlCallable
=
call
.
wrap
()
val
ttlCallable
=
call
.
wrap
Ttl
()
// create after new Task, won't see parent value in in task!
createParentTtlInstancesAfterCreateChild
(
ttlInstances
)
...
...
src/test/java/com/alibaba/ttl/TtlRunnableExtKtTest.kt
浏览文件 @
15e1d082
...
...
@@ -14,17 +14,17 @@ class TtlRunnableExtKtTest {
val
rule
=
ConditionalIgnoreRule
()
@Test
fun
`runnable
wrap
extension
function
`
()
{
fun
`runnable
wrap
Ttl
extension
function
`
()
{
val
task
=
Task
(
"1"
)
val
ttlRunnable
=
task
.
wrap
()
val
ttlRunnable
=
task
.
wrap
Ttl
()
Assert
.
assertSame
(
task
,
ttlRunnable
.
runnable
)
}
@Test
fun
`runnable
wrap
extension
function
multiple
times`
()
{
val
task
=
Task
(
"1"
).
wrap
()
fun
`runnable
wrap
Ttl
extension
function
multiple
times`
()
{
val
task
=
Task
(
"1"
).
wrap
Ttl
()
try
{
task
.
wrap
()
task
.
wrap
Ttl
()
Assert
.
fail
()
}
catch
(
e
:
IllegalStateException
)
{
Assert
.
assertThat
<
String
>(
e
.
message
,
CoreMatchers
.
containsString
(
"Already TtlRunnable"
))
...
...
@@ -33,8 +33,8 @@ class TtlRunnableExtKtTest {
}
@Test
fun
`list
of
runnable
wrap
extension
function`
()
{
val
taskList
=
listOf
(
Task
(
"1"
),
Task
(
"2"
),
Task
(
"3"
)).
wrap
()
fun
`list
of
runnable
wrap
Ttl
extension
function`
()
{
val
taskList
=
listOf
(
Task
(
"1"
),
Task
(
"2"
),
Task
(
"3"
)).
wrap
Ttl
()
Assert
.
assertEquals
(
3
,
taskList
.
size
)
taskList
.
forEach
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录