Skip to content

Commit

Permalink
add ForkJoinPool4StreamTest #77
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Nov 12, 2018
1 parent 95f6f70 commit 159ae5b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
71 changes: 71 additions & 0 deletions src/test/java/com/alibaba/ttl/forkjoin/ForkJoinPool4StreamTest.kt
Original file line number Diff line number Diff line change
@@ -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<String?>()
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<String?>()
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)
}
}
}

0 comments on commit 159ae5b

Please sign in to comment.