"use strict";
/**
 * Tests AmqpConnection
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Amqp Connection
 */
Object.defineProperty(exports, "__esModule", { value: true });
var AmqpConnection_1 = require("../../../src/system/amqp/AmqpConnection");
var events_1 = require("events");
var chai_1 = require("chai");
chai_1.use(require('chai-as-promised'));
describe('AmqpConnection', function () {
    describe('#connect', function () {
        it('fails when exchange cannot be asserted', function () {
            var expected_err = new Error('test failure');
            var mock_channel = {
                on: function () { },
                assertExchange: function () { return Promise.reject(expected_err); },
            };
            var mock_connection = {
                once: function () { },
                createChannel: function () {
                    return Promise.resolve(mock_channel);
                },
            };
            var mock_amqp = {
                connect: function () {
                    return Promise.resolve(mock_connection);
                },
            };
            var emitter = new events_1.EventEmitter();
            var conf = {};
            var sut = new AmqpConnection_1.AmqpConnection(mock_amqp, conf, emitter);
            return chai_1.expect(sut.connect()).to.eventually.be.rejectedWith(expected_err);
        });
    });
    describe('#reconnect', function () {
        it('is called when there is an error with the connection', function () {
            var reconnect_called = false;
            var mock_channel = {
                on: function () { },
                assertExchange: function () { return Promise.resolve(); },
            };
            var mock_connection = (Object.create(new events_1.EventEmitter()));
            mock_connection.createChannel = function () {
                return Promise.resolve(mock_channel);
            };
            var mock_amqp = {
                connect: function () {
                    return Promise.resolve(mock_connection);
                },
            };
            var emitter = new events_1.EventEmitter();
            emitter.on('amqp-reconnect', function () {
                reconnect_called = true;
            });
            var conf = {};
            var sut = new AmqpConnection_1.AmqpConnection(mock_amqp, conf, emitter);
            var result = sut.connect().then(function () { return mock_connection.emit('error'); });
            return chai_1.expect(result)
                .to.eventually.deep.equal(true)
                .then(function (_) { return chai_1.expect(reconnect_called).to.be.true; });
        });
        it('is called when there is an error with the channel', function () {
            var reconnect_called = false;
            var mock_channel = Object.create(new events_1.EventEmitter());
            mock_channel.assertExchange = function () {
                return Promise.resolve();
            };
            var mock_connection = (Object.create(new events_1.EventEmitter()));
            mock_connection.createChannel = function () {
                return Promise.resolve(mock_channel);
            };
            var mock_amqp = {
                connect: function () {
                    return Promise.resolve(mock_connection);
                },
            };
            var emitter = new events_1.EventEmitter();
            emitter.on('amqp-reconnect', function () {
                reconnect_called = true;
            });
            var conf = {};
            var sut = new AmqpConnection_1.AmqpConnection(mock_amqp, conf, emitter);
            var result = sut.connect().then(function () { return mock_channel.emit('close'); });
            return chai_1.expect(result)
                .to.eventually.deep.equal(true)
                .then(function (_) { return chai_1.expect(reconnect_called).to.be.true; });
        });
        it('throws an error if it is unable to reconnect', function (done) {
            var connect_call_count = 0;
            var retry_call_count = 0;
            var mock_channel = {
                on: function () { },
                assertExchange: function () { return Promise.resolve(); },
            };
            var conn = Object.create(new events_1.EventEmitter());
            conn.createChannel = function () {
                return Promise.resolve(mock_channel);
            };
            var mock_amqp = {
                connect: function () {
                    if (connect_call_count++ === 0) {
                        return Promise.resolve(conn);
                    }
                    return Promise.reject(new Error('Foo'));
                },
            };
            var conf = {
                retry_wait: 0,
                retries: 3,
            };
            var sut = new AmqpConnection_1.AmqpConnection(mock_amqp, conf, new events_1.EventEmitter());
            var old_setTimeout = global.setTimeout;
            global.setTimeout = function (cb, _) {
                retry_call_count++;
                try {
                    cb();
                }
                catch (e) {
                    chai_1.expect(e).to.deep.equal(new Error('Coulds not re-establish AMQP connection.'));
                    chai_1.expect(retry_call_count).to.equal(3);
                    chai_1.expect(connect_call_count).to.equal(4);
                    global.setTimeout = old_setTimeout;
                    done();
                }
                return {};
            };
            sut.connect().then(function (_) { return conn.emit('error', 'moo'); });
        });
    });
});
//# sourceMappingURL=data:application/json;base64, 