|
| 1 | +jest.setTimeout(30000); |
| 2 | + |
| 3 | +const { |
| 4 | + secureRandom, |
| 5 | + createConsumer, |
| 6 | + createProducer, |
| 7 | + createTopic, |
| 8 | + waitForMessages, |
| 9 | +} = require('../testhelpers'); |
| 10 | +const { ErrorCodes } = require('../../../lib').KafkaJS; |
| 11 | + |
| 12 | +describe('Producer > Transactional producer', () => { |
| 13 | + let producer, basicProducer, topicName, topicName2, transactionalId, message, consumer, groupId; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + topicName = `test-topic-${secureRandom()}`; |
| 17 | + topicName2 = `test-topic2-${secureRandom()}`; |
| 18 | + transactionalId = `transactional-id-${secureRandom()}`; |
| 19 | + message = { key: `key-${secureRandom()}`, value: `value-${secureRandom()}` }; |
| 20 | + groupId = `group-id-${secureRandom()}`; |
| 21 | + |
| 22 | + producer = createProducer({ |
| 23 | + idempotent: true, |
| 24 | + transactionalId, |
| 25 | + transactionTimeout: 1000, |
| 26 | + }); |
| 27 | + |
| 28 | + basicProducer = createProducer({}); |
| 29 | + |
| 30 | + consumer = createConsumer({ groupId, autoCommit: false, fromBeginning: true }); |
| 31 | + |
| 32 | + await createTopic({ topic: topicName, partitions: 1 }); |
| 33 | + await createTopic({ topic: topicName2 }); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + consumer && (await consumer.disconnect()); |
| 38 | + producer && (await producer.disconnect()); |
| 39 | + basicProducer && (await basicProducer.disconnect()); |
| 40 | + }); |
| 41 | + |
| 42 | + it('fails when using consumer group id while sending offsets from transactional producer', async () => { |
| 43 | + await producer.connect(); |
| 44 | + await basicProducer.connect(); |
| 45 | + await consumer.connect(); |
| 46 | + |
| 47 | + await basicProducer.send({ topic: topicName, messages: [message] }); |
| 48 | + |
| 49 | + await consumer.subscribe({ topic: topicName }); |
| 50 | + |
| 51 | + let messagesConsumed = []; |
| 52 | + await consumer.run({ |
| 53 | + eachMessage: async ({ message }) => { |
| 54 | + const transaction = await producer.transaction(); |
| 55 | + await transaction.send({ topic: topicName, messages: [message] }); |
| 56 | + |
| 57 | + await expect( |
| 58 | + transaction.sendOffsets({ consumerGroupId: groupId })).rejects.toHaveProperty('code', ErrorCodes.ERR__INVALID_ARG); |
| 59 | + await expect( |
| 60 | + transaction.sendOffsets({ consumerGroupId: groupId, consumer })).rejects.toHaveProperty('code', ErrorCodes.ERR__INVALID_ARG); |
| 61 | + |
| 62 | + await transaction.abort(); |
| 63 | + messagesConsumed.push(message); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + await waitForMessages(messagesConsumed, { number: 1 }); |
| 68 | + expect(messagesConsumed.length).toBe(1); |
| 69 | + }); |
| 70 | + |
| 71 | + it('sends offsets when transaction is committed', async () => { |
| 72 | + await producer.connect(); |
| 73 | + await basicProducer.connect(); |
| 74 | + await consumer.connect(); |
| 75 | + |
| 76 | + await basicProducer.send({ topic: topicName, messages: [message] }); |
| 77 | + |
| 78 | + await consumer.subscribe({ topic: topicName }); |
| 79 | + |
| 80 | + let messagesConsumed = []; |
| 81 | + await consumer.run({ |
| 82 | + eachMessage: async ({ topic, partition, message }) => { |
| 83 | + const transaction = await producer.transaction(); |
| 84 | + await transaction.send({ topic: topicName2, messages: [message] }); |
| 85 | + |
| 86 | + await transaction.sendOffsets({ consumer, topics: [ |
| 87 | + { |
| 88 | + topic, |
| 89 | + partitions: [ |
| 90 | + { partition, offset: Number(message.offset) + 1 }, |
| 91 | + ], |
| 92 | + } |
| 93 | + ], }); |
| 94 | + |
| 95 | + await transaction.commit(); |
| 96 | + messagesConsumed.push(message); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + await waitForMessages(messagesConsumed, { number: 1 }); |
| 101 | + expect(messagesConsumed.length).toBe(1); |
| 102 | + const committed = await consumer.committed(); |
| 103 | + expect(committed).toEqual( |
| 104 | + expect.arrayContaining([ |
| 105 | + expect.objectContaining({ |
| 106 | + topic: topicName, |
| 107 | + offset: '1', |
| 108 | + partition: 0, |
| 109 | + }), |
| 110 | + ]) |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('sends no offsets when transaction is aborted', async () => { |
| 115 | + await producer.connect(); |
| 116 | + await basicProducer.connect(); |
| 117 | + await consumer.connect(); |
| 118 | + |
| 119 | + await basicProducer.send({ topic: topicName, messages: [message] }); |
| 120 | + |
| 121 | + await consumer.subscribe({ topic: topicName }); |
| 122 | + |
| 123 | + let messagesConsumed = []; |
| 124 | + await consumer.run({ |
| 125 | + eachMessage: async ({ topic, partition, message }) => { |
| 126 | + const transaction = await producer.transaction(); |
| 127 | + await transaction.send({ topic: topicName2, messages: [message] }); |
| 128 | + |
| 129 | + await transaction.sendOffsets({ consumer, topics: [ |
| 130 | + { |
| 131 | + topic, |
| 132 | + partitions: [ |
| 133 | + { partition, offset: Number(message.offset) + 1 }, |
| 134 | + ], |
| 135 | + } |
| 136 | + ], }); |
| 137 | + |
| 138 | + await transaction.abort(); |
| 139 | + messagesConsumed.push(message); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + await waitForMessages(messagesConsumed, { number: 1 }); |
| 144 | + expect(messagesConsumed.length).toBe(1); |
| 145 | + const committed = await consumer.committed(); |
| 146 | + expect(committed).toEqual( |
| 147 | + expect.arrayContaining([ |
| 148 | + expect.objectContaining({ |
| 149 | + topic: topicName, |
| 150 | + offset: null, |
| 151 | + partition: 0, |
| 152 | + }), |
| 153 | + ]) |
| 154 | + ); |
| 155 | + }); |
| 156 | +}); |
0 commit comments