提交 b7c6038c 编写于 作者: R rybalkinsd 提交者: oldratlee

ttl runnable ext

上级 4c9d909e
package com.alibaba.ttl
/**
* Extension function wrap {@link Runnable} into {@link TtlRunnable}.
* <p>
*
* @param releaseTtlValueReferenceAfterRun release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred.
* @param idempotent is idempotent mode or not. if {@code true}, just return input {@link Runnable} when it's {@link TtlRunnable},
* otherwise throw {@link IllegalStateException}.
* <B><I>Caution</I></B>: {@code true} will cover up bugs! <b>DO NOT</b> set, only when you know why.
* @return Wrapped {@link Runnable}
*
* * @since TODO
*/
fun Runnable.wrap(
releaseTtlValueReferenceAfterRun: Boolean = false,
idempotent: Boolean = false
): TtlRunnable = TtlRunnable.get(this, releaseTtlValueReferenceAfterRun, idempotent)!!
/**
* Extension function wrap input {@link Runnable} Collection to {@link TtlRunnable} Collection.
*
* @param releaseTtlValueReferenceAfterRun release TTL value reference after run, avoid memory leak even if {@link TtlRunnable} is referred.
* @param idempotent is idempotent mode or not. if {@code true}, just return input {@link Runnable} when it's {@link TtlRunnable},
* otherwise throw {@link IllegalStateException}.
* <B><I>Caution</I></B>: {@code true} will cover up bugs! <b>DO NOT</b> set, only when you know why.
* @return Wrapped list of {@link Runnable}
*
* @see #Runnable::wrap
* @since TODO
*/
fun List<Runnable>.wrap(
releaseTtlValueReferenceAfterRun: Boolean = false,
idempotent: Boolean = false
): List<TtlRunnable> = map { it.wrap(releaseTtlValueReferenceAfterRun, idempotent) }
/**
* Extension function to unwrap {@link TtlRunnable} to the original/underneath one.
* <p>
* if input {@code Runnable} parameter is not a {@link TtlRunnable} just return input {@code Runnable}.
* <p>
* so {@code Runnable.wrap().unwrap()} will always return the same input {@code Runnable} object.
*
* @since TODO
*/
fun Runnable.unwrap(): Runnable = when (this) {
is TtlRunnable -> runnable
else -> this
}
/**
* Extension function to unwrap {@link TtlRunnable} to the original/underneath one.
* <p>
* Invoke {@link #unwrap(Runnable)} for each element in collection.
* <p>
*
* @see #Runnable::unwrap
* @since TODO
*/
fun List<Runnable>.unwrap(): List<Runnable> = map { it.unwrap() }
......@@ -5,16 +5,22 @@ import com.alibaba.assertParentTtlValues
import com.alibaba.copyTtlValues
import com.alibaba.createParentTtlInstances
import com.alibaba.createParentTtlInstancesAfterCreateChild
import com.alibaba.support.junit.conditional.ConditionalIgnoreRule
import com.alibaba.ttl.testmodel.Call
import org.hamcrest.CoreMatchers
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertThat
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
class TtlCallableExtKtTest {
@Rule
@JvmField
val rule = ConditionalIgnoreRule()
@Test
fun `callable wrap extension function `() {
val call = Call("1")
......
package com.alibaba.ttl
import com.alibaba.support.junit.conditional.ConditionalIgnoreRule
import com.alibaba.ttl.testmodel.Task
import org.hamcrest.CoreMatchers
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
class TtlRunnableExtKtTest {
@Rule
@JvmField
val rule = ConditionalIgnoreRule()
@Test
fun `runnable wrap extension function `() {
val task = Task("1")
val ttlRunnable = task.wrap()
Assert.assertSame(task, ttlRunnable.runnable)
}
@Test
fun `runnable wrap extension function multiple times`() {
val task = Task("1").wrap()
try {
task.wrap()
Assert.fail()
} catch (e: IllegalStateException) {
Assert.assertThat<String>(e.message, CoreMatchers.containsString("Already TtlRunnable"))
}
}
@Test
fun `list of runnable wrap extension function`() {
val taskList = listOf(Task("1"), Task("2"), Task("3")).wrap()
Assert.assertEquals(3, taskList.size)
taskList.forEach {
Assert.assertThat(it, CoreMatchers.instanceOf(TtlRunnable::class.java))
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册