Skip to content

Commit 56cd5e4

Browse files
committed
kafka transport
1 parent 81bddfd commit 56cd5e4

10 files changed

Lines changed: 492 additions & 16 deletions

‎pkg/rdkafka/RdKafkaConnectionFactory.php‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function __construct($config = 'rdkafka://')
4646
$this->config = array_replace($this->defaultConfig(), $config);
4747
}
4848

49-
5049
/**
5150
* {@inheritdoc}
5251
*
@@ -83,24 +82,19 @@ private function parseDsn($dsn)
8382
throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "rdkafka" only.', $dsnConfig['scheme']));
8483
}
8584

86-
// $query = [];
87-
// if ($dsnConfig['query']) {
88-
// parse_str($dsnConfig['query'], $query);
89-
// }
90-
85+
$config = [];
86+
if ($dsnConfig['query']) {
87+
parse_str($dsnConfig['query'], $config);
88+
}
9189

9290
$broker = $dsnConfig['host'];
9391
if ($dsnConfig['port']) {
9492
$broker .= ':'.$dsnConfig['port'];
9593
}
9694

97-
return [
98-
'global' => [
99-
'group.id' => uniqid('', true),
100-
'metadata.broker.list' => $broker,
101-
'auto.offset.reset' => 'largest',
102-
],
103-
];
95+
$config['global']['metadata.broker.list'] = $broker;
96+
97+
return array_replace_recursive($this->defaultConfig(), $config);
10498
}
10599

106100
/**
@@ -110,8 +104,8 @@ private function defaultConfig()
110104
{
111105
return [
112106
'global' => [
113-
'metadata.broker.list' => 'localhost:9092',
114107
'group.id' => uniqid('', true),
108+
'metadata.broker.list' => 'localhost:9092',
115109
],
116110
];
117111
}

‎pkg/rdkafka/RdKafkaConsumer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function receive($timeout = 0)
9898
*/
9999
public function receiveNoWait()
100100
{
101-
return $this->receive(1000);
101+
return $this->receive(10);
102102
}
103103

