Skip to content

Commit

Permalink
Bikeshed API and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Jun 5, 2024
1 parent 535fc8a commit d854799
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions core/jvm/src/main/scala/cats/effect/IOLocalPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import IOFiberConstants.ioLocalPropagation
private[effect] trait IOLocalPlatform[A] { self: IOLocal[A] =>

/**
* Returns a [[java.lang.ThreadLocal]] that allows to unsafely get, set, and remove (aka
* reset) the value. The system property `cats.effect.ioLocalPropagation` must be `true`,
* otherwise throws an [[java.lang.UnsupportedOperationException]].
* Returns a [[java.lang.ThreadLocal]] view of this [[IOLocal]] that allows to unsafely get,
* set, and remove (aka reset) the value in the currently running fiber. The system property
* `cats.effect.ioLocalPropagation` must be `true`, otherwise throws an
* [[java.lang.UnsupportedOperationException]].
*/
def unsafeToThreadLocal(): ThreadLocal[A] = if (ioLocalPropagation)
def unsafeThreadLocal(): ThreadLocal[A] = if (ioLocalPropagation)
new ThreadLocal[A] {
override def get(): A = {
val fiber = IOFiber.currentIOFiber()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ class IOLocalsSpec extends BaseSpec {
"IOLocals" should {
"return a default value" in real {
IOLocal(42)
.flatMap(local => IO(local.unsafeToThreadLocal().get()))
.flatMap(local => IO(local.unsafeThreadLocal().get()))
.map(_ must beEqualTo(42))
}

"return a set value" in real {
for {
local <- IOLocal(42)
threadLocal <- IO(local.unsafeToThreadLocal())
threadLocal <- IO(local.unsafeThreadLocal())
_ <- local.set(24)
got <- IO(threadLocal.get())
} yield got must beEqualTo(24)
}

"unsafely set" in real {
IOLocal(42).flatMap(local =>
IO(local.unsafeToThreadLocal().set(24)) *> local.get.map(_ must beEqualTo(24)))
IO(local.unsafeThreadLocal().set(24)) *> local.get.map(_ must beEqualTo(24)))
}

"unsafely reset" in real {
for {
local <- IOLocal(42)
threadLocal <- IO(local.unsafeToThreadLocal())
threadLocal <- IO(local.unsafeThreadLocal())
_ <- local.set(24)
_ <- IO(threadLocal.remove())
got <- local.get
Expand Down

0 comments on commit d854799

Please sign in to comment.