-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/test/java/com/alibaba/support/junit/conditional/IsAgentRunOrBelowJava8.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/com/alibaba/ttl/forkjoin/ForkJoinPool4StreamTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |