Skip to content

Commit 01c9c3a

Browse files
committed
refactoring
1 parent 3600a53 commit 01c9c3a

File tree

3 files changed

+43
-37
lines changed

3 files changed

+43
-37
lines changed

src/main/scala/app/Main.scala

+6-27
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,16 @@ package app
22

33
import zio._
44

5-
import java.io.IOException
6-
75
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(5)).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-
}
6+
override def run: ZIO[Environment with ZIOAppArgs with Scope, Any, Any] = for {
7+
queue <- Queue.bounded[String](100)
8+
_ <- offerReadLine(queue).forever.fork
9+
_ <- Program(queue).run
10+
} yield ()
2511

26-
val sayPlease = Console.printLine("say sth plz").repeat(Schedule.spaced(Duration.fromSeconds(1)))
27-
def say(queue: Queue[String]) = for {
12+
private def offerReadLine(queue: Queue[String]) = for {
2813
read <- Console.readLine
2914
_ <- queue.offer(read)
3015
} yield ()
3116

32-
33-
override def run: ZIO[Environment with ZIOAppArgs with Scope, Any, Any] = for {
34-
queue <- Queue.bounded[String](100)
35-
_ <- say(queue).forever.fork
36-
_ <- program(queue)
37-
} yield ()
3817
}

src/main/scala/app/Program.scala

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package app
2+
3+
import zio.{Console, Queue, _}
4+
5+
import java.io.IOException
6+
7+
case class Program(queue: Queue[String]) {
8+
9+
private val ticker = Console.printLine("say sth plz")
10+
.repeat(Schedule.spaced(Duration.fromSeconds(1)))
11+
12+
def run: ZIO[Any, Throwable, Unit] = {
13+
def loop(sayingBackgroundFiber: Ref[Fiber.Runtime[IOException, Long]]): Task[Unit] = for {
14+
read <- queue.take
15+
_ <- sayingBackgroundFiber.get.flatMap(_.interrupt)
16+
fork <- ticker.delay(Duration.fromSeconds(5)).fork
17+
_ <- sayingBackgroundFiber.set(fork)
18+
_ <- Console.printLine(s"wrote: $read")
19+
_ <- loop(sayingBackgroundFiber)
20+
} yield ()
21+
22+
for {
23+
fiber2 <- ticker.fork
24+
_ <- ZIO.sleep(100.millis)
25+
ref <- Ref.make(fiber2)
26+
_ <- loop(ref)
27+
} yield ()
28+
}
29+
30+
}

src/test/scala/app/MainSpec.scala

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,29 @@ import zio.test.{Spec, TestClock, TestConsole, TestEnvironment, ZIOSpecDefault,
55
import zio.{Queue, Scope, durationInt}
66

77
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") {
8+
override def spec: Spec[TestEnvironment with Scope, Any] = suite("Program")(
9+
test("non empty queue and less than 5s -> no ticker message") {
1310
for {
1411
queue <- Queue.bounded[String](100)
1512
_ <- queue.offer("a")
16-
_ <- Main.program(queue).fork
13+
_ <- Program(queue).run.fork
1714
_ <- TestClock.adjust(4.seconds)
1815
output <- TestConsole.output
1916
} yield assertTrue (output == Vector("say sth plz\n", "wrote: a\n"))
2017
},
21-
test("d") {
18+
test("non empty queue and more than 5s -> one ticker message") {
2219
for {
2320
queue <- Queue.bounded[String](100)
2421
_ <- queue.offer("a")
25-
_ <- Main.program(queue).fork
22+
_ <- Program(queue).run.fork
2623
_ <- TestClock.adjust(6.seconds)
2724
output <- TestConsole.output
2825
} yield assertTrue(output == Vector("say sth plz\n", "wrote: a\n", "say sth plz\n"))
2926
},
30-
test("d") {
27+
test("empty queue => ticker message multiplied by number of seconds + 1") {
3128
for {
3229
queue <- Queue.bounded[String](100)
33-
_ <- Main.program(queue).fork
30+
_ <- Program(queue).run.fork
3431
_ <- TestClock.adjust(9.seconds)
3532
output <- TestConsole.output
3633
} yield assertTrue(output.count(_ == "say sth plz\n") == 10)

0 commit comments

Comments
 (0)