/
csp-test.mjs
215 lines (197 loc) · 4.65 KB
/
csp-test.mjs
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
#metaimport
masakari
hash-require
#require
chai expect
mori
'../lib/csp' (go, chan, put, put-async, alts, close!, onto, into, timeout)
#external
describe
it
#defmacro #go
unary
LOW
expand: block ->
`go #->
try do
~`block
catch var go-error
done go-error
describe
'go'
#->
describe
'returns a channel which'
#->
it
'will emit result and close'
done ->
const ch = chan ()
const g =
go #->
var v = yield ch
v
#go
yield put (ch, 42)
(expect yield g).to.eql 42
(expect yield g).to.eql null
done ()
it
'will close in case of an exception'
done ->
const g =
go #->
yield timeout 1
throw new Error ()
#go
(expect yield g).to.eql null
done ()
describe
'#alts'
#->
it
'will let only one operation succeed'
done ->
const
c1 = chan ()
c2 = chan ()
const g =
#go
var r = yield alts ([c1, [c2, ::c2]])
r
#go
yield put (c1, ::c1)
(expect yield alts ([c2], {default: ::default})).to.eql {value: ::default}
(expect yield g).to.have.property (::value).eql ::c1
done ()
describe
'#chan n'
#->
[0, 1, 2, 3].for-each n ->
it
'allows #n operations to succeed (' + n + ')'
done ->
const ch = chan n
#go
var i = 0
while i < n
#let ({value} = yield alts ([[ch, i]], {default: ::failed}))
(expect value).to.eql i
i += 1
#let ({value} = yield alts ([[ch, 42]], {default: ::failed}))
(expect value).to.eql ::failed
done ()
describe
'#dropping-chan'
#->
[1, 2, 3].for-each n ->
it
'preserves first #n elements (' + n + ')'
done ->
const
coll = [1, 2, 3, 4]
ch = csp.dropping-chan n
es = coll.slice (0, n)
#go
yield onto (ch, coll)
(expect yield into ([], ch)).to.eql es
(expect yield ch).to.eql null
done ()
describe
'#sliding-chan'
#->
[1, 2, 3].for-each n ->
it
'preserves last #n elements (' + n + ')'
done ->
const
coll = [1, 2, 3, 4]
ch = csp.sliding-chan n
es = coll.slice (coll.length - n)
#go
yield onto (ch, coll)
(expect yield into ([], ch)).to.eql es
(expect yield ch).to.eql null
done ()
describe
'#close!'
#->
it
'allows pending puts to complete'
done ->
const ch =
#doto (chan ())
put-async 42
close!
#go
(expect yield ch).to.eql 42
(expect yield ch).to.eql null
done ()
it
'causes pending takes to complete with null'
done ->
const ch1 = chan ()
const ch2 = chan ()
#go
(expect yield ch1).to.eql null
yield put (ch2, 42)
#go
close! ch1
(expect yield ch2).to.eql 42
done ()
const closed-chan =
#doto (chan ())
close!
it
'causes #put to complete immediatelly with false'
done ->
#go
(expect yield put (closed-chan, 42)).to.eql false
done ()
it
'causes #take to complete immediatelly with null'
done ->
#go
(expect yield closed-chan).to.eql null
done ()
describe
'#timeout returns channel which'
#->
it
'closes after the given timeout'
done ->
#go
(expect yield timeout 1).to.eql null
done ()
it
'can be passed to #alts'
done ->
const
c = chan ()
t = timeout 1
#go
#let ({channel} = yield alts ([c, t]))
(expect channel).to.equal t
done ()
it
'can be explicitly taken from'
done ->
const c = timeout 1
#go
(expect yield csp.take c).to.eql null
done ()
describe
'#onto'
#->
[[], [1], [1, 2]].for-each array ->
it
'puts all elements of given collection into channel'
done ->
const c = chan ()
onto (c, array, ::keep-open)
#go
var i = 0
while (i < array.length)
(expect yield c).to.eql (array[i])
i += 1
done ()