Skip to content

Commit 8717c3c

Browse files
author
Eugene Leonovich
committed
Add Queue::truncate()
1 parent abbda01 commit 8717c3c

File tree

4 files changed

+69
-26
lines changed

4 files changed

+69
-26
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ $queue->put(['foo' => ['bar' => ['baz' => null]]]);
7878

7979
### Tasks
8080

81-
Almost every method of the [Queue API](src/Queue.php) (except for `kick()` and `statistics()`)
82-
returns back a [Task](src/Task.php) object containing the following getters:
81+
Most of the [Queue API](src/Queue.php) methonds return back
82+
a [Task](src/Task.php) object containing the following getters:
8383

8484
```php
8585
Task::getId()
@@ -137,16 +137,16 @@ $data = $task->getData();
137137

138138
// process $data
139139

140-
$queue->ack($task->getId());
140+
$task = $queue->ack($task->getId());
141141
```
142142

143143
Or put back into the queue in case it cannot be executed:
144144

145145
```php
146-
$queue->release($task->getId());
146+
$task = $queue->release($task->getId());
147147

148148
// for ttl-like queues you can specify a delay
149-
$queue->release($task->getId(), ['delay' => 30]);
149+
$task = $queue->release($task->getId(), ['delay' => 30]);
150150
```
151151

152152
To look at a task without changing its state, use:
@@ -158,7 +158,7 @@ $task = $queue->peek($task->getId());
158158
To bury (disable) a task:
159159

160160
```php
161-
$queue->bury($task->getId());
161+
$task = $queue->bury($task->getId());
162162
```
163163

164164
To reset buried task(s) back to `READY` state:
@@ -170,7 +170,13 @@ $count = $queue->kick(3); // kick 3 buried tasks
170170
A task (in any state) can be deleted permanently with `delete()`:
171171

172172
```php
173-
$queue->delete($task->getId());
173+
$task = $queue->delete($task->getId());
174+
```
175+
176+
To delete all tasks in a queue:
177+
178+
```php
179+
$queue->truncate();
174180
```
175181

176182
> For a detailed API documentation, please read [API](https://github.com/tarantool/queue#api)

src/Queue.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public function delete($taskId)
125125
return Task::createFromTuple($result[0]);
126126
}
127127

128+
public function truncate()
129+
{
130+
$this->client->call($this->prefix.'truncate');
131+
}
132+
128133
/**
129134
* @param string|null $path
130135
*

tests/Integration/QueueTest.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,6 @@ public function testAck()
137137
$this->assertTask($task, 0, States::DONE, 'ack_0');
138138
}
139139

140-
/**
141-
* @eval queue.tube['%tube_name%']:put('delete_0')
142-
*/
143-
public function testDelete()
144-
{
145-
$task = $this->queue->delete(0);
146-
147-
$this->assertTask($task, 0, States::DONE, 'delete_0');
148-
}
149-
150140
/**
151141
* @eval queue.tube['%tube_name%']:put('bury_0')
152142
*/
@@ -183,6 +173,38 @@ public function testKickMany()
183173
$this->assertSame(3, $count);
184174
}
185175

176+
/**
177+
* @eval queue.tube['%tube_name%']:put('delete_0')
178+
*/
179+
public function testDelete()
180+
{
181+
$task = $this->queue->delete(0);
182+
183+
$this->assertTask($task, 0, States::DONE, 'delete_0');
184+
}
185+
186+
/**
187+
* @eval queue.tube['%tube_name%']:put('truncate_0')
188+
* @eval queue.tube['%tube_name%']:put('truncate_1')
189+
*/
190+
public function testTruncate()
191+
{
192+
$this->assertSame(2, $this->queue->statistics('tasks.total'));
193+
194+
$this->queue->truncate();
195+
196+
$this->assertSame(0, $this->queue->statistics('tasks.total'));
197+
}
198+
199+
public function testTruncateEmpty()
200+
{
201+
$this->assertSame(0, $this->queue->statistics('tasks.total'));
202+
203+
$this->queue->truncate();
204+
205+
$this->assertSame(0, $this->queue->statistics('tasks.total'));
206+
}
207+
186208
/**
187209
* @eval queue.tube['%tube_name%']:put('stat_0')
188210
* @eval queue.tube['%tube_name%']:put('stat_1')

tests/Unit/QueueTest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
class QueueTest extends \PHPUnit_Framework_TestCase
1818
{
19+
const QUEUE_NAME = 'foo';
20+
1921
/**
2022
* @var \Tarantool|\PHPUnit_Framework_MockObject_MockObject
2123
*/
@@ -49,23 +51,23 @@ class QueueTest extends \PHPUnit_Framework_TestCase
4951
protected function setUp()
5052
{
5153
$this->client = $this->getMockBuilder('Tarantool')->setMethods(['call'])->getMock();
52-
$this->queue = new Queue($this->client, 'foo');
54+
$this->queue = new Queue($this->client, self::QUEUE_NAME);
5355
}
5456

5557
/**
5658
* @dataProvider provideApiMethodData
5759
*/
58-
public function testApiMethod($functionName, array $args, array $returnValue, $result)
60+
public function testApiMethod($functionName, array $args, array $returnValue, $expectedResult)
5961
{
6062
$this->client->expects($this->once())->method('call')
61-
->with("queue.tube.foo:$functionName", $args)
62-
->will($this->returnValue([$returnValue]));
63+
->with('queue.tube.'.self::QUEUE_NAME.':'.$functionName, $args)
64+
->willReturn($returnValue);
6365

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

66-
is_object($result)
67-
? $this->assertEquals($result, $actualResult)
68-
: $this->assertSame($result, $actualResult);
68+
is_object($expectedResult)
69+
? $this->assertEquals($expectedResult, $actualResult)
70+
: $this->assertSame($expectedResult, $actualResult);
6971
}
7072

7173
public function provideApiMethodData()
@@ -88,10 +90,18 @@ public function provideApiMethodData()
8890
];
8991
}
9092

93+
public function testTruncate()
94+
{
95+
$this->client->expects($this->once())->method('call')
96+
->with('queue.tube.'.self::QUEUE_NAME.':truncate');
97+
98+
$this->queue->truncate();
99+
}
100+
91101
/**
92102
* @dataProvider provideStatisticsData
93103
*/
94-
public function testStatistics(array $stats, $result, $path = null)
104+
public function testStatistics(array $stats, $expectedResult, $path = null)
95105
{
96106
$this->client->expects($this->once())->method('call')
97107
->with('queue.statistics')
@@ -101,7 +111,7 @@ public function testStatistics(array $stats, $result, $path = null)
101111
? $this->queue->statistics($path)
102112
: $this->queue->statistics();
103113

104-
$this->assertSame($result, $actualResult);
114+
$this->assertSame($expectedResult, $actualResult);
105115
}
106116

107117
public function provideStatisticsData()

0 commit comments

Comments
 (0)