UNPKG

3.29 kBJavaScriptView Raw
1/* eslint-disable max-lines-per-function */
2/* eslint-disable func-names */
3const StreamClient = require('../lib/stream_client');
4const PassThroughStream = require('stream').PassThrough;
5
6describe('StreamClient', function() {
7 this.timeout(10000);
8
9 describe('#buildStreamPromise(response)', function () {
10 it('should resolve when #disconnect() is called', function(done) {
11 let client = new StreamClient({token: 'abcdef'});
12 let stream = new PassThroughStream();
13 let response_mock = { data: stream };
14 client.buildStreamPromise(response_mock)
15 .then(done)
16 .catch((e) => done(e));
17 setTimeout(() => client.disconnect(), 50);
18 stream.end();
19 });
20
21 it('should emit tweet without a timeout when it is streamed in', function(done) {
22 let client = new StreamClient({token: 'abcdef'});
23 let stream = new PassThroughStream();
24 let response_mock = { data: stream };
25 client.on('tweet', () => done());
26 client.buildStreamPromise(response_mock).catch((e) => done(e));
27 stream.write(`{"data":{"test":"dave"}}\r\n`);
28 stream.end();
29 });
30
31 it('should reject with an error when stream emits error', function(done) {
32 let client = new StreamClient({token: 'abcdef'});
33 let stream = new PassThroughStream();
34 let response_mock = { data: stream };
35 client.buildStreamPromise(response_mock)
36 .then(() => {
37 done(new Error('Stream didnt reject'));
38 })
39 .catch(() => done());
40 stream.emit('error', new Error('Fake error'));
41 stream.end();
42 });
43 });
44
45 describe('#connect({ params: object, max_connects: number})', function() {
46 it('should reject after max_reconnects attempts', function(done) {
47 let client = new StreamClient({
48 token: 'abcdef',
49 timeout: 1
50 });
51
52 client.buildConnection = () => {
53 let error_fake = new Error('Fake retriable error');
54 error_fake.status = 429;
55 return Promise.reject(error_fake);
56 };
57
58 client.connect({max_reconnects: -2})
59 .then(() => {
60 done(new Error('Did not reject'));
61 })
62 .catch((error) => {
63 if(error.reconnects) {
64 done();
65 } else {
66 done(error);
67 }
68 });
69 });
70
71 it('should resolve on disconnect', function(done) {
72 let client = new StreamClient({
73 token: 'abcdef',
74 timeout: 1000
75 });
76
77 client.connect({max_reconnects: 1})
78 .then(() => done())
79 .catch((error) => done(error));
80
81 setTimeout(() => client.disconnect(), 50);
82 });
83
84 it('should reject on unretriable error', function(done) {
85 let client = new StreamClient({
86 token: 'abcdef',
87 timeout: 1000
88 });
89
90 client.skip_sleep = true;
91 client.buildConnection = () => {
92 let error_fake = new Error('Retriable error should not throw');
93 error_fake.status = 420;
94 return Promise.reject(error_fake);
95 };
96
97 client.connect({max_reconnects: 1})
98 .then(() => {
99 done(new Error('Did not reject'));
100 })
101 .catch((error) => {
102 if(error.reconnects) {
103 done(error);
104 } else {
105 done();
106 }
107 });
108 });
109 });
110});