|
| 1 | +package com.alibaba.demo.coroutine.ttl_intergration.usage |
| 2 | + |
| 3 | +import com.alibaba.demo.coroutine.ttl_intergration.ttlContext |
| 4 | +import com.alibaba.ttl.TransmittableThreadLocal |
| 5 | +import kotlinx.coroutines.Dispatchers |
| 6 | +import kotlinx.coroutines.delay |
| 7 | +import kotlinx.coroutines.launch |
| 8 | +import kotlinx.coroutines.runBlocking |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Assert.assertNotEquals |
| 11 | +import org.junit.Test |
| 12 | + |
| 13 | +class TtlCoroutineContextTest { |
| 14 | + @Test |
| 15 | + fun threadContextElement_passByValue(): Unit = runBlocking { |
| 16 | + val mainValue = "main-${System.currentTimeMillis()}" |
| 17 | + val testThread = Thread.currentThread() |
| 18 | + |
| 19 | + // String ThreadLocal, String is immutable value, can only be passed by value |
| 20 | + val threadLocal = TransmittableThreadLocal<String?>() |
| 21 | + threadLocal.set(mainValue) |
| 22 | + println("test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 23 | + |
| 24 | + val job = launch(Dispatchers.Default + ttlContext()) { |
| 25 | + println("Launch start, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 26 | + assertEquals(mainValue, threadLocal.get()) |
| 27 | + assertNotEquals(testThread, Thread.currentThread()) |
| 28 | + |
| 29 | + delay(5) |
| 30 | + |
| 31 | + println("After delay, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 32 | + assertEquals(mainValue, threadLocal.get()) |
| 33 | + assertNotEquals(testThread, Thread.currentThread()) |
| 34 | + |
| 35 | + val reset = "job-reset-${threadLocal.get()}" |
| 36 | + threadLocal.set(reset) |
| 37 | + assertEquals(reset, threadLocal.get()) |
| 38 | + |
| 39 | + delay(5) |
| 40 | + |
| 41 | + println("After delay set reset, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 42 | + assertEquals(reset, threadLocal.get()) |
| 43 | + assertNotEquals(testThread, Thread.currentThread()) |
| 44 | + } |
| 45 | + job.join() |
| 46 | + |
| 47 | + println("after launch, test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 48 | + assertEquals(mainValue, threadLocal.get()) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun threadContextElement_passByReference(): Unit = runBlocking { |
| 53 | + data class Reference(var data: Int = 42) |
| 54 | + |
| 55 | + val mainValue = Reference() |
| 56 | + val testThread = Thread.currentThread() |
| 57 | + |
| 58 | + // Reference ThreadLocal, mutable value, pass by reference |
| 59 | + val threadLocal = TransmittableThreadLocal<Reference>() // declare thread-local variable |
| 60 | + threadLocal.set(mainValue) |
| 61 | + println("test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 62 | + |
| 63 | + val job = launch(Dispatchers.Default + ttlContext()) { |
| 64 | + println("Launch start, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 65 | + assertEquals(mainValue, threadLocal.get()) |
| 66 | + assertNotEquals(testThread, Thread.currentThread()) |
| 67 | + |
| 68 | + delay(5) |
| 69 | + |
| 70 | + println("After delay, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 71 | + assertEquals(mainValue, threadLocal.get()) |
| 72 | + assertNotEquals(testThread, Thread.currentThread()) |
| 73 | + |
| 74 | + val reset = -42 |
| 75 | + threadLocal.get().data = reset |
| 76 | + |
| 77 | + delay(5) |
| 78 | + |
| 79 | + println("After delay set reset, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 80 | + assertEquals(Reference(reset), threadLocal.get()) |
| 81 | + assertNotEquals(testThread, Thread.currentThread()) |
| 82 | + } |
| 83 | + job.join() |
| 84 | + |
| 85 | + println("after launch, test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()}") |
| 86 | + assertEquals(mainValue, threadLocal.get()) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun twoThreadContextElement(): Unit = runBlocking { |
| 91 | + val mainValue = "main-a-${System.currentTimeMillis()}" |
| 92 | + val anotherMainValue = "main-another-${System.currentTimeMillis()}" |
| 93 | + val testThread = Thread.currentThread() |
| 94 | + |
| 95 | + val threadLocal = TransmittableThreadLocal<String?>() // declare thread-local variable |
| 96 | + val anotherThreadLocal = TransmittableThreadLocal<String?>() // declare thread-local variable |
| 97 | + |
| 98 | + threadLocal.set(mainValue) |
| 99 | + anotherThreadLocal.set(anotherMainValue) |
| 100 | + println("test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()} | ${anotherThreadLocal.get()}") |
| 101 | + |
| 102 | + println() |
| 103 | + launch(Dispatchers.Default + ttlContext()) { |
| 104 | + println("Launch start, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()} | ${anotherThreadLocal.get()}") |
| 105 | + assertEquals(mainValue, threadLocal.get()) |
| 106 | + assertEquals(anotherMainValue, anotherThreadLocal.get()) |
| 107 | + assertNotEquals(testThread, Thread.currentThread()) |
| 108 | + |
| 109 | + delay(5) |
| 110 | + |
| 111 | + println("After delay, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()} | ${anotherThreadLocal.get()}") |
| 112 | + assertEquals(mainValue, threadLocal.get()) |
| 113 | + assertEquals(anotherMainValue, anotherThreadLocal.get()) |
| 114 | + assertNotEquals(testThread, Thread.currentThread()) |
| 115 | + |
| 116 | + val resetA = "job-reset-${threadLocal.get()}" |
| 117 | + threadLocal.set(resetA) |
| 118 | + val resetAnother = "job-reset-${anotherThreadLocal.get()}" |
| 119 | + anotherThreadLocal.set(resetAnother) |
| 120 | + println("Before delay set reset, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()} | ${anotherThreadLocal.get()}") |
| 121 | + |
| 122 | + delay(5) |
| 123 | + |
| 124 | + println("After delay set reset, current thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()} | ${anotherThreadLocal.get()}") |
| 125 | + assertEquals(resetA, threadLocal.get()) |
| 126 | + assertEquals(resetAnother, anotherThreadLocal.get()) |
| 127 | + assertNotEquals(testThread, Thread.currentThread()) |
| 128 | + }.join() |
| 129 | + |
| 130 | + println("after launch2, test thread: ${Thread.currentThread()}, thread local value: ${threadLocal.get()} | ${anotherThreadLocal.get()}") |
| 131 | + assertEquals(mainValue, threadLocal.get()) |
| 132 | + assertEquals(anotherMainValue, anotherThreadLocal.get()) |
| 133 | + } |
| 134 | +} |
0 commit comments