Files: d2f2697f296dd39aed6a8b63c6d04a736a7db5b3 / node_modules / pull-stream / test / drain-abort.js
1313 bytesRaw
1 | var pull = require('..') |
2 | |
3 | var tape = require('tape') |
4 | |
5 | tape('abort on drain', function (t) { |
6 | |
7 | var c = 100 |
8 | var drain = pull.drain(function () { |
9 | if(c < 0) throw new Error('stream should have aborted') |
10 | if(!--c) return false //drain.abort() |
11 | }, function () { |
12 | t.end() |
13 | }) |
14 | |
15 | pull(pull.infinite(), drain) |
16 | |
17 | }) |
18 | |
19 | |
20 | function delay () { |
21 | return pull.asyncMap(function (e, cb) { |
22 | setTimeout(function () { cb(null, e) }) |
23 | }) |
24 | } |
25 | |
26 | tape('abort on drain - async', function (t) { |
27 | |
28 | var c = 100 |
29 | var drain = pull.drain(function () { |
30 | if(c < 0) throw new Error('stream should have aborted') |
31 | if(!--c) return drain.abort() |
32 | }, function () { |
33 | t.end() |
34 | }) |
35 | |
36 | pull(pull.infinite(), delay(), drain) |
37 | |
38 | }) |
39 | |
40 | tape('abort on drain - sync', function (t) { |
41 | |
42 | var c = 100 |
43 | var drain = pull.drain(function () { |
44 | if(c < 0) throw new Error('stream should have aborted') |
45 | if(!--c) return drain.abort() |
46 | }, function () { |
47 | t.end() |
48 | }) |
49 | |
50 | pull(pull.infinite(), drain) |
51 | |
52 | }) |
53 | |
54 | |
55 | tape('abort on drain - async, out of cb', function (t) { |
56 | |
57 | var c = 0, ERR = new Error('test ABORT') |
58 | var drain = pull.drain(function () { |
59 | --c |
60 | }, function (err) { |
61 | t.ok(c < 0) |
62 | t.equal(err, ERR) |
63 | t.end() |
64 | }) |
65 | |
66 | pull(pull.infinite(), delay(), drain) |
67 | |
68 | setTimeout(function () { |
69 | drain.abort(ERR) |
70 | }, 100) |
71 | |
72 | }) |
73 | |
74 | |
75 | |
76 | |
77 | |
78 |
Built with git-ssb-web