aboutsummaryrefslogtreecommitdiff
path: root/core/t/FiberSpec.scala
blob: 22a2eb902b2d066c9269993a924dbafe11225d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package eu.mulk.fibers

import minitest._
import minitest.laws.Checkers
import monix.eval.Task
import monix.execution.Ack.{Continue, Stop}
import monix.execution.Scheduler.Implicits.global
import monix.execution.{Ack, Scheduler}
import monix.reactive.observers.Subscriber
import monix.reactive.{Consumer, Observable}

import scala.concurrent.Future
import scala.util.{Failure, Success, Try}

object FiberSpec extends SimpleTestSuite with Checkers {
  import Fiber._

  class FakeException extends RuntimeException

  test("sanity") {
    assert(0 != 1)
  }

  testAsync("can produce nothing") {
    val o = run {}
    for (empty ← o.isEmptyL) {
      assert(empty)
    }
  }

  testAsync("can produce a single value") {
    val o = run[Int] {
      emit(100)
    }
    for (l ← o.toListL) {
      assert(l == List(100))
    }
  }

  testAsync("can produce many values") {
    val o = run[Int] {
      for (x ← 1.to(10).cps)
        emit(x)
    }
    for (l ← o.toListL) {
      assertEquals(l, List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
    }
  }

  testAsync("can throw exceptions before emitting values") {
    val o = run[Int] {
      throw new FakeException
    }
    for (t ← o.toListL.failed) {
      assert(t.isInstanceOf[FakeException])
    }
  }

  testAsync("can throw exceptions after emitting values") {
    val o = run[Int] {
      emit(100)
      throw new FakeException
    }
    for (t ← o.toListL.failed) {
      assert(t.isInstanceOf[FakeException])
    }
  }

  testAsync("can await standard futures") {
    val err = new FakeException
    val f1 = Future(1)
    val f2 = Future.failed(err)
    val f3 = Future(3)
    val o = run[Try[Int]] {
      val y1 = await(f1)
      emit(y1)
      val y2 = await(f2)
      emit(y2)
      val y3 = await(f3)
      emit(y3)
    }
    for (t ← o.toListL) {
      assertEquals(t, List(Success(1), Failure(err), Success(3)))
    }
  }

  testAsync("can run Monix tasks") {
    val err = new FakeException
    val f1 = Task.delay(1)
    val f2 = Task.raiseError(err)
    val f3 = Task.delay(3)
    val o = run[Try[Int]] {
      val y1 = await(f1)
      emit(y1)
      val y2 = await(f2)
      emit(y2)
      val y3 = await(f3)
      emit(y3)
    }
    for (t ← o.toListL) {
      assertEquals(t, List(Success(1), Failure(err), Success(3)))
    }
  }

  testAsync("can spawn two fibers, one waiting for the other") {
    val o1: Observable[Int] = run[Int] {
      emit(1)
      emit(2)
      emit(3)
    }
    lazy val o2: Observable[Int] = run[Int] {
      val Success(a1) = await(o1.drop(0).firstL)
      emit(a1)
      val Success(a2) = await(o1.drop(1).firstL)
      emit(a2)
      val Success(a3) = await(o1.drop(2).firstL)
      emit(a3)
    }
    for (t2 ← o2.toListL) {
      assertEquals(t2, List(1, 2, 3))
    }
  }

  testAsync("can spawn two mutually waiting fibers") {
    lazy val o1: Observable[Int] = run[Int] {
      emit(1)
      val Success(b1) = await(o2.firstL)
      emit(b1 + 1)
      val Success(b2) = await(o2.tail.firstL)
      emit(b2 + 1)
    }.cache
    lazy val o2: Observable[Int] = run[Int] {
      val Success(a1) = await(o1.firstL)
      emit(a1 + 1)
      val Success(a2) = await(o1.tail.firstL)
      emit(a2 + 1)
    }.cache
    for (t1 ← o1.toListL;
         t2 ← o2.toListL) {
      assertEquals(t1, List(1, 3, 5))
      assertEquals(t2, List(2, 4))
    }
  }

  testAsync("can store fiber-local state") {
    lazy val o = run {
      putFiberVar(100)
      emit(1)
      val state = getFiberVar.asInstanceOf[Int]
      emit(state)
    }
    for (l ← o.toListL) {
      assertEquals(l, List(1, 100))
    }
  }

  testAsync("can properly maintain state between two alternating fibers") {
    val o1: Observable[Int] = run {
      putFiberVar(2)
      emit(1)
      val a2 = getFiberVar
      putFiberVar(3)
      emit(a2)
      val a3 = getFiberVar
      emit(a3)
    }
    lazy val o2: Observable[Int] = run {
      putFiberVar(100)
      val Success(a1) = await(o1.drop(0).firstL)
      emit(a1)
      emit(getFiberVar)
      val Success(a2) = await(o1.drop(1).firstL)
      emit(a2)
      emit(getFiberVar)
      val Success(a3) = await(o1.drop(2).firstL)
      emit(a3)
      emit(getFiberVar)
    }
    for (t2 ← o2.toListL) {
      assertEquals(t2, List(1, 100, 2, 100, 3, 100))
    }
  }

  testAsync("can be stopped") {
    var f3executed = false

    val f1 = Task.delay(1)
    val f2 = Task.delay(2)
    val f3 = Task.delay({ f3executed = true; 3 })

    val o: Observable[Int] = run {
      val Success(y1) = await(f1)
      emit(y1)
      val Success(y2) = await(f2)
      emit(y2)
      val Success(y3) = await(f3)
      emit(y3)
    }

    val consumeTask = o.consumeWith(Consumer.create[Int, Int] {
      (scheduler, cancelable, cb) ⇒
        new Subscriber.Sync[Int] {
          var triggered = 0

          override implicit def scheduler: Scheduler =
            monix.execution.Scheduler.Implicits.global

          override def onNext(elem: Int): Ack = {
            triggered += 1
            if (triggered >= 2) {
              cb.onSuccess(triggered)
              Stop
            } else {
              Continue
            }
          }

          override def onError(ex: Throwable): Unit =
            cb.onError(ex)

          override def onComplete(): Unit =
            ()
        }
    })

    for (result ← consumeTask) {
      assertEquals(result, 2)
      assert(!f3executed)
    }
  }
}