diff --git a/src/test/java/com/alibaba/support/junit/conditional/IsAgentRunOrBelowJava8.kt b/src/test/java/com/alibaba/support/junit/conditional/IsAgentRunOrBelowJava8.kt new file mode 100644 index 000000000..e00e646b2 --- /dev/null +++ b/src/test/java/com/alibaba/support/junit/conditional/IsAgentRunOrBelowJava8.kt @@ -0,0 +1,10 @@ +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 IsAgentRunOrBelowJava8 : IgnoreCondition { + override fun isSatisfied(): Boolean = IsAgentRun().isSatisfied || BelowJava8().isSatisfied +} diff --git a/src/test/java/com/alibaba/support/junit/conditional/NoAgentRun.kt b/src/test/java/com/alibaba/support/junit/conditional/NoAgentRunOrBelowJava8.kt similarity index 65% rename from src/test/java/com/alibaba/support/junit/conditional/NoAgentRun.kt rename to src/test/java/com/alibaba/support/junit/conditional/NoAgentRunOrBelowJava8.kt index 95db3e74e..f5fd0287a 100644 --- a/src/test/java/com/alibaba/support/junit/conditional/NoAgentRun.kt +++ b/src/test/java/com/alibaba/support/junit/conditional/NoAgentRunOrBelowJava8.kt @@ -6,6 +6,6 @@ import com.alibaba.support.junit.conditional.ConditionalIgnoreRule.IgnoreConditi /** * @see [Getting Java version at runtime](https://stackoverflow.com/a/23706899/922688) */ -class NoAgentRun : IgnoreCondition { - override fun isSatisfied(): Boolean = noTtlAgentRun() +class NoAgentRunOrBelowJava8 : IgnoreCondition { + override fun isSatisfied(): Boolean = noTtlAgentRun() || BelowJava8().isSatisfied } diff --git a/src/test/java/com/alibaba/ttl/forkjoin/ForkJoinPool4StreamTest.kt b/src/test/java/com/alibaba/ttl/forkjoin/ForkJoinPool4StreamTest.kt new file mode 100644 index 000000000..460649a21 --- /dev/null +++ b/src/test/java/com/alibaba/ttl/forkjoin/ForkJoinPool4StreamTest.kt @@ -0,0 +1,71 @@ +package com.alibaba.ttl.forkjoin + +import com.alibaba.expandThreadPool +import com.alibaba.support.junit.conditional.ConditionalIgnoreRule +import com.alibaba.support.junit.conditional.ConditionalIgnoreRule.ConditionalIgnore +import com.alibaba.support.junit.conditional.IsAgentRunOrBelowJava8 +import com.alibaba.support.junit.conditional.NoAgentRunOrBelowJava8 +import com.alibaba.ttl.TransmittableThreadLocal +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Rule +import org.junit.Test +import java.util.concurrent.ForkJoinPool + + +private const val hello = "hello" + +class ForkJoinPool4StreamTest { + @Rule + @JvmField + val rule = ConditionalIgnoreRule() + + @Test + @ConditionalIgnore(condition = NoAgentRunOrBelowJava8::class) + fun test_stream_with_agent() { + expandThreadPool(ForkJoinPool.commonPool()) + + val ttl = TransmittableThreadLocal() + ttl.set(hello) + + (0..100).map { + ForkJoinPool.commonPool().submit { + assertEquals(hello, ttl.get()) + } + }.forEach { it.get() } + + (0..1000).toList().stream().parallel().mapToInt { + assertEquals(hello, ttl.get()) + + it + }.sum().let { + assertEquals((0..1000).sum(), it) + } + } + + @Test + @ConditionalIgnore(condition = IsAgentRunOrBelowJava8::class) + fun test_stream_no_agent() { + val name = Thread.currentThread().name + expandThreadPool(ForkJoinPool.commonPool()) + + val ttl = TransmittableThreadLocal() + ttl.set(hello) + + (0..100).map { + ForkJoinPool.commonPool().submit { + if (Thread.currentThread().name == name) assertEquals(hello, ttl.get()) + else assertNull(ttl.get()) + } + }.forEach { it.get() } + + (0..1000).toList().stream().parallel().mapToInt { + if (Thread.currentThread().name == name) assertEquals(hello, ttl.get()) + else assertNull(ttl.get()) + + it + }.sum().let { + assertEquals((0..1000).sum(), it) + } + } +}