Comments (2)
There are multiple issues with your code:
- The workflow that uses the
Clock
service isask
but you provide theLiveClock
to practically every other workflow in your test but not that one. You can useTestAspect.withLiveClock
to run your entire test with the live clock orLive.live
orLive.withLive
to run a particular part with the live clock. But if you want to use the more granular operators you need to actually use them on the part of your test that you want to use the live clock. - You are forking an uninterruptible fiber that never completes and then attempting to interrupt it which never completes. You fork a fiber in the
acquire
action ofZIO.acquireRelease
. Theacquire
action is performed uninterruptibly and fibers inherit the interruptibility of their parents so the forked fiber is interruptible. You then attempt to interrupt that fiber in therelease
action but that can never complete because the fiber is uninterruptible. You can use theinterruptible
operator on the workflow you are forking to designate that it should be interruptible even though it is forked in an uninterruptible region.
See a corrected example below. We have numerous forums such as Discord where users can get help. Please use Github issues for things that are not working or feature requests, not questions about how to do something.
import zio._
import zio.stream._
import zio.test._
import zio.test.TestAspect._
import java.util.concurrent.atomic.AtomicInteger
object EchoService {
val nextId = new AtomicInteger(0)
val manager = ZLayer.fromZIO(
for {
in <- Queue.bounded[String](10)
out <- Queue.bounded[(Int, String)](10)
} yield Mgr(in, out)
)
val forked = ZLayer.fromZIO(
ZIO
.acquireRelease(
for {
mgr <- ZIO.service[Mgr]
f <- mgr.fork
} yield f
)(_.interrupt)
)
def ask(s: String) =
ZIO.service[Mgr].flatMap(_.ask(s))
case class Mgr(in: Queue[String], out: Queue[(Int, String)]) {
def ask(s: String) = in.offer(s) *> out.take.timeout(1.second)
def fork = Svc(nextId.updateAndGet(_ + 1), in, out).serve.interruptible.fork
}
case class Svc(id: Int, in: Queue[String], out: Queue[(Int, String)]) {
val serve = ZStream
.fromQueue(in)
.takeWhile(_.nonEmpty)
.foreach(s => out.offer(id -> s))
}
}
object Tests extends ZIOSpecDefault {
def spec = suite("Tests")(
suite("Forked service")(
test("echo") {
for {
x <- EchoService.ask("foo")
} yield assertTrue(x == Some((1, "foo")))
}
).provideSomeLayerShared(EchoService.manager >+> EchoService.forked) @@ withLiveClock
)
}
from zio.
The workflow that uses the Clock service is ask but you provide the LiveClock to practically every other workflow in your test but not that one.
Not sure what you mean by that. Is not providing live clock to ask?
def ask(s: String) = ZIO.service[Mgr].flatMap(_.ask(s)).withClock(ClockLive)
The acquire action is performed uninterruptibly and fibers inherit the interruptibility of their parents so the forked fiber is interruptible. You then attempt to interrupt that fiber in the release action but that can never complete because the fiber is uninterruptible. You can use the interruptible operator on the workflow you are forking to designate that it should be interruptible even though it is forked in an uninterruptible region.
This is pure gold! Exactly what I was missing. Thank you so much!!!
from zio.
Related Issues (20)
- ZTestRunnerJS failure message is unhelpful
- assertTrue on zio-http Response - Exception occurred while executing macro expansion HOT 2
- ZStream.broadcast hangs when some of the streams are (implicitly) not consumed HOT 7
- zio-test: provide something like `assertAll(tests: Seq[TestResult])` HOT 3
- ZIO Metrics: Unexpected `ZStream.tagged` behavior
- ZPool incorrectly propagates uninterruptible flags to `get` Zio, for dynamically created members
- zio-test: compile error on assertion on zio.Cause HOT 4
- ZStream.ensuring, finalizer may be deferred to channel executor's close and as a result may cause deadlocks HOT 1
- ZLayer scoped doesn't work with multiple type constructors HOT 1
- No test found when an exception is raised in the construction of a provided layer HOT 4
- TestClock manipulation doesn't seem to work right for shared environment HOT 5
- Debounce on stream does not work properly HOT 3
- ZStream flatMapPar doesn't respect scope HOT 1
- zio-test + zio-prelude: Need way to "fuzzy compare" with Prelude HOT 2
- ZIO TEST: Using .nn feature breaks reporting on failing assertions with java.lang.StringIndexOutOfBoundsException HOT 5
- zio test 2.0.20 - contains in assertTrue failing compilation HOT 2
- Too big zio-test output when assertion failed on case class instances HOT 10
- 404 after clicking on the ZIO Newsletter button HOT 1
- Layers shared between different test runs when using `zio.test.check` HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zio.