Skip to content

Commit

Permalink
Add Queue::truncate()
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Leonovich committed Jun 30, 2016
1 parent abbda01 commit 8717c3c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ $queue->put(['foo' => ['bar' => ['baz' => null]]]);

### Tasks

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

```php
Task::getId()
Expand Down Expand Up @@ -137,16 +137,16 @@ $data = $task->getData();

// process $data

$queue->ack($task->getId());
$task = $queue->ack($task->getId());
```

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

```php
$queue->release($task->getId());
$task = $queue->release($task->getId());

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

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

```php
$queue->bury($task->getId());
$task = $queue->bury($task->getId());
```

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

```php
$queue->delete($task->getId());
$task = $queue->delete($task->getId());
```

To delete all tasks in a queue:

```php
$queue->truncate();
```

> For a detailed API documentation, please read [API](https://github.com/tarantool/queue#api)
Expand Down
5 changes: 5 additions & 0 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public function delete($taskId)
return Task::createFromTuple($result[0]);
}

public function truncate()
{
$this->client->call($this->prefix.'truncate');
}

/**
* @param string|null $path
*
Expand Down
42 changes: 32 additions & 10 deletions tests/Integration/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ public function testAck()
$this->assertTask($task, 0, States::DONE, 'ack_0');
}

/**
* @eval queue.tube['%tube_name%']:put('delete_0')
*/
public function testDelete()
{
$task = $this->queue->delete(0);

$this->assertTask($task, 0, States::DONE, 'delete_0');
}

/**
* @eval queue.tube['%tube_name%']:put('bury_0')
*/
Expand Down Expand Up @@ -183,6 +173,38 @@ public function testKickMany()
$this->assertSame(3, $count);
}

/**
* @eval queue.tube['%tube_name%']:put('delete_0')
*/
public function testDelete()
{
$task = $this->queue->delete(0);

$this->assertTask($task, 0, States::DONE, 'delete_0');
}

/**
* @eval queue.tube['%tube_name%']:put('truncate_0')
* @eval queue.tube['%tube_name%']:put('truncate_1')
*/
public function testTruncate()
{
$this->assertSame(2, $this->queue->statistics('tasks.total'));

$this->queue->truncate();

$this->assertSame(0, $this->queue->statistics('tasks.total'));
}

public function testTruncateEmpty()
{
$this->assertSame(0, $this->queue->statistics('tasks.total'));

$this->queue->truncate();

$this->assertSame(0, $this->queue->statistics('tasks.total'));
}

/**
* @eval queue.tube['%tube_name%']:put('stat_0')
* @eval queue.tube['%tube_name%']:put('stat_1')
Expand Down
28 changes: 19 additions & 9 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class QueueTest extends \PHPUnit_Framework_TestCase
{
const QUEUE_NAME = 'foo';

/**
* @var \Tarantool|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -49,23 +51,23 @@ class QueueTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->client = $this->getMockBuilder('Tarantool')->setMethods(['call'])->getMock();
$this->queue = new Queue($this->client, 'foo');
$this->queue = new Queue($this->client, self::QUEUE_NAME);
}

/**
* @dataProvider provideApiMethodData
*/
public function testApiMethod($functionName, array $args, array $returnValue, $result)
public function testApiMethod($functionName, array $args, array $returnValue, $expectedResult)
{
$this->client->expects($this->once())->method('call')
->with("queue.tube.foo:$functionName", $args)
->will($this->returnValue([$returnValue]));
->with('queue.tube.'.self::QUEUE_NAME.':'.$functionName, $args)
->willReturn($returnValue);

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

is_object($result)
? $this->assertEquals($result, $actualResult)
: $this->assertSame($result, $actualResult);
is_object($expectedResult)
? $this->assertEquals($expectedResult, $actualResult)
: $this->assertSame($expectedResult, $actualResult);
}

public function provideApiMethodData()
Expand All @@ -88,10 +90,18 @@ public function provideApiMethodData()
];
}

public function testTruncate()
{
$this->client->expects($this->once())->method('call')
->with('queue.tube.'.self::QUEUE_NAME.':truncate');

$this->queue->truncate();
}

/**
* @dataProvider provideStatisticsData
*/
public function testStatistics(array $stats, $result, $path = null)
public function testStatistics(array $stats, $expectedResult, $path = null)
{
$this->client->expects($this->once())->method('call')
->with('queue.statistics')
Expand All @@ -101,7 +111,7 @@ public function testStatistics(array $stats, $result, $path = null)
? $this->queue->statistics($path)
: $this->queue->statistics();

$this->assertSame($result, $actualResult);
$this->assertSame($expectedResult, $actualResult);
}

public function provideStatisticsData()
Expand Down

0 comments on commit 8717c3c

Please sign in to comment.