~ubuntu-branches/ubuntu/wily/ruby-passenger/wily-proposed

« back to all changes in this revision

Viewing changes to test/node/line_reader_spec.js

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-11-23 23:50:02 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131123235002-8fdhsq7afj15o2z2
Tags: 4.0.25-1
* New upstream release.
* Refresh fix_install_path.patch.
* Build for Ruby 2.0 instead of 1.8. (Closes: #725591)
* Add fix_ftbfs_fortify_source.patch.
* Install passenger template files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var Helper = require('./spec_helper').Helper;
 
2
var FakeStream = require('./spec_helper').FakeStream;
 
3
var sinon = require('sinon');
 
4
var should = require('should');
 
5
var assert = require('assert');
 
6
var LineReader = require('phusion_passenger/line_reader').LineReader;
 
7
 
 
8
describe('LineReader', function() {
 
9
        this.timeout(1000);
 
10
 
 
11
        beforeEach(function() {
 
12
                this.stream = new FakeStream();
 
13
                this.reader = new LineReader(this.stream);
 
14
        });
 
15
 
 
16
        it('does nothing when the stream is idle', function(done) {
 
17
                var finished;
 
18
                this.reader.readLine(function(line) {
 
19
                        if (!finished) {
 
20
                                finished = true;
 
21
                                assert.fail();
 
22
                        }
 
23
                });
 
24
                setTimeout(function() {
 
25
                        if (!finished) {
 
26
                                finished = true;
 
27
                                done();
 
28
                        }
 
29
                }, 50);
 
30
        });
 
31
 
 
32
        describe('when one partial line has been received', function() {
 
33
                beforeEach(function() {
 
34
                        this.stream.emit('data', 'hello');
 
35
                });
 
36
 
 
37
                it('buffers the data', function() {
 
38
                        this.reader.buffer.should.equal('hello');
 
39
                });
 
40
 
 
41
                it('resumes the stream', function() {
 
42
                        this.stream.paused.should.be.false;
 
43
                        this.reader.paused.should.be.false;
 
44
                });
 
45
 
 
46
                describe('when the rest of the line is received later', function() {
 
47
                        beforeEach(function() {
 
48
                                this.stream.emit('data', " world\n");
 
49
                        });
 
50
 
 
51
                        it('memorizes a line', function() {
 
52
                                this.reader.lines.should.eql(["hello world\n"]);
 
53
                        });
 
54
 
 
55
                        it('empties the buffer', function() {
 
56
                                this.reader.buffer.should.eql('');
 
57
                        });
 
58
 
 
59
                        it('pauses the stream', function() {
 
60
                                this.stream.paused.should.be.true;
 
61
                                this.reader.paused.should.be.true;
 
62
                        });
 
63
                });
 
64
 
 
65
                describe('when the rest of the line, plus a partial line, is received later', function() {
 
66
                        beforeEach(function() {
 
67
                                this.stream.emit('data', " world\nhey");
 
68
                        });
 
69
 
 
70
                        it('memorizes a line', function() {
 
71
                                this.reader.lines.should.eql(["hello world\n"]);
 
72
                        });
 
73
 
 
74
                        it('buffers the partial line', function() {
 
75
                                this.reader.buffer.should.eql('hey');
 
76
                        });
 
77
 
 
78
                        it('pauses the stream', function() {
 
79
                                this.stream.paused.should.be.true;
 
80
                                this.reader.paused.should.be.true;
 
81
                        });
 
82
                });
 
83
 
 
84
                describe('when the rest of the line, plus a full line, is received later', function() {
 
85
                        beforeEach(function() {
 
86
                                this.stream.emit('data', " world\nhey\n");
 
87
                        });
 
88
 
 
89
                        it('memorizes two lines', function() {
 
90
                                this.reader.lines.should.eql(["hello world\n", "hey\n"]);
 
91
                        });
 
92
 
 
93
                        it('empties the buffer', function() {
 
94
                                this.reader.buffer.should.eql('');
 
95
                        });
 
96
 
 
97
                        it('pauses the stream', function() {
 
98
                                this.stream.paused.should.be.true;
 
99
                                this.reader.paused.should.be.true;
 
100
                        });
 
101
                });
 
102
        });
 
103
 
 
104
        describe('when one full line has been received', function() {
 
105
                beforeEach(function() {
 
106
                        this.stream.emit('data', "hello world\n");
 
107
                });
 
108
 
 
109
                it('memorizes the line', function() {
 
110
                        this.reader.lines.should.eql(["hello world\n"]);
 
111
                });
 
112
 
 
113
                it('empties the buffer', function() {
 
114
                        this.reader.buffer.should.eql('');
 
115
                });
 
116
 
 
117
                it('pauses the stream', function() {
 
118
                        this.stream.paused.should.be.true;
 
119
                        this.reader.paused.should.be.true;
 
120
                });
 
121
        });
 
122
 
 
123
        describe('when multiple full lines have been received', function() {
 
124
                beforeEach(function() {
 
125
                        this.stream.emit('data', "hello world\nhey\n");
 
126
                });
 
127
 
 
128
                it('memorizes all lines', function() {
 
129
                        this.reader.lines.should.eql(["hello world\n", "hey\n"]);
 
130
                });
 
131
 
 
132
                it('empties the buffer', function() {
 
133
                        this.reader.buffer.should.eql('');
 
134
                });
 
135
 
 
136
                it('pauses the stream', function() {
 
137
                        this.stream.paused.should.be.true;
 
138
                        this.reader.paused.should.be.true;
 
139
                });
 
140
        });
 
141
 
 
142
        describe('when multiple full lines and one partial line have been received', function() {
 
143
                beforeEach(function() {
 
144
                        this.stream.emit('data', "hello world\nhey\nfoo");
 
145
                });
 
146
 
 
147
                it('memorizes all full lines', function() {
 
148
                        this.reader.lines.should.eql(["hello world\n", "hey\n"]);
 
149
                });
 
150
 
 
151
                it('buffers the partial line', function() {
 
152
                        this.reader.buffer.should.eql('foo');
 
153
                });
 
154
 
 
155
                it('pauses the stream', function() {
 
156
                        this.stream.paused.should.be.true;
 
157
                        this.reader.paused.should.be.true;
 
158
                });
 
159
        });
 
160
 
 
161
        describe('on EOF', function() {
 
162
                describe('if the buffer is non-empty', function() {
 
163
                        beforeEach(function() {
 
164
                                this.reader.buffer = 'hello';
 
165
                                this.stream.emit('end');
 
166
                        });
 
167
 
 
168
                        it('memorizes the buffer contents as a line', function() {
 
169
                                this.reader.lines.should.eql(["hello"]);
 
170
                        });
 
171
                });
 
172
 
 
173
                it('marks the reader as having reached EOF', function() {
 
174
                        this.stream.emit('end');
 
175
                        this.reader.endReached().should.be.true;
 
176
                });
 
177
 
 
178
                it('pauses the stream', function() {
 
179
                        this.stream.emit('end');
 
180
                        this.stream.paused.should.be.true;
 
181
                        this.reader.paused.should.be.true;
 
182
                })
 
183
        });
 
184
 
 
185
        describe('.readLine', function() {
 
186
                describe('if there is at least one line in memory', function() {
 
187
                        beforeEach(function() {
 
188
                                this.reader.lines = ["hello\n", "world\n"];
 
189
                        });
 
190
 
 
191
                        it('pops the first line in memory', function(done) {
 
192
                                this.reader.readLine(function(line) {
 
193
                                        line.should.eql("hello\n");
 
194
                                        done();
 
195
                                });
 
196
                        });
 
197
 
 
198
                        describe('if the line memory is non-empty after reading', function() {
 
199
                                it('pauses the stream', function(done) {
 
200
                                        var self = this;
 
201
                                        this.reader.readLine(function(line) {
 
202
                                                line.should.eql("hello\n");
 
203
                                                process.nextTick(function() {
 
204
                                                        self.stream.paused.should.be.true;
 
205
                                                        self.reader.paused.should.be.true;
 
206
                                                        done();
 
207
                                                });
 
208
                                        });
 
209
                                });
 
210
                        });
 
211
 
 
212
                        describe('if the line memory is empty after reading', function() {
 
213
                                beforeEach(function() {
 
214
                                        this.reader.lines = ["hello\n"];
 
215
                                });
 
216
 
 
217
                                it('resumes the stream', function(done) {
 
218
                                        var self = this;
 
219
                                        this.reader.readLine(function(line) {
 
220
                                                line.should.eql("hello\n");
 
221
                                                process.nextTick(function() {
 
222
                                                        self.stream.paused.should.be.false;
 
223
                                                        self.reader.paused.should.be.false;
 
224
                                                        done();
 
225
                                                });
 
226
                                        });
 
227
                                });
 
228
                        });
 
229
 
 
230
                        describe('if the stream had already reached EOF', function() {
 
231
                                beforeEach(function() {
 
232
                                        this.reader.eof = true;
 
233
                                        this.reader.lines = ["hello\n"];
 
234
                                });
 
235
 
 
236
                                it('yields the line upon first call', function(done) {
 
237
                                        this.reader.readLine(function(line) {
 
238
                                                line.should.eql("hello\n");
 
239
                                                done();
 
240
                                        });
 
241
                                });
 
242
 
 
243
                                it('yields undefined once the line memory has become empty', function(done) {
 
244
                                        var self = this;
 
245
                                        this.reader.readLine(function(line) {
 
246
                                                line.should.eql("hello\n");
 
247
                                                self.reader.readLine(function(line2) {
 
248
                                                        should(line2).equal(undefined);
 
249
                                                        self.reader.readLine(function(line3) {
 
250
                                                                should(line3).equal(undefined);
 
251
                                                                done();
 
252
                                                        });
 
253
                                                });
 
254
                                        });
 
255
                                });
 
256
                        });
 
257
 
 
258
                        describe('when calling readLine again in the callback', function() {
 
259
                                beforeEach(function() {
 
260
                                        this.reader.lines = ["hello\n"];
 
261
                                });
 
262
 
 
263
                                it('waits until another line has been memorized', function(done) {
 
264
                                        var self = this;
 
265
                                        var finished;
 
266
                                        this.reader.readLine(function(line) {
 
267
                                                line.should.eql("hello\n");
 
268
                                                self.reader.readLine(function(line2) {
 
269
                                                        if (!finished) {
 
270
                                                                finished = true;
 
271
                                                                line2.should.eql("world\n");
 
272
                                                                done();
 
273
                                                        }
 
274
                                                });
 
275
                                                setTimeout(function() {
 
276
                                                        if (!finished) {
 
277
                                                                finished = true;
 
278
                                                                self.stream.emit('data', "world\n");
 
279
                                                                done();
 
280
                                                        }
 
281
                                                }, 50);
 
282
                                        });
 
283
                                });
 
284
 
 
285
                                it('resumes the stream', function(done) {
 
286
                                        var self = this;
 
287
                                        var finished;
 
288
                                        this.reader.readLine(function(line) {
 
289
                                                line.should.eql("hello\n");
 
290
                                                self.reader.readLine(function(line2) {
 
291
                                                        if (!finished) {
 
292
                                                                finished = true;
 
293
                                                                assert.fail("never reached");
 
294
                                                        }
 
295
                                                });
 
296
                                                process.nextTick(function() {
 
297
                                                        if (!finished) {
 
298
                                                                finished = true;
 
299
                                                                self.stream.paused.should.be.false;
 
300
                                                                self.reader.paused.should.be.false;
 
301
                                                                done();
 
302
                                                        }
 
303
                                                });
 
304
                                        });
 
305
                                });
 
306
                        });
 
307
                });
 
308
 
 
309
                describe('if there is no line in memory', function() {
 
310
                        it('waits until a line has been memorized', function(done) {
 
311
                                var state = 'waiting';
 
312
                                var self = this;
 
313
                                this.reader.readLine(function(line) {
 
314
                                        state.should.eql('line fed');
 
315
                                        line.should.eql("hello\n");
 
316
                                        done();
 
317
                                });
 
318
                                setTimeout(function() {
 
319
                                        state.should.eql('waiting');
 
320
                                        state = 'line fed';
 
321
                                        self.stream.emit('data', "hello\n");
 
322
                                }, 50);
 
323
                        });
 
324
 
 
325
                        describe('if the stream had already reached EOF', function(done) {
 
326
                                beforeEach(function() {
 
327
                                        this.reader.eof = true;
 
328
                                })
 
329
 
 
330
                                it('yields undefined', function() {
 
331
                                        this.reader.readLine(function(line) {
 
332
                                                should(line).equal(undefined);
 
333
                                        });
 
334
                                });
 
335
                        });
 
336
                });
 
337
        });
 
338
});