Skip to content

Commit 20ce665

Browse files
committed
Initial commit
0 parents  commit 20ce665

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Project exclude paths
2+
/project/target/
3+
/target/
4+
/target/scala-2.13/classes/
5+
/target/scala-2.13/test-classes/

build.sbt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ThisBuild / scalaVersion := "2.13.10"
2+
ThisBuild / version := "0.1.0-SNAPSHOT"
3+
ThisBuild / organization := "com.example"
4+
ThisBuild / organizationName := "example"
5+
6+
lazy val root = (project in file("."))
7+
.settings(
8+
name := "zio2-fiber-workshop",
9+
libraryDependencies ++= Seq(
10+
"dev.zio" %% "zio" % "2.0.6",
11+
"dev.zio" %% "zio-test" % "2.0.6" % Test
12+
),
13+
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
14+
)

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 1.8.2

src/main/scala/app/Main.scala

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package app
2+
3+
import zio._
4+
5+
import java.io.IOException
6+
7+
object Main extends ZIOAppDefault {
8+
def program(queue: Queue[String]) = {
9+
def loop(sayingBackgroundFiber: Ref[Fiber.Runtime[IOException, Long]]): Task[Unit] = for {
10+
read <- queue.take
11+
_ <- sayingBackgroundFiber.get.flatMap(_.interrupt)
12+
fork <- sayPlease.delay(Duration.fromSeconds(10)).fork
13+
_ <- sayingBackgroundFiber.set(fork)
14+
_ <- Console.printLine(s"wrote: $read")
15+
_ <- loop(sayingBackgroundFiber)
16+
} yield ()
17+
18+
for {
19+
fiber2 <- sayPlease.fork
20+
_ <- ZIO.sleep(100.millis)
21+
ref <- Ref.make(fiber2)
22+
_ <- loop(ref)
23+
} yield ()
24+
}
25+
26+
val sayPlease = Console.printLine("say sth plz").repeat(Schedule.spaced(Duration.fromSeconds(1)))
27+
def say(queue: Queue[String]) = for {
28+
read <- Console.readLine
29+
_ <- queue.offer(read)
30+
} yield ()
31+
32+
33+
override def run: ZIO[Environment with ZIOAppArgs with Scope, Any, Any] = for {
34+
queue <- Queue.bounded[String](100)
35+
_ <- say(queue).fork
36+
_ <- program(queue)
37+
} yield ()
38+
}

src/test/scala/app/MainSpec.scala

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package app
2+
3+
import zio.test.TestAspect.repeats
4+
import zio.test.{Spec, TestClock, TestConsole, TestEnvironment, ZIOSpecDefault, assertTrue}
5+
import zio.{Queue, Scope, durationInt}
6+
7+
object MainSpec extends ZIOSpecDefault {
8+
override def spec: Spec[TestEnvironment with Scope, Any] = suite("a")(
9+
test("b") {
10+
assertTrue("a" == "a")
11+
},
12+
test("c") {
13+
for {
14+
queue <- Queue.bounded[String](100)
15+
_ <- queue.offer("a")
16+
_ <- Main.program(queue).fork
17+
_ <- TestClock.adjust(9.seconds)
18+
output <- TestConsole.output
19+
} yield assertTrue (output == Vector("say sth plz\n", "wrote: a\n"))
20+
},
21+
test("d") {
22+
for {
23+
queue <- Queue.bounded[String](100)
24+
_ <- queue.offer("a")
25+
_ <- Main.program(queue).fork
26+
_ <- TestClock.adjust(11.seconds)
27+
output <- TestConsole.output
28+
} yield assertTrue(output == Vector("say sth plz\n", "wrote: a\n", "say sth plz\n"))
29+
},
30+
test("d") {
31+
for {
32+
queue <- Queue.bounded[String](100)
33+
_ <- Main.program(queue).fork
34+
_ <- TestClock.adjust(9.seconds)
35+
output <- TestConsole.output
36+
} yield assertTrue(output.count(_ == "say sth plz\n") == 10)
37+
}
38+
) @@ repeats(50)
39+
}

0 commit comments

Comments
 (0)