|
| 1 | +const axios = require('axios'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const nock = require('nock'); |
| 4 | +const { once } = require('events'); |
| 5 | +const amqplib = require('amqplib'); |
| 6 | +const messageQueueClient = require('../../example-application/libraries/message-queue-client'); |
| 7 | +const testHelpers = require('./test-helpers'); |
| 8 | +const orderRepository = require('../../example-application/data-access/order-repository'); |
| 9 | + |
| 10 | +const { |
| 11 | + getNextMQConfirmation, |
| 12 | + startFakeMessageQueue, |
| 13 | + getMQMessageOrTimeout, |
| 14 | + getShortUnique, |
| 15 | +} = require('./test-helpers'); |
| 16 | +const { |
| 17 | + FakeMessageQueueProvider, |
| 18 | +} = require('../../example-application/libraries/fake-message-queue-provider'); |
| 19 | + |
| 20 | +const { |
| 21 | + initializeWebServer, |
| 22 | + stopWebServer, |
| 23 | +} = require('../../example-application/entry-points/api'); |
| 24 | + |
| 25 | +let axiosAPIClient, mqClient, activeQueue; |
| 26 | + |
| 27 | +beforeAll(async (done) => { |
| 28 | + // ️️️✅ Best Practice: Place the backend under test within the same process |
| 29 | + const apiConnection = await initializeWebServer(); |
| 30 | + |
| 31 | + // ️️️✅ Best Practice: Ensure that this component is isolated by preventing unknown calls |
| 32 | + nock.disableNetConnect(); |
| 33 | + nock.enableNetConnect('127.0.0.1'); |
| 34 | + const axiosConfig = { |
| 35 | + baseURL: `http://127.0.0.1:${apiConnection.port}`, |
| 36 | + validateStatus: () => true, //Don't throw HTTP exceptions. Delegate to the tests to decide which error is acceptable |
| 37 | + }; |
| 38 | + axiosAPIClient = axios.create(axiosConfig); |
| 39 | + |
| 40 | + mqClient = new messageQueueClient(amqplib); |
| 41 | + |
| 42 | + done(); |
| 43 | +}); |
| 44 | + |
| 45 | +beforeEach(async () => { |
| 46 | + nock('http://localhost/user/').get(`/1`).reply(200, { |
| 47 | + id: 1, |
| 48 | + name: 'John', |
| 49 | + }); |
| 50 | + nock('http://mail.com').post('/send').reply(202); |
| 51 | +}); |
| 52 | + |
| 53 | +afterEach(async () => { |
| 54 | + nock.cleanAll(); |
| 55 | + sinon.restore(); |
| 56 | + console.log('After each about delete queue', activeQueue); |
| 57 | + |
| 58 | + if (activeQueue) { |
| 59 | + //await mqClient.deleteQueue(activeQueue); |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +afterAll(async (done) => { |
| 64 | + // ️️️✅ Best Practice: Clean-up resources after each run |
| 65 | + await stopWebServer(); |
| 66 | + //await messageQueueClient.close(); |
| 67 | + nock.enableNetConnect(); |
| 68 | + done(); |
| 69 | +}); |
| 70 | + |
| 71 | +// Playground |
| 72 | +test.skip('playground 2 When a message is poisoned, then its rejected and put back to queue', async () => { |
| 73 | + // Arrange |
| 74 | + const userDeletedQueueName = `user-deleted-${getShortUnique()}`; |
| 75 | + console.time('queue-creation'); |
| 76 | + await mqClient.assertQueue(userDeletedQueueName); |
| 77 | + await mqClient.bindQueue(userDeletedQueueName, 'user-events', 'user.deleted'); |
| 78 | + console.timeEnd('queue-creation'); |
| 79 | + |
| 80 | + // Act |
| 81 | + |
| 82 | + console.log('before publish'); |
| 83 | + await mqClient.publish('user-events', 'user.deleted', { |
| 84 | + invalidField: 'invalid-value', |
| 85 | + }); |
| 86 | + console.log('after publish'); |
| 87 | + |
| 88 | + // Assert |
| 89 | +}); |
| 90 | + |
| 91 | +test('When a delete message fails ONCE, than thanks to retry the order is deleted', async () => { |
| 92 | + // Arrange |
| 93 | + const orderToAdd = { |
| 94 | + userId: 1, |
| 95 | + productId: 2, |
| 96 | + mode: 'approved', |
| 97 | + }; |
| 98 | + const addedOrderId = (await axiosAPIClient.post('/order', orderToAdd)).data |
| 99 | + .id; |
| 100 | + console.time('putq'); |
| 101 | + activeQueue = `user-deleted-${getShortUnique()}`; |
| 102 | + const exchangeName = `user-events-${getShortUnique()}`; |
| 103 | + mqClient.assertExchange(exchangeName, 'topic'); |
| 104 | + await mqClient.assertQueue(activeQueue); |
| 105 | + await mqClient.bindQueue(activeQueue, exchangeName, 'user.deleted'); |
| 106 | + const mqClient2 = await testHelpers.startMQSubscriber(activeQueue); |
| 107 | + const waitForAck = once(mqClient2, 'ack'); |
| 108 | + const deleteOrderStub = sinon.stub(orderRepository.prototype, 'deleteOrder'); |
| 109 | + deleteOrderStub.onFirstCall().rejects(new Error('Cant delete order')); |
| 110 | + orderRepository.prototype.deleteOrder.callThrough(); |
| 111 | + |
| 112 | + // Act |
| 113 | + await mqClient.publish(exchangeName, 'user.deleted', { id: addedOrderId }); |
| 114 | + |
| 115 | + // Assert |
| 116 | + await waitForAck; |
| 117 | + console.timeEnd('putq'); |
| 118 | + const aQueryForDeletedOrder = await axiosAPIClient.get( |
| 119 | + `/order/${addedOrderId}` |
| 120 | + ); |
| 121 | + expect(aQueryForDeletedOrder.status).toBe(404); |
| 122 | + console.log('final', aQueryForDeletedOrder.status); |
| 123 | +}); |
0 commit comments