-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
107 lines (87 loc) · 2.34 KB
/
test.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
const test = require('tape')
const from = require('from')
const {
createFifoWrapper,
createFifoUnwrapper,
} = require('./index.js')
test('basic test', function (t) {
var wrapper = createFifoWrapper()
var unwrapper = createFifoUnwrapper()
var inputArray = ['1','2','3','4']
var resultArray = []
t.plan(1)
from(inputArray)
.pipe(wrapper)
.pipe(unwrapper)
.on('data', function(data){
resultArray.push(data)
})
.on('end', function(){
t.equal(inputArray.toString(), resultArray.toString(), 'result should match input')
t.end()
})
})
test('out of order test', function (t) {
var wrapper = createFifoWrapper()
var unwrapper = createFifoUnwrapper()
var chaos = new OrderJumbleTransform()
var inputArray = ['1','2','3','4']
var resultArray = []
t.plan(1)
from(inputArray)
.pipe(wrapper)
.pipe(chaos)
.pipe(unwrapper)
.on('data', function(data){
resultArray.push(data)
})
.on('end', function(){
t.equal(inputArray.toString(), resultArray.toString(), 'result should match input')
t.end()
})
})
test('out of order w/o fifo test', function (t) {
var wrapper = createFifoWrapper()
var unwrapper = createFifoUnwrapper()
var chaos = new OrderJumbleTransform()
var inputArray = ['1','2','3','4']
var resultArray = []
t.plan(1)
from(inputArray)
.pipe(chaos)
.on('data', function(data){
resultArray.push(data)
})
.on('end', function(){
t.notEqual(inputArray.toString(), resultArray.toString(), 'result should not match input')
t.end()
})
})
// util
var TransformStream = require('stream').Transform
var inherits = require('util').inherits
// jumbles message order by switching order of every pair of messages.
inherits(OrderJumbleTransform, TransformStream)
function OrderJumbleTransform(opts) {
TransformStream.call(this)
}
OrderJumbleTransform.prototype._transform = function(payload, encoding, callback) {
var heldMessage = this.heldMessage
if (heldMessage) {
// send both, out of order
this.heldMessage = null
this.push(payload)
this.push(heldMessage)
} else {
// hold this message
this.heldMessage = payload
}
callback()
}
OrderJumbleTransform.prototype._flush = function(callback) {
var heldMessage = this.heldMessage
if (heldMessage) {
this.push(heldMessage)
}
callback()
}