Skip to content

Commit 1b63893

Browse files
committed
* change existing tests to use subscribe
* add test that when a offset is set we use assign
1 parent d2fa178 commit 1b63893

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

‎pkg/rdkafka/RdKafkaConsumer.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct(KafkaConsumer $consumer, RdKafkaContext $context, Rd
5555
$this->topic = $topic;
5656
$this->subscribed = false;
5757
$this->commitAsync = false;
58-
58+
5959
$this->setSerializer($serializer);
6060
}
6161

@@ -97,15 +97,15 @@ public function getQueue()
9797
*/
9898
public function receive($timeout = 0)
9999
{
100-
if (false == $this->subscribed) {
101-
if ($this->offset === null) {
100+
if (false === $this->subscribed) {
101+
if (null === $this->offset) {
102102
$this->consumer->subscribe([$this->getQueue()->getQueueName()]);
103103
} else {
104104
$this->consumer->assign([new TopicPartition(
105105
$this->getQueue()->getQueueName(),
106106
$this->getQueue()->getPartition(),
107107
$this->offset
108-
), ]);
108+
)]);
109109
}
110110
$this->subscribed = true;
111111
}

‎pkg/rdkafka/Tests/RdKafkaConsumerTest.php‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testShouldReceiveFromQueueAndReturnNullIfNoMessageInQueue()
5050
$kafkaConsumer = $this->createKafkaConsumerMock();
5151
$kafkaConsumer
5252
->expects($this->once())
53-
->method('assign')
53+
->method('subscribe')
5454
;
5555
$kafkaConsumer
5656
->expects($this->once())
@@ -79,7 +79,7 @@ public function testShouldPassProperlyConfiguredTopicPartitionOnAssign()
7979
$kafkaConsumer = $this->createKafkaConsumerMock();
8080
$kafkaConsumer
8181
->expects($this->once())
82-
->method('assign')
82+
->method('subscribe')
8383
;
8484
$kafkaConsumer
8585
->expects($this->any())
@@ -106,6 +106,36 @@ public function testShouldSubscribeOnFirstReceiveOnly()
106106
$kafkaMessage = new Message();
107107
$kafkaMessage->err = RD_KAFKA_RESP_ERR__TIMED_OUT;
108108

109+
$kafkaConsumer = $this->createKafkaConsumerMock();
110+
$kafkaConsumer
111+
->expects($this->once())
112+
->method('subscribe')
113+
;
114+
$kafkaConsumer
115+
->expects($this->any())
116+
->method('consume')
117+
->willReturn($kafkaMessage)
118+
;
119+
120+
$consumer = new RdKafkaConsumer(
121+
$kafkaConsumer,
122+
$this->createContextMock(),
123+
$destination,
124+
$this->createSerializerMock()
125+
);
126+
127+
$consumer->receive(1000);
128+
$consumer->receive(1000);
129+
$consumer->receive(1000);
130+
}
131+
132+
public function testShouldAssignWhenOffsetIsSet()
133+
{
134+
$destination = new RdKafkaTopic('dest');
135+
136+
$kafkaMessage = new Message();
137+
$kafkaMessage->err = RD_KAFKA_RESP_ERR__TIMED_OUT;
138+
109139
$kafkaConsumer = $this->createKafkaConsumerMock();
110140
$kafkaConsumer
111141
->expects($this->once())
@@ -124,6 +154,8 @@ public function testShouldSubscribeOnFirstReceiveOnly()
124154
$this->createSerializerMock()
125155
);
126156

157+
$consumer->setOffset(123);
158+
127159
$consumer->receive(1000);
128160
$consumer->receive(1000);
129161
$consumer->receive(1000);
@@ -139,7 +171,7 @@ public function testThrowOnOffsetChangeAfterSubscribing()
139171
$kafkaConsumer = $this->createKafkaConsumerMock();
140172
$kafkaConsumer
141173
->expects($this->once())
142-
->method('assign')
174+
->method('subscribe')
143175
;
144176
$kafkaConsumer
145177
->expects($this->any())
@@ -174,7 +206,7 @@ public function testShouldReceiveFromQueueAndReturnMessageIfMessageInQueue()
174206
$kafkaConsumer = $this->createKafkaConsumerMock();
175207
$kafkaConsumer
176208
->expects($this->once())
177-
->method('assign')
209+
->method('subscribe')
178210
;
179211
$kafkaConsumer
180212
->expects($this->once())

0 commit comments

Comments
 (0)