Skip to content

Commit fc448df

Browse files
author
Eugene Leonovich
committed
Add Queue:statistics()
1 parent 7d275ea commit fc448df

File tree

4 files changed

+121
-11
lines changed

4 files changed

+121
-11
lines changed

src/Queue.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
class Queue
66
{
77
private $client;
8+
private $tubeName;
89
private $prefix;
910

1011
public function __construct(\Tarantool $client, $tubeName)
1112
{
1213
$this->client = $client;
14+
$this->tubeName = $tubeName;
1315
$this->prefix = "queue.tube.$tubeName:";
1416
}
1517

@@ -28,7 +30,7 @@ public function put($data, array $options = null)
2830
}
2931

3032
/**
31-
* @param float|null $timeout
33+
* @param int|float|null $timeout
3234
*
3335
* @return Task|null
3436
*/
@@ -113,4 +115,26 @@ public function delete($taskId)
113115

114116
return Task::createFromTuple($result[0]);
115117
}
118+
119+
/**
120+
* @return array|int|null
121+
*/
122+
public function statistics(/* ... */)
123+
{
124+
$result = $this->client->call('queue.statistics', [$this->tubeName]);
125+
126+
if (empty($result[0][0])) {
127+
return;
128+
}
129+
130+
$result = $result[0][0];
131+
foreach (func_get_args() as $arg) {
132+
if (!isset($result[$arg])) {
133+
return;
134+
}
135+
$result = $result[$arg];
136+
}
137+
138+
return $result;
139+
}
116140
}

tests/Integration/QueueTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Tarantool\Queue\Queue;
66
use Tarantool\Queue\States;
7-
use Tarantool\Queue\Task;
87

98
abstract class QueueTest extends \PHPUnit_Framework_TestCase
109
{
@@ -174,6 +173,47 @@ public function testKickMany()
174173
$this->assertSame(3, $count);
175174
}
176175

176+
/**
177+
* @eval queue.tube['%tube_name%']:put('stat_0')
178+
* @eval queue.tube['%tube_name%']:put('stat_1')
179+
* @eval queue.tube['%tube_name%']:put('stat_2')
180+
* @eval queue.tube['%tube_name%']:put('stat_3')
181+
* @eval queue.tube['%tube_name%']:put('stat_4')
182+
* @eval queue.tube['%tube_name%']:delete(4)
183+
* @eval queue.tube['%tube_name%']:take(.001)
184+
* @eval queue.tube['%tube_name%']:release(0)
185+
* @eval queue.tube['%tube_name%']:take(.001)
186+
* @eval queue.tube['%tube_name%']:ack(0)
187+
* @eval queue.tube['%tube_name%']:bury(1)
188+
* @eval queue.tube['%tube_name%']:bury(2)
189+
* @eval queue.tube['%tube_name%']:kick(1)
190+
* @eval queue.tube['%tube_name%']:take(.001)
191+
*/
192+
public function testStatistics()
193+
{
194+
$stat = $this->queue->statistics();
195+
196+
$this->assertEquals([
197+
'tasks' => [
198+
'taken' => 1,
199+
'buried' => 1,
200+
'ready' => 1,
201+
'done' => 2,
202+
'delayed' => 0,
203+
'total' => 3,
204+
],
205+
'calls' => [
206+
'ack' => 1,
207+
'delete' => 1,
208+
'take' => 3,
209+
'kick' => 1,
210+
'release' => 1,
211+
'put' => 5,
212+
'bury' => 2,
213+
],
214+
], $stat, '', 0.0, 3, true);
215+
}
216+
177217
/**
178218
* @dataProvider provideFailureCallbackData
179219
* @expectedException \Exception

tests/Integration/Ttl.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
trait Ttl
66
{
77
/**
8-
* @eval queue.tube['%tube_name%']:put('ttr_1', { ttr = 1})
8+
* @eval queue.tube['%tube_name%']:put('ttr_1', {ttr = 1})
99
*/
1010
public function testTimeToRun()
1111
{
@@ -19,7 +19,7 @@ public function testTimeToRun()
1919
}
2020

