|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const chai = require("chai"); |
| 4 | +const expect = chai.expect; |
| 5 | +const request = require("supertest"); |
| 6 | +const sleep = require("then-sleep"); |
| 7 | +const amqplib = require("amqplib"); |
| 8 | + |
| 9 | +const merapi = require("merapi"); |
| 10 | +const { async, Component } = require("merapi"); |
| 11 | + |
| 12 | +const { rabbitConnection, rabbitUrl } = require("./configuration.js"); |
| 13 | + |
| 14 | +/* eslint-env mocha */ |
| 15 | + |
| 16 | +describe("Merapi Plugin Service: Queue Subscriber", function() { |
| 17 | + let publisherContainer, subscriberAContainer, subscriberBContainer; |
| 18 | + let service = {}; |
| 19 | + let connection = {}; |
| 20 | + let channel = {}; |
| 21 | + let messageA = []; |
| 22 | + |
| 23 | + beforeEach(async(function*() { |
| 24 | + this.timeout(5000); |
| 25 | + |
| 26 | + let publisherConfig = { |
| 27 | + name: "publisher", |
| 28 | + version: "1.0.0", |
| 29 | + main: "mainCom", |
| 30 | + plugins: ["service"], |
| 31 | + service: { |
| 32 | + rabbit: rabbitConnection, |
| 33 | + queue: { |
| 34 | + publish: { |
| 35 | + subscriber: { |
| 36 | + sub_queue_reconnect_publisher_test: "inQueuePublisherTest", |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + port: 5135, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + let subscriberConfig = { |
| 45 | + name: "subscriber", |
| 46 | + version: "1.0.0", |
| 47 | + main: "mainCom", |
| 48 | + plugins: ["service"], |
| 49 | + service: { |
| 50 | + rabbit: rabbitConnection, |
| 51 | + queue: { |
| 52 | + subscribe: { |
| 53 | + sub_queue_reconnect_publisher_test: "mainCom.handleIncomingMessage", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + publisherContainer = merapi({ |
| 60 | + basepath: __dirname, |
| 61 | + config: publisherConfig, |
| 62 | + }); |
| 63 | + |
| 64 | + publisherContainer.registerPlugin( |
| 65 | + "service-rabbit", |
| 66 | + require("../index.js")(publisherContainer) |
| 67 | + ); |
| 68 | + publisherContainer.register( |
| 69 | + "mainCom", |
| 70 | + class MainCom extends Component { |
| 71 | + start() {} |
| 72 | + } |
| 73 | + ); |
| 74 | + yield publisherContainer.start(); |
| 75 | + |
| 76 | + subscriberConfig.service.port = 5212; |
| 77 | + subscriberAContainer = merapi({ |
| 78 | + basepath: __dirname, |
| 79 | + config: subscriberConfig, |
| 80 | + }); |
| 81 | + |
| 82 | + subscriberAContainer.registerPlugin( |
| 83 | + "service-rabbit", |
| 84 | + require("../index.js")(subscriberAContainer) |
| 85 | + ); |
| 86 | + subscriberAContainer.register( |
| 87 | + "mainCom", |
| 88 | + class MainCom extends Component { |
| 89 | + start() {} |
| 90 | + *handleIncomingMessage(payload) { |
| 91 | + messageA.push(payload); |
| 92 | + } |
| 93 | + } |
| 94 | + ); |
| 95 | + yield subscriberAContainer.start(); |
| 96 | + |
| 97 | + service = yield subscriberAContainer.resolve("service"); |
| 98 | + connection = yield amqplib.connect(rabbitUrl); |
| 99 | + channel = yield connection.createChannel(); |
| 100 | + |
| 101 | + yield sleep(100); |
| 102 | + })); |
| 103 | + |
| 104 | + afterEach(async(function*() { |
| 105 | + yield subscriberAContainer.stop(); |
| 106 | + yield channel.close(); |
| 107 | + yield connection.close(); |
| 108 | + })); |
| 109 | + |
| 110 | + describe("Subscriber service", function() { |
| 111 | + |
| 112 | + describe("when subscribing event", function() { |
| 113 | + it("published event should be caught", async(function*() { |
| 114 | + this.timeout(5000); |
| 115 | + let trigger = yield publisherContainer.resolve("inQueuePublisherTest"); |
| 116 | + |
| 117 | + // send "0" |
| 118 | + yield sleep(100); |
| 119 | + yield trigger(0); |
| 120 | + yield sleep(1000); |
| 121 | + // messageA should be [0] |
| 122 | + expect(messageA).to.deep.equal([0]); |
| 123 | + |
| 124 | + // emulate broken connection |
| 125 | + yield channel.close(); |
| 126 | + yield connection.close(); |
| 127 | + // reconnect |
| 128 | + connection = yield amqplib.connect(rabbitUrl); |
| 129 | + channel = yield connection.createChannel(); |
| 130 | + |
| 131 | + // send "1" |
| 132 | + yield sleep(100); |
| 133 | + yield trigger(1); |
| 134 | + yield sleep(1000); |
| 135 | + // messageA should be [1] |
| 136 | + expect(messageA).to.deep.equal([0, 1]); |
| 137 | + |
| 138 | + })); |
| 139 | + }); |
| 140 | + |
| 141 | + }); |
| 142 | +}); |
0 commit comments