dominictarr / through

simple way to create a ReadableWritable stream that works
Other
669 stars 64 forks source link

Weird behavior #30

Closed gangstead closed 9 years ago

gangstead commented 9 years ago

I'm working through stream-adventure and I'm trying to use through in a slightly different way than the official solution.

The jist of the supplied solution is this code in the server definition:

req.pipe(through(function (buf) {
    this.queue(buf.toString().toUpperCase());
})).pipe(res);

The only difference I'm trying to do is:

//outside server definition
var tr = through(function (buf) {
    this.queue(buf.toString().toUpperCase());
})

...
//inside server definition
req.pipe(tr).pipe(res);

It there any reason why I can't do an assignment like this?

My code will work when I make my posts individually, but when stream-adventure verifies it with a collection of tests I get a handful of actual results duplicated, and the rest of the expected results don't line up :

ACTUAL                             EXPECTED
------                             --------
"CAN"                              "CAN"
"SLOUGHCHANGE"                     "SLOUGHCHANGE"
"IN"                               "IN"
"THE"                              "THE"
"NIP"                              "NIP"
"OF"                               "OF"
"CAN"                          !== "A"
"SLOUGHCHANGE"                 !== "A"
"IN"                           !== "NAPPLE"
"THE"                          !== "NAPPLE"
"NIP"                          !== "SOLONGAS"
"OF"                           !== "WE"
"A"                            !== "SOLONGAS"
"A"                            !== "WE"
"NAPPLE"                       !== "CAN"
"NAPPLE"                       !== "CAN"
"SOLONGAS"                     !== "ALLSEE"
"WE"                           !== "ALLSEE"
"SOLONGAS"                     !== "FOR"
"WE"                           !== "FOR"
"CAN"                          !== "DEEDSETTON"
"CAN"                          !== "DEEDSETTON"
"ALLSEE"                       !== "YOUR"
"ALLSEE"                       !== "YOUR"
"FOR"                          !== "QUICK."
"FOR"                          !== "QUICK."
"DEEDSETTON"                   !== "TARK'S"
"DEEDSETTON"                   !== "TARK'S"
"YOUR"                         !== "BIMBOOWOOD"
"YOUR"                         !== "BIMBOOWOOD"
"QUICK."                       !== "SO"
"QUICK."                       !== "SO"
"TARK'S"                       !== "PLEASEKINDLY"
"TARK'S"                       !== "PLEASEKINDLY"
"BIMBOOWOOD"                   !== ""
"SO"                           !== null
"SO"                           !== null
"PLEASEKINDLY"                 !== null
"PLEASEKINDLY"                 !== null
""                             !== null
# FAIL

I've seen 1-5 results duplicated, sometimes it it's the expected results that are duplicated, not the actual results. Initially I wanted to blame the test runner, but it works fine when the through stream is defined inline so I suspect I'm not understanding something fundamental about the through module.

I've also used this code from an earlier module in stream-adventure that uses through and had the same problem:

var tr = through(write);

function write(buf) { this.queue(buf.toString().toUpperCase())};
function end() {this.queue('')};

//inside server definition
req.pipe(tr).pipe(res);
gangstead commented 9 years ago

The problem is that defining the through stream outside of the server means all the requests share the same queue. This is fine when I test it once at a time, but the tester makes a bunch of tests in parallel.

dominictarr commented 9 years ago

correct.