2121
/**
22-
* @eval queue.tube['%tube_name%']:put('ttl_1', { ttl = 1})
22+
* @eval queue.tube['%tube_name%']:put('ttl_1', {ttl = 1})
2323
*/
2424
public function testTimeToLive()
2525
{
@@ -30,8 +30,8 @@ public function testTimeToLive()
3030
}
3131

3232
/**
33-
* @eval queue.tube['%tube_name%']:put('pri_low', { pri = 2})
34-
* @eval queue.tube['%tube_name%']:put('pri_high', { pri = 1})
33+
* @eval queue.tube['%tube_name%']:put('pri_low', {pri = 2})
34+
* @eval queue.tube['%tube_name%']:put('pri_high', {pri = 1})
3535
*/
3636
public function testPriority()
3737
{
@@ -46,7 +46,7 @@ public function testPriority()
4646
}
4747

4848
/**
49-
* @eval queue.tube['%tube_name%']:put('delay_1', { delay = 1})
49+
* @eval queue.tube['%tube_name%']:put('delay_1', {delay = 1})
5050
*/
5151
public function testDelay()
5252
{
@@ -60,4 +60,15 @@ public function testDelay()
6060
$this->assertTaskInstance($task);
6161
$this->assertSame('delay_1', $task->getData());
6262
}
63+
64+
/**
65+
* @eval queue.tube['%tube_name%']:put('stat_delayed_0', {delay = 9999})
66+
* @eval queue.tube['%tube_name%']:put('stat_delayed_1', {delay = 9999})
67+
*/
68+
public function testStatisticsDelayed()
69+
{
70+
$count = $this->queue->statistics('tasks', 'delayed');
71+
72+
$this->assertSame(2, $count);
73+
}
6374
}

tests/Unit/QueueTest.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ protected function setUp()
1717
}
1818

1919
/**
20-
* @dataProvider provideCallData
20+
* @dataProvider provideApiMethodData
2121
*/
22-
public function testMethod($functionName, array $args, array $tuple, $result)
22+
public function testApiMethod($functionName, array $args, array $returnValue, $result)
2323
{
2424
$this->client->expects($this->once())->method('call')
2525
->with("queue.tube.foo:$functionName", $args)
26-
->will($this->returnValue([$tuple]));
26+
->will($this->returnValue([$returnValue]));
2727

2828
$actualResult = call_user_func_array([$this->queue, $functionName], $args);
2929

@@ -32,7 +32,7 @@ public function testMethod($functionName, array $args, array $tuple, $result)
3232
: $this->assertSame($result, $actualResult);
3333
}
3434

35-
public function provideCallData()
35+
public function provideApiMethodData()
3636
{
3737
$tuple = [1, 'x', 42];
3838
$task = Task::createFromTuple($tuple);
@@ -51,4 +51,39 @@ public function provideCallData()
5151
['delete', [1], $tuple, $task],
5252
];
5353
}
54+
55+
/**
56+
* @dataProvider provideStatisticsData
57+
*/
58+
public function testStatistics(array $args, array $returnValue, $result)
59+
{
60+
$this->client->expects($this->once())->method('call')
61+
->with('queue.statistics')
62+
->will($this->returnValue([$returnValue]));
63+
64+
$actualResult = call_user_func_array([$this->queue, 'statistics'], $args);
65+
66+
$this->assertSame($result, $actualResult);
67+
}
68+
69+
public function provideStatisticsData()
70+
{
71+
$stats = ['tasks' => ['ready' => 1, 'done' => 0], 'calls' => ['put' => 3]];
72+
73+
return [
74+
[[], [$stats], $stats],
75+
[['tasks'], [$stats], $stats['tasks']],
76+
[['tasks', 'ready'], [$stats], $stats['tasks']['ready']],
77+
[['tasks', 'done'], [$stats], $stats['tasks']['done']],
78+
[['calls'], [$stats], $stats['calls']],
79+
[['calls', 'put'], [$stats], $stats['calls']['put']],
80+
[[null], [$stats], null],
81+
[[null, null], [$stats], null],
82+
[[''], [$stats], null],
83+
[['foo'], [$stats], null],
84+
[['tasks', 'foo'], [$stats], null],
85+
[[null, 'tasks'], [$stats], null],
86+
[['tasks', ''], [$stats], null],
87+
];
88+
}
5489
}

0 commit comments

Comments
 (0)