|
| 1 | +--TEST-- |
| 2 | +Produce, consume |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +require __DIR__ . '/integration-tests-check.php'; |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | +require __DIR__ . '/integration-tests-check.php'; |
| 9 | + |
| 10 | +$conf = new SimpleKafkaClient\Configuration(); |
| 11 | +$conf->set('metadata.broker.list', getenv('TEST_KAFKA_BROKERS')); |
| 12 | + |
| 13 | +$conf->setDrMsgCb(function (SimpleKafkaClient\Producer $kafka, SimpleKafkaClient\Message $message, $opaque) { |
| 14 | + if (RD_KAFKA_RESP_ERR_NO_ERROR !== $message->err) { |
| 15 | + $errorStr = rd_kafka_err2str($message->err); |
| 16 | + |
| 17 | + echo sprintf('Message FAILED (%s, %s) to send with payload => %s', $message->err, $errorStr, $message->payload) . PHP_EOL; |
| 18 | + } else { |
| 19 | + if (false === is_string($opaque)) { |
| 20 | + $opaque = 'opaque was already freed'; |
| 21 | + } |
| 22 | + |
| 23 | + echo sprintf('Message opaque: %s', $opaque) . PHP_EOL; |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +$producer = new SimpleKafkaClient\Producer($conf); |
| 28 | +$topic = $producer->getTopicHandle('pure-php-test-topic'); |
| 29 | +$amountTestMessages = 10; |
| 30 | + |
| 31 | +for ($i = 0; $i < $amountTestMessages; ++$i) { |
| 32 | + $topic->producev( |
| 33 | + RD_KAFKA_PARTITION_UA, |
| 34 | + RD_KAFKA_MSG_F_BLOCK, // will block produce if queue is full |
| 35 | + sprintf('test message-%d',$i), |
| 36 | + sprintf('test-key-%d', $i), |
| 37 | + [ |
| 38 | + 'some' => sprintf('header value %d', $i) |
| 39 | + ], |
| 40 | + null, |
| 41 | + "opaque $i" |
| 42 | + ); |
| 43 | + |
| 44 | + $producer->poll(0); |
| 45 | +} |
| 46 | + |
| 47 | +$result = $producer->flush(20000); |
| 48 | +if (RD_KAFKA_RESP_ERR_NO_ERROR !== $result) { |
| 49 | + echo 'Was not able to shutdown within 20s. Messages might be lost!' . PHP_EOL; |
| 50 | +} |
| 51 | +--EXPECT-- |
| 52 | +Message key test-key-0 and opaque: opaque 0 |
| 53 | +Message key test-key-1 and opaque: opaque 1 |
| 54 | +Message key test-key-2 and opaque: opaque 2 |
| 55 | +Message key test-key-3 and opaque: opaque 3 |
| 56 | +Message key test-key-4 and opaque: opaque 4 |
| 57 | +Message key test-key-5 and opaque: opaque 5 |
| 58 | +Message key test-key-6 and opaque: opaque 6 |
| 59 | +Message key test-key-7 and opaque: opaque 7 |
| 60 | +Message key test-key-8 and opaque: opaque 8 |
| 61 | +Message key test-key-9 and opaque: opaque 9 |
0 commit comments