|
| 1 | +'use strict'; |
| 2 | +const expect = require('chai').expect; |
| 3 | +const publish = require('../lib/actions/publish'); |
| 4 | +const consume = require('../lib/triggers/consume'); |
| 5 | +const EventEmitter = require('events'); |
| 6 | +const co = require('co'); |
| 7 | + |
| 8 | +class TestEmitter extends EventEmitter { |
| 9 | + |
| 10 | + constructor(done) { |
| 11 | + super(); |
| 12 | + this.data = []; |
| 13 | + this.end = 0; |
| 14 | + this.error = []; |
| 15 | + |
| 16 | + this.on('data', (value) => this.data.push(value)); |
| 17 | + this.on('error', (value) => this.error.push(value)); |
| 18 | + this.on('end', () => { |
| 19 | + this.end++; |
| 20 | + done(); |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +describe('Integration test', () => { |
| 27 | + |
| 28 | + |
| 29 | + before(() => { |
| 30 | + if (!process.env.AMQP_URL) throw new Error("Please set AMQP_URL env variable to proceed"); |
| 31 | + if (!process.env.ELASTICIO_MESSAGE_CRYPTO_IV) throw new Error("Please set ELASTICIO_MESSAGE_CRYPTO_IV env " + |
| 32 | + "variable to proceed"); |
| 33 | + if (!process.env.ELASTICIO_MESSAGE_CRYPTO_PASSWORD) throw new Error("Please set " + |
| 34 | + "ELASTICIO_MESSAGE_CRYPTO_PASSWORD env variable to proceed"); |
| 35 | + |
| 36 | + }); |
| 37 | + |
| 38 | + describe('subscribe then publish', () => { |
| 39 | + const cfg = { |
| 40 | + amqpURI : process.env.AMQP_URL, |
| 41 | + topic: 'integartion-testing-' + (process.env.TRAVIS_BUILD_ID || 'local'), |
| 42 | + bindingKeys: 'foo.bar' |
| 43 | + }; |
| 44 | + |
| 45 | + before(() => publish.init(cfg).then(consume.init(cfg))); |
| 46 | + |
| 47 | + |
| 48 | + it('send and receive', () => co(function* gen() { |
| 49 | + console.log('Starting test'); |
| 50 | + const receiver = new TestEmitter(); |
| 51 | + const sender = new TestEmitter(); |
| 52 | + const msg1 = { |
| 53 | + id: 'one', |
| 54 | + body: { |
| 55 | + routingKey: 'foo.bar', |
| 56 | + payload: { |
| 57 | + value: 'foo.bar' |
| 58 | + } |
| 59 | + }, |
| 60 | + attachments: { |
| 61 | + one: 'http://one.com' |
| 62 | + } |
| 63 | + }; |
| 64 | + const msg2 = { |
| 65 | + id: 'two', |
| 66 | + body: { |
| 67 | + routingKey: 'foo.baz', |
| 68 | + payload: { |
| 69 | + value: 'foo.baz' |
| 70 | + } |
| 71 | + }, |
| 72 | + attachments: { |
| 73 | + two: 'http://two.com' |
| 74 | + } |
| 75 | + }; |
| 76 | + console.log('Initializing receiver'); |
| 77 | + yield consume.process.call(receiver, {}, cfg); |
| 78 | + yield new Promise((ok) => setTimeout(ok, 1000)); |
| 79 | + console.log('Sending messages'); |
| 80 | + const out1 = yield publish.process.call(sender, msg1, cfg); |
| 81 | + const out2 = yield publish.process.call(sender, msg2, cfg); |
| 82 | + expect(out1).deep.equal(msg1); |
| 83 | + expect(out2).deep.equal(msg2); |
| 84 | + console.log('Sending completed, now wait'); |
| 85 | + yield new Promise((ok) => setTimeout(ok, 1000)); |
| 86 | + console.log('Lets check'); |
| 87 | + expect(receiver.data.length).equal(1); |
| 88 | + expect(receiver.data[0]).deep.equal({ |
| 89 | + id: 'one', |
| 90 | + body: { |
| 91 | + value: 'foo.bar' |
| 92 | + }, |
| 93 | + attachments: { |
| 94 | + one: 'http://one.com' |
| 95 | + }, |
| 96 | + headers: {}, |
| 97 | + metadata: {} |
| 98 | + }); |
| 99 | + })).timeout(5000); |
| 100 | + }); |
| 101 | + |
| 102 | +}); |
0 commit comments