|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('assert'); |
| 4 | +var Queue = require('double-ended-queue'); |
| 5 | +var utils = require('../lib/utils'); |
| 6 | + |
| 7 | +describe('utils.js', function () { |
| 8 | + |
| 9 | + describe('clone', function () { |
| 10 | + it('ignore the object prototype and clone a nested array / object', function () { |
| 11 | + var obj = { |
| 12 | + a: [null, 'foo', ['bar'], { |
| 13 | + "I'm special": true |
| 14 | + }], |
| 15 | + number: 5, |
| 16 | + fn: function noop () {} |
| 17 | + }; |
| 18 | + var clone = utils.clone(obj); |
| 19 | + assert.deepEqual(clone, obj); |
| 20 | + assert.strictEqual(obj.fn, clone.fn); |
| 21 | + assert(typeof clone.fn === 'function'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('replace faulty values with an empty object as return value', function () { |
| 25 | + var a = utils.clone(); |
| 26 | + var b = utils.clone(null); |
| 27 | + assert.strictEqual(Object.keys(a).length, 0); |
| 28 | + assert.strictEqual(Object.keys(b).length, 0); |
| 29 | + }); |
| 30 | + |
| 31 | + it('throws on circular data', function () { |
| 32 | + try { |
| 33 | + var a = {}; |
| 34 | + a.b = a; |
| 35 | + utils.clone(a); |
| 36 | + throw new Error('failed'); |
| 37 | + } catch (e) { |
| 38 | + assert(e.message !== 'failed'); |
| 39 | + } |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('reply_in_order', function () { |
| 44 | + |
| 45 | + var err_count = 0; |
| 46 | + var res_count = 0; |
| 47 | + var emitted = false; |
| 48 | + var clientMock = { |
| 49 | + emit: function () { emitted = true; }, |
| 50 | + offline_queue: new Queue(), |
| 51 | + command_queue: new Queue() |
| 52 | + }; |
| 53 | + var create_command_obj = function () { |
| 54 | + return { |
| 55 | + callback: function (err, res) { |
| 56 | + if (err) err_count++; |
| 57 | + else res_count++; |
| 58 | + } |
| 59 | + }; |
| 60 | + }; |
| 61 | + |
| 62 | + beforeEach(function () { |
| 63 | + clientMock.offline_queue.clear(); |
| 64 | + clientMock.command_queue.clear(); |
| 65 | + err_count = 0; |
| 66 | + res_count = 0; |
| 67 | + emitted = false; |
| 68 | + }); |
| 69 | + |
| 70 | + it('no elements in either queue. Reply in the next tick', function (done) { |
| 71 | + var called = false; |
| 72 | + utils.reply_in_order(clientMock, function () { |
| 73 | + called = true; |
| 74 | + done(); |
| 75 | + }, null, null); |
| 76 | + assert(!called); |
| 77 | + }); |
| 78 | + |
| 79 | + it('no elements in either queue. Reply in the next tick', function (done) { |
| 80 | + assert(!emitted); |
| 81 | + utils.reply_in_order(clientMock, null, new Error('tada')); |
| 82 | + assert(!emitted); |
| 83 | + setTimeout(function () { |
| 84 | + assert(emitted); |
| 85 | + done(); |
| 86 | + }, 1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj callback', function (done) { |
| 90 | + clientMock.offline_queue.push(create_command_obj(), create_command_obj()); |
| 91 | + utils.reply_in_order(clientMock, function () { |
| 92 | + assert.strictEqual(clientMock.offline_queue.length, 0); |
| 93 | + assert.strictEqual(res_count, 2); |
| 94 | + done(); |
| 95 | + }, null, null); |
| 96 | + while (clientMock.offline_queue.length) clientMock.offline_queue.shift().callback(null, 'foo'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj error emit', function (done) { |
| 100 | + clientMock.command_queue.push({}, create_command_obj(), {}); |
| 101 | + utils.reply_in_order(clientMock, function () { |
| 102 | + assert.strictEqual(clientMock.command_queue.length, 0); |
| 103 | + assert(emitted); |
| 104 | + assert.strictEqual(err_count, 1); |
| 105 | + assert.strictEqual(res_count, 0); |
| 106 | + done(); |
| 107 | + }, null, null); |
| 108 | + while (clientMock.command_queue.length) { |
| 109 | + var command_obj = clientMock.command_queue.shift(); |
| 110 | + if (command_obj.callback) { |
| 111 | + command_obj.callback(new Error('tada')); |
| 112 | + } |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj', function (done) { |
| 117 | + clientMock.command_queue.push(create_command_obj(), {}); |
| 118 | + utils.reply_in_order(clientMock, function () { |
| 119 | + assert.strictEqual(clientMock.command_queue.length, 0); |
| 120 | + assert(!emitted); |
| 121 | + assert.strictEqual(res_count, 1); |
| 122 | + done(); |
| 123 | + }, null, null); |
| 124 | + while (clientMock.command_queue.length) { |
| 125 | + clientMock.command_queue.shift().callback(null, 'bar'); |
| 126 | + } |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments