-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path06-maybe-can-take-sync.js
367 lines (277 loc) · 10.6 KB
/
06-maybe-can-take-sync.js
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import test from './helpers'
import chan from '../src'
const NOT_YET = { notYet: true }
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`#maybeCanTakeSync() blocks until there is a pending send in a non-buffered chan, and ` +
`returns true when unblocked`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
await t.nextTurn()
t.ok(unblockedWith == NOT_YET)
let sent = false
ch.send('x').then(_ => sent = true).catch(t.fail)
await t.nextTick()
t.ok(unblockedWith == true)
t.ok(sent == false)
t.is('x', await ch.take())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`blocks until there is a buffered value in a buffered chan, and returns true ` +
`when unblocked`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan(3)
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
await t.nextTurn()
t.ok(unblockedWith == NOT_YET)
await ch.send('x')
await t.nextTick()
t.ok(unblockedWith == true)
t.is('x', await ch.take())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`blocks until there is a buffered value in a buffered chan, and returns true when ` +
`unblocked (sync send)`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan(1)
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
await t.nextTurn()
t.ok(unblockedWith == NOT_YET)
ch.sendSync('x')
await t.nextTick()
t.ok(unblockedWith == true)
t.is('x', await ch.take())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`gets unblocked on the next turn after a failed sync send`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
// initially blocked
await t.nextTick()
t.ok(unblockedWith == NOT_YET)
// failed send
t.ok(ch.sendSync('x') == false)
// still blocked on the next tick
let p1 = t.nextTick().then(_ => t.ok(unblockedWith == NOT_YET))
// unblocked on the next turn
let p2 = t.nextTurn().then(_ => t.ok(unblockedWith == true))
await Promise.all([ p1, p2 ])
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`gets unblocked by a maybeCanSendSync`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let maybeCanTake = NOT_YET
let maybeCanSend = NOT_YET
ch.maybeCanTakeSync().then(v => maybeCanTake = v).catch(t.fail)
ch.maybeCanSendSync().then(v => maybeCanSend = v).catch(t.fail)
await t.nextTick()
t.ok(maybeCanTake == true)
await t.nextTurn()
t.ok(maybeCanSend == NOT_YET)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`doesn't consume`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan(2)
ch.maybeCanTakeSync()
await ch.send('x')
ch.sendSync('y')
ch.send('z')
await ch.maybeCanTakeSync()
t.is('x', await ch.take())
t.is('y', await ch.take())
t.is('z', await ch.take())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`returns true if there is a waiting send in the chan at the time of the call`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let sent = false
ch.send('x').then(_ => sent = true).catch(t.fail)
await t.nextTick()
t.is(true, await ch.maybeCanTakeSync())
t.is('x', await ch.take())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`returns true if there is a buffered value in the chan`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan(1)
ch.sendSync('x')
t.is(true, await ch.maybeCanTakeSync())
t.is('x', await ch.take())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`remains blocked after receive`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let unblocked = false
ch.maybeCanTakeSync().then(_ => unblocked = true).catch(t.fail)
ch.take()
ch.takeSync()
await t.nextTurn()
t.ok(unblocked == false)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`remains blocked when sends get immediately consumed (non-buffered chan)`,
////////////////////////////////////////////////////////////////////////////////////////////////////
async t => {
let ch = chan()
let unblocked = false
ch.maybeCanTakeSync().then(_ => unblocked = true).catch(t.fail)
let pRecv = ch.take()
await t.nextTick()
await ch.send('x')
await t.nextTurn()
t.ok(unblocked == false)
t.is('x', await pRecv)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`remains blocked when sends get immediately consumed (buffered chan)`,
////////////////////////////////////////////////////////////////////////////////////////////////////
async t => {
let ch = chan(3)
let unblocked = false
ch.maybeCanTakeSync().then(_ => unblocked = true).catch(t.fail)
let pRecv = ch.take()
await t.nextTick()
await ch.send('x')
await t.nextTurn()
t.ok(unblocked == false)
t.is('x', await pRecv)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`returns false when chan gets closed`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
await ch.close()
t.ok(true == ch.isClosed)
await t.nextTurn()
t.ok(unblockedWith == false)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`returns false when chan gets synchronously closed`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
t.ok(true == ch.closeSync())
t.ok(true == ch.isClosed)
await t.nextTurn()
t.ok(unblockedWith == false)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`returns false when chan gets immediately closed`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let unblockedWith = NOT_YET
ch.maybeCanTakeSync().then(v => unblockedWith = v).catch(t.fail)
ch.closeNow()
t.ok(true == ch.isClosed)
await t.nextTurn()
t.ok(unblockedWith == false)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`doesn't interfere with chan closing`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
let sent = false
let closed = false
ch.send('x').then(_ => sent = true).catch(t.fail)
await t.nextTick()
ch.maybeCanTakeSync()
ch.close().then(_ => closed = true).catch(t.fail)
await t.nextTick()
t.ok(sent == false)
t.ok(closed == false)
t.ok('x' == await ch.take())
await t.nextTick()
t.ok(sent == true)
t.ok(closed == true)
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`returns false when chan is already closed`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan()
ch.closeNow()
t.is(false, await ch.maybeCanTakeSync())
t.is(false, await ch.maybeCanTakeSync())
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`multiple calls get unblocked by the same send`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let ch = chan(3)
let events = ''
ch.maybeCanTakeSync().then(v => events += `1(${v})`).catch(t.fail)
ch.maybeCanTakeSync().then(v => events += `2(${v})`).catch(t.fail)
ch.maybeCanTakeSync().then(v => events += `3(${v})`).catch(t.fail)
await ch.send('x')
await t.nextTick()
t.ok(events == '1(true)2(true)3(true)')
})
////////////////////////////////////////////////////////////////////////////////////////////////////
test(`can be used with #sendSync() for batch flow`, async t => {
////////////////////////////////////////////////////////////////////////////////////////////////////
let events = []
let ch = chan(2)
let producer = async items => {
let i = 0, n = 10; while (items.length && ++i <= n) {
let item = items.shift()
if (!ch.sendSync(item)) {
events.push(`P(sending ${item})`)
await ch.send(item)
}
events.push(`P(sent ${item})`)
}
if (i > n) {
events.push('P(livelock)')
} else {
events.push(`P(closing)`)
await ch.close()
events.push(`P(closed)`)
}
}
let consumer = async () => {
let i = 0, n = 10; while (++i <= n) {
events.push(`C(wait)`)
if (!await ch.maybeCanTakeSync()) {
events.push(`C(closed)`)
return
}
while (ch.takeSync() && ++i) {
events.push(`C(recv ${ ch.value })`)
}
}
events.push('C(livelock)')
}
let pProduced = producer([ 'a', 'b', 'c', 'd', 'e' ])
let pConsumed = consumer()
await pProduced
await pConsumed
t.same(events, [
`P(sent a)`,
`P(sent b)`,
`P(sending c)`,
`C(wait)`,
`C(recv a)`,
`C(recv b)`,
`C(recv c)`,
`C(wait)`,
`P(sent c)`,
`P(sent d)`,
`P(sent e)`,
`P(closing)`,
`C(recv d)`,
`C(recv e)`,
`C(wait)`,
`P(closed)`,
`C(closed)`,
])
})