-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from embrace-io/thread-local
Create property delegate function for ThreadLocal
- Loading branch information
Showing
4 changed files
with
68 additions
and
15 deletions.
There are no files selected for viewing
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
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
25 changes: 25 additions & 0 deletions
25
...d-sdk/src/main/java/io/embrace/android/embracesdk/internal/utils/ThreadLocalExtensions.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,25 @@ | ||
package io.embrace.android.embracesdk.internal.utils | ||
|
||
import kotlin.properties.ReadOnlyProperty | ||
import kotlin.reflect.KProperty | ||
|
||
/** | ||
* Syntactic sugar that makes it easier to define a property in Kotlin whose value is backed by | ||
* a ThreadLocal. | ||
*/ | ||
internal inline fun <reified T> threadLocal( | ||
noinline provider: () -> T | ||
): ReadOnlyProperty<Any?, T> = ThreadLocalDelegate(provider) | ||
|
||
internal class ThreadLocalDelegate<T>( | ||
provider: () -> T | ||
) : ReadOnlyProperty<Any?, T> { | ||
|
||
private val threadLocal: ThreadLocal<T> = object : ThreadLocal<T>() { | ||
override fun initialValue(): T = provider() | ||
} | ||
|
||
override fun getValue(thisRef: Any?, property: KProperty<*>): T { | ||
return checkNotNull(threadLocal.get()) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...k/src/test/java/io/embrace/android/embracesdk/internal/utils/ThreadLocalExtensionsTest.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,29 @@ | ||
package io.embrace.android.embracesdk.internal.utils | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotEquals | ||
import org.junit.Test | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal class ThreadLocalExtensionsTest { | ||
|
||
private val id: Long by threadLocal { | ||
Thread.currentThread().id | ||
} | ||
|
||
@Test | ||
fun testThreadLocalProperties() { | ||
val testThreadId = Thread.currentThread().id | ||
assertEquals(id, testThreadId) | ||
|
||
val latch = CountDownLatch(1) | ||
Executors.newSingleThreadExecutor().submit { | ||
assertEquals(id, Thread.currentThread().id) | ||
assertNotEquals(id, testThreadId) | ||
latch.countDown() | ||
} | ||
latch.await(1, TimeUnit.SECONDS) | ||
} | ||
} |