Skip to content

Commit

Permalink
test: add reproduce test case for issue, test should fail
Browse files Browse the repository at this point in the history
for issue
#133 (comment)
  • Loading branch information
oldratlee committed Mar 19, 2022
1 parent a999ca1 commit 32adad1
Showing 1 changed file with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit

class MyThreadPoolExecutor : ThreadPoolExecutor(10, 20, 2, TimeUnit.SECONDS, LinkedBlockingQueue()) {
class MyThreadPoolExecutor : ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS, LinkedBlockingQueue()) {
val runnableList = CopyOnWriteArrayList<Runnable>()

override fun afterExecute(r: Runnable, t: Throwable?) {
Expand Down Expand Up @@ -55,15 +55,31 @@ class BeforeAndAfterExecuteMethodOfExecutorSubclassTest {
assertTrue(myThreadPoolExecutor.runnableList.all { it is MyRunnable })
}

/**
* for bug submitted by
* https://github.com/alibaba/transmittable-thread-local/issues/133#issuecomment-1068793261
*/
@Test
@ConditionalIgnore(condition = IsAgentRun::class)
fun noAgent() {
@ConditionalIgnore(condition = NoAgentRun::class)
fun underAgent_task_is_explicit_TtlRunnable__should_not_be_unwrapped() {
val myThreadPoolExecutor = MyThreadPoolExecutor()

val ttlExecutorService = myThreadPoolExecutor.let {
it.setKeepAliveTime(10, TimeUnit.SECONDS)
TtlExecutors.getTtlExecutorService(it)
}!!
(0 until 10).map {
val r = TtlRunnable.get(MyRunnable())!!
myThreadPoolExecutor.execute(r)
}

Thread.sleep(100)

assertEquals(20, myThreadPoolExecutor.runnableList.size)
assertTrue(myThreadPoolExecutor.runnableList.all { it is TtlRunnable })
}

@Test
@ConditionalIgnore(condition = IsAgentRun::class)
fun noAgent_task_is_TtlRunnable() {
val myThreadPoolExecutor = MyThreadPoolExecutor()
val ttlExecutorService = TtlExecutors.getTtlExecutorService(myThreadPoolExecutor)!!

(0 until 10).map {
ttlExecutorService.execute(MyRunnable())
Expand All @@ -75,6 +91,21 @@ class BeforeAndAfterExecuteMethodOfExecutorSubclassTest {
assertTrue(myThreadPoolExecutor.runnableList.all { it is TtlRunnable })
}

@Test
@ConditionalIgnore(condition = IsAgentRun::class)
fun noAgent_task_is_NOT_TtlRunnable() {
val myThreadPoolExecutor = MyThreadPoolExecutor()

(0 until 10).map {
myThreadPoolExecutor.execute(MyRunnable())
}

Thread.sleep(100)

assertEquals(20, myThreadPoolExecutor.runnableList.size)
assertTrue(myThreadPoolExecutor.runnableList.all { it is MyRunnable })
}

@Rule
@JvmField
val rule = ConditionalIgnoreRule()
Expand Down

0 comments on commit 32adad1

Please sign in to comment.