@@ -11,161 +11,6 @@ import scala.concurrent.duration.DurationInt
11
11
import scala .jdk .CollectionConverters .*
12
12
13
13
class SourceOpsTest extends AnyFlatSpec with Matchers with Eventually {
14
- it should " map over a source" in {
15
- scoped {
16
- val c = Channel [Int ]()
17
- fork {
18
- c.send(1 )
19
- c.send(2 )
20
- c.send(3 )
21
- c.done()
22
- }
23
-
24
- val s = c.map(_ * 10 )
25
-
26
- s.receive() shouldBe 10
27
- s.receive() shouldBe 20
28
- s.receive() shouldBe 30
29
- s.receive() shouldBe ChannelClosed .Done
30
- }
31
- }
32
-
33
- it should " map over a source (stress test)" in {
34
- // this demonstrated a race condition where a cell was added by select to the waiting list by T1, completed by T2,
35
- // which then subsequently completed the stream; only then T1 wakes up, and checks if no new elements have been added
36
- for (_ <- 1 to 100000 ) {
37
- scoped {
38
- val c = Channel [Int ]()
39
- fork {
40
- c.send(1 )
41
- c.done()
42
- }
43
-
44
- val s = c.map(_ * 10 )
45
-
46
- s.receive() shouldBe 10
47
- s.receive() shouldBe ChannelClosed .Done
48
- }
49
- }
50
- }
51
-
52
- it should " map over a source using for-syntax" in {
53
- scoped {
54
- val c = Channel [Int ]()
55
- fork {
56
- c.send(1 )
57
- c.send(2 )
58
- c.send(3 )
59
- c.done()
60
- }
61
-
62
- val s = for {
63
- v <- c
64
- } yield v * 2
65
-
66
- s.receive() shouldBe 2
67
- s.receive() shouldBe 4
68
- s.receive() shouldBe 6
69
- s.receive() shouldBe ChannelClosed .Done
70
- }
71
- }
72
-
73
- it should " iterate over a source" in {
74
- val c = Channel [Int ](10 )
75
- c.send(1 )
76
- c.send(2 )
77
- c.send(3 )
78
- c.done()
79
-
80
- var r : List [Int ] = Nil
81
- c.foreach(v => r = v :: r)
82
-
83
- r shouldBe List (3 , 2 , 1 )
84
- }
85
-
86
- it should " iterate over a source using for-syntax" in {
87
- val c = Channel [Int ](10 )
88
- c.send(1 )
89
- c.send(2 )
90
- c.send(3 )
91
- c.done()
92
-
93
- var r : List [Int ] = Nil
94
- for {
95
- v <- c
96
- } r = v :: r
97
-
98
- r shouldBe List (3 , 2 , 1 )
99
- }
100
-
101
- it should " convert source to a list" in {
102
- val c = Channel [Int ](10 )
103
- c.send(1 )
104
- c.send(2 )
105
- c.send(3 )
106
- c.done()
107
-
108
- c.toList shouldBe List (1 , 2 , 3 )
109
- }
110
-
111
- it should " transform a source using a simple map" in {
112
- val c = Channel [Int ](10 )
113
- c.send(1 )
114
- c.send(2 )
115
- c.send(3 )
116
- c.done()
117
-
118
- scoped {
119
- c.transform(_.map(_ * 2 )).toList shouldBe List (2 , 4 , 6 )
120
- }
121
- }
122
-
123
- it should " transform a source using a complex chain of operations" in {
124
- val c = Channel [Int ](10 )
125
- c.send(1 )
126
- c.send(2 )
127
- c.send(3 )
128
- c.send(4 )
129
- c.done()
130
-
131
- scoped {
132
- c.transform(_.drop(2 ).flatMap(i => List (i, i + 1 , i + 2 )).filter(_ % 2 == 0 )).toList shouldBe List (4 , 4 , 6 )
133
- }
134
- }
135
-
136
- it should " transform an infinite source" in {
137
- val c = Channel [Int ]()
138
- scoped {
139
- fork {
140
- var i = 0
141
- while true do
142
- c.send(i)
143
- i += 1
144
- }
145
-
146
- val s = c.transform(_.filter(_ % 2 == 0 ).flatMap(i => List (i, i + 1 )))
147
- s.receive() shouldBe 0
148
- s.receive() shouldBe 1
149
- s.receive() shouldBe 2
150
- }
151
- }
152
-
153
- it should " transform an infinite source (stress test)" in {
154
- for (_ <- 1 to 1000 ) { // this nicely demonstrated two race conditions
155
- val c = Channel [Int ]()
156
- scoped {
157
- fork {
158
- var i = 0
159
- while true do
160
- c.send(i)
161
- i += 1
162
- }
163
-
164
- val s = c.transform(x => x)
165
- s.receive() shouldBe 0
166
- }
167
- }
168
- }
169
14
170
15
it should " tick regularly" in {
171
16
scoped {
@@ -231,28 +76,6 @@ class SourceOpsTest extends AnyFlatSpec with Matchers with Eventually {
231
76
}
232
77
}
233
78
234
- it should " create a source from a fork" in {
235
- scoped {
236
- val f = fork(1 )
237
- val c = Source .fromFork(f)
238
- c.toList shouldBe List (1 )
239
- }
240
- }
241
-
242
- it should " create an iterating source" in {
243
- scoped {
244
- val c = Source .iterate(1 )(_ + 1 )
245
- c.take(3 ).toList shouldBe List (1 , 2 , 3 )
246
- }
247
- }
248
-
249
- it should " unfold a function" in {
250
- scoped {
251
- val c = Source .unfold(0 )(i => if i < 3 then Some ((i, i + 1 )) else None )
252
- c.toList shouldBe List (0 , 1 , 2 )
253
- }
254
- }
255
-
256
79
it should " concatenate sources" in {
257
80
scoped {
258
81
val s1 = Source .fromValues(" a" , " b" , " c" )
@@ -264,12 +87,4 @@ class SourceOpsTest extends AnyFlatSpec with Matchers with Eventually {
264
87
s.toList shouldBe List (" a" , " b" , " c" , " d" , " e" , " f" , " g" , " h" , " i" )
265
88
}
266
89
}
267
-
268
- it should " produce a range" in {
269
- scoped {
270
- Source .range(1 , 5 , 1 ).toList shouldBe List (1 , 2 , 3 , 4 , 5 )
271
- Source .range(1 , 5 , 2 ).toList shouldBe List (1 , 3 , 5 )
272
- Source .range(1 , 11 , 3 ).toList shouldBe List (1 , 4 , 7 , 10 )
273
- }
274
- }
275
90
}
0 commit comments