Debian Codegraph @

test/index.js

raw
1 var test = require("tape").test
2 var spigot = require("stream-spigot")
3 var concat = require("concat-stream")
4
5 var meter
6
7 // Stats
8 test("load", function (t) {
9 t.plan(1)
10
11 meter = require("../")
12 t.ok(meter, "loaded module")
13 })
14
15 test("no max (passthrough)", function (t) {
16 t.plan(2)
17
18 var m = meter()
19
20 var content = "ABCD1234"
21
22 function match(d) {
23 t.equals(d.toString(), content)
24 t.equals(m.bytes, 8)
25 }
26
27 spigot([content]).pipe(m).pipe(concat(match))
28 })
29
30 test("under max", function (t) {
31 t.plan(2)
32
33 var m = meter(100)
34
35 var content = "ABCD1234"
36
37 function match(d) {
38 t.equals(d.toString(), content)
39 t.equals(m.bytes, 8)
40 }
41
42 spigot([content]).pipe(m).pipe(concat(match))
43 })
44
45 test("stops at meter", function (t) {
46 t.plan(3)
47
48 var chunks = 0
49
50 function match(d) {
51 t.fail()
52 }
53
54 var c = concat(match)
55
56 var m = meter(10)
57 m.on("error", function (e) {
58 t.ok(e.message, "Stream exceeded specified max of 10 bytes.")
59 // 12 because read frame is 4, so the 3rd read will put it over the max at 12 bytes
60 t.equals(c.getBody().toString(), "ABCDEFGHIJKL")
61 t.equals(m.bytes, 12)
62 })
63
64 spigot(["ABCD", "EFGH", "IJKL", "MNOP", "QRST", "UVWX", "YZ"]).pipe(m).pipe(c)
65 })