104104
/**

‎pkg/rdkafka/RdKafkaTopic.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
namespace Enqueue\RdKafka;
33

4+
use Interop\Queue\PsrQueue;
45
use Interop\Queue\PsrTopic;
56
use RdKafka\TopicConf;
67

7-
class RdKafkaTopic implements PsrTopic
8+
class RdKafkaTopic implements PsrTopic, PsrQueue
89
{
910
/**
1011
* @var string
@@ -43,6 +44,14 @@ public function getTopicName()
4344
return $this->name;
4445
}
4546

47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function getQueueName()
51+
{
52+
return $this->name;
53+
}
54+
4655
/**
4756
* @return TopicConf
4857
*/
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
namespace Enqueue\RdKafka\Tests;
3+
4+
use Enqueue\RdKafka\RdKafkaConnectionFactory;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class RdKafkaConnectionFactoryTest extends TestCase
8+
{
9+
public function testThrowNeitherArrayStringNorNullGivenAsConfig()
10+
{
11+
$this->expectException(\LogicException::class);
12+
$this->expectExceptionMessage('The config must be either an array of options, a DSN string or null');
13+
14+
new RdKafkaConnectionFactory(new \stdClass());
15+
}
16+
17+
public function testThrowIfSchemeIsNotBeanstalkAmqp()
18+
{
19+
$this->expectException(\LogicException::class);
20+
$this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "rdkafka" only.');
21+
22+
new RdKafkaConnectionFactory('http://example.com');
23+
}
24+
25+
public function testThrowIfDsnCouldNotBeParsed()
26+
{
27+
$this->expectException(\LogicException::class);
28+
$this->expectExceptionMessage('Failed to parse DSN "rdkafka://:@/"');
29+
30+
new RdKafkaConnectionFactory('rdkafka://:@/');
31+
}
32+
33+
public function testShouldBeExpectedDefaultConfig()
34+
{
35+
$factory = new RdKafkaConnectionFactory(null);
36+
37+
$config = $this->getObjectAttribute($factory, 'config');
38+
39+
$this->assertNotEmpty($config['global']['group.id']);
40+
41+
$config['global']['group.id'] = 'group-id';
42+
$this->assertSame([
43+
'global' => [
44+
'group.id' => 'group-id',
45+
'metadata.broker.list' => 'localhost:9092',
46+
]
47+
], $config);
48+
}
49+
50+
public function testShouldBeExpectedDefaultDsnConfig()
51+
{
52+
$factory = new RdKafkaConnectionFactory('rdkafka://');
53+
54+
$config = $this->getObjectAttribute($factory, 'config');
55+
56+
$this->assertNotEmpty($config['global']['group.id']);
57+
58+
$config['global']['group.id'] = 'group-id';
59+
$this->assertSame([
60+
'global' => [
61+
'group.id' => 'group-id',
62+
'metadata.broker.list' => 'localhost:9092',
63+
]
64+
], $config);
65+
}
66+
67+
/**
68+
* @dataProvider provideConfigs
69+
*
70+
* @param mixed $config
71+
* @param mixed $expectedConfig
72+
*/
73+
public function testShouldParseConfigurationAsExpected($config, $expectedConfig)
74+
{
75+
$factory = new RdKafkaConnectionFactory($config);
76+
77+
$this->assertAttributeEquals($expectedConfig, 'config', $factory);
78+
}
79+
80+
public static function provideConfigs()
81+
{
82+
yield [
83+
'rdkafka://theHost:1234?global%5Bgroup.id%5D=group-id',
84+
[
85+
'global' => [
86+
'metadata.broker.list' => 'theHost:1234',
87+
'group.id' => 'group-id',
88+
]
89+
],
90+
];
91+
92+
yield [
93+
[
94+
'global' => [
95+
'metadata.broker.list' => 'theHost:1234',
96+
'group.id' => 'group-id',
97+
]
98+
],
99+
[
100+
'global' => [
101+
'metadata.broker.list' => 'theHost:1234',
102+
'group.id' => 'group-id',
103+
]
104+
],
105+
];
106+
}
107+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
namespace Enqueue\RdKafka\Tests;
3+
4+
use Enqueue\RdKafka\RdKafkaConsumer;
5+
use Enqueue\RdKafka\RdKafkaContext;
6+
use Enqueue\RdKafka\RdKafkaMessage;
7+
use Enqueue\RdKafka\RdKafkaTopic;
8+
use PHPUnit\Framework\TestCase;
9+
use RdKafka\KafkaConsumer;
10+
use RdKafka\Message;
11+
12+
class RdKafkaConsumerTest extends TestCase
13+
{
14+
public function testCouldBeConstructedWithRequiredArguments()
15+
{
16+
new RdKafkaConsumer($this->createKafkaConsumerMock(), $this->createContextMock(), new RdKafkaTopic(''));
17+
}
18+
19+
public function testShouldReturnQueueSetInConstructor()
20+
{
21+
$destination = new RdKafkaTopic('');
22+
23+
$consumer = new RdKafkaConsumer($this->createKafkaConsumerMock(), $this->createContextMock(), $destination);
24+
25+
$this->assertSame($destination, $consumer->getQueue());
26+
}
27+
28+
public function testShouldReceiveFromQueueAndReturnNullIfNoMessageInQueue()
29+
{
30+
$destination = new RdKafkaTopic('dest');
31+
32+
$kafkaMessage = new Message();
33+
$kafkaMessage->err = RD_KAFKA_RESP_ERR__TIMED_OUT;
34+
35+
$kafkaConsumer = $this->createKafkaConsumerMock();
36+
$kafkaConsumer
37+
->expects($this->once())
38+
->method('subscribe')
39+
->with(['dest'])
40+
;
41+
$kafkaConsumer
42+
->expects($this->once())
43+
->method('consume')
44+
->with(1000)
45+
->willReturn($kafkaMessage)
46+
;
47+
$kafkaConsumer
48+
->expects($this->once())
49+
->method('unsubscribe')
50+
;
51+
52+
$consumer = new RdKafkaConsumer($kafkaConsumer, $this->createContextMock(), $destination);
53+
54+
$this->assertNull($consumer->receive(1000));
55+
}
56+
57+
public function testShouldReceiveFromQueueAndReturnMessageIfMessageInQueue()
58+
{
59+
$destination = new RdKafkaTopic('dest');
60+
61+
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
62+
63+
$kafkaMessage = new Message();
64+
$kafkaMessage->err = RD_KAFKA_RESP_ERR_NO_ERROR;
65+
$kafkaMessage->payload = json_encode($message);
66+
67+
$kafkaConsumer = $this->createKafkaConsumerMock();
68+
$kafkaConsumer
69+
->expects($this->once())
70+
->method('subscribe')
71+
->with(['dest'])
72+
;
73+
$kafkaConsumer
74+
->expects($this->once())
75+
->method('consume')
76+
->with(1000)
77+
->willReturn($kafkaMessage)
78+
;
79+
$kafkaConsumer
80+
->expects($this->once())
81+
->method('unsubscribe')
82+
;
83+
84+
$consumer = new RdKafkaConsumer($kafkaConsumer, $this->createContextMock(), $destination);
85+
86+
$actualMessage = $consumer->receive(1000);
87+
88+
$this->assertSame('theBody', $actualMessage->getBody());
89+
$this->assertSame(['foo' => 'fooVal'], $actualMessage->getProperties());
90+
$this->assertSame(['bar' => 'barVal'], $actualMessage->getHeaders());
91+
$this->assertSame($kafkaMessage, $actualMessage->getKafkaMessage());
92+
}
93+
94+
public function testShouldReceiveNoWaitFromQueueAndReturnNullIfNoMessageInQueue()
95+
{
96+
$destination = new RdKafkaTopic('dest');
97+
98+
$kafkaMessage = new Message();
99+
$kafkaMessage->err = RD_KAFKA_RESP_ERR__TIMED_OUT;
100+
101+
$kafkaConsumer = $this->createKafkaConsumerMock();
102+
$kafkaConsumer
103+
->expects($this->once())
104+
->method('subscribe')
105+
->with(['dest'])
106+
;
107+
$kafkaConsumer
108+
->expects($this->once())
109+
->method('consume')
110+
->with(10)
111+
->willReturn($kafkaMessage)
112+
;
113+
$kafkaConsumer
114+
->expects($this->once())
115+
->method('unsubscribe')
116+
;
117+
118+
$consumer = new RdKafkaConsumer($kafkaConsumer, $this->createContextMock(), $destination);
119+
120+
$this->assertNull($consumer->receiveNoWait());
121+
}
122+
123+
public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue()
124+
{
125+
$destination = new RdKafkaTopic('dest');
126+
127+
$message = new RdKafkaMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
128+
129+
$kafkaMessage = new Message();
130+
$kafkaMessage->err = RD_KAFKA_RESP_ERR_NO_ERROR;
131+
$kafkaMessage->payload = json_encode($message);
132+
133+
$kafkaConsumer = $this->createKafkaConsumerMock();
134+
$kafkaConsumer
135+
->expects($this->once())
136+
->method('subscribe')
137+
->with(['dest'])
138+
;
139+
$kafkaConsumer
140+
->expects($this->once())
141+
->method('consume')
142+
->with(10)
143+
->willReturn($kafkaMessage)
144+
;
145+
$kafkaConsumer
146+
->expects($this->once())
147+
->method('unsubscribe')
148+
;
149+
150+
$consumer = new RdKafkaConsumer($kafkaConsumer, $this->createContextMock(), $destination);
151+
152+
$actualMessage = $consumer->receiveNoWait();
153+
154+
$this->assertSame('theBody', $actualMessage->getBody());
155+
$this->assertSame(['foo' => 'fooVal'], $actualMessage->getProperties());
156+
$this->assertSame(['bar' => 'barVal'], $actualMessage->getHeaders());
157+
$this->assertSame($kafkaMessage, $actualMessage->getKafkaMessage());
158+
}
159+
160+
/**
161+
* @return \PHPUnit_Framework_MockObject_MockObject|KafkaConsumer
162+
*/
163+
private function createKafkaConsumerMock()
164+
{
165+
return $this->createMock(KafkaConsumer::class);
166+
}
167+
168+
/**
169+
* @return \PHPUnit_Framework_MockObject_MockObject|RdKafkaContext
170+
*/
171+
private function createContextMock()
172+
{
173+
return $this->createMock(RdKafkaContext::class);
174+
}
175+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace Enqueue\RdKafka\Tests;
3+
4+
use Enqueue\Null\NullQueue;
5+
use Enqueue\RdKafka\RdKafkaContext;
6+
use Interop\Queue\InvalidDestinationException;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class RdKafkaContextTest extends TestCase
10+
{
11+
public function testThrowNotImplementedOnCreateTemporaryQueue()
12+
{
13+
$context = new RdKafkaContext([]);
14+
15+
$this->expectException(\LogicException::class);
16+
$this->expectExceptionMessage('Not implemented');
17+
$context->createTemporaryQueue();
18+
}
19+
20+
public function testThrowInvalidDestinationIfInvalidDestinationGivenOnCreateConsumer()
21+
{
22+
$context = new RdKafkaContext([]);
23+
24+
$this->expectException(InvalidDestinationException::class);
25+
$context->createConsumer(new NullQueue('aQueue'));
26+
}
27+
}

0 commit comments

Comments
 (0)