Skip to content

Commit

Permalink
Add tests for BatchQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewaite committed Apr 7, 2017
1 parent 21e78ed commit e14dff9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 10 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1

cache:
directories:
- $HOME/.composer/cache

install: travis_retry composer install

script: vendor/bin/phpunit tests
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## Laravel Queue for AWS Batch

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]

This package is currently built out for Laravel 5.1 as most of my work requires use of LTS builds. Support will be coming for 5.4.

### Usage
Expand Down Expand Up @@ -68,3 +73,10 @@ this requirement can be relaxed later.
our runner, and the DB queue is just backing it to ship the jobs more easily, we don't have an easy work around. If you
require delayed jobs for your use case, at this point my recommendation would be to use a regular DB queue, and to fire
a job into it which will fire your batch job at the correct time.

[ico-version]: https://img.shields.io/packagist/v/lukewaite/laravel-queue-aws-batch.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/lukewaite/laravel-queue-aws-batch/master.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/lukewaite/laravel-queue-aws-batch
[link-travis]: https://travis-ci.org/lukewaite/laravel-queue-aws-batch
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
}
},
"require": {
"illuminate/support": ">5.1 <5.2",
"illuminate/support": "~5.1.0",
"aws/aws-sdk-php": "^3.20.6"
},
"require-dev": {
"orchestra/testbench": "3.1.7",
"phpunit/phpunit": "~4.0",
"mockery/mockery": "^0.9.6",
"laravel/framework": "~5.1"
"laravel/framework": "~5.1.0",
"phpunit/phpunit": "~5.7"
}
}
27 changes: 21 additions & 6 deletions src/Queues/BatchQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,51 @@ public function __construct(

public function push($job, $data = '', $queue = null)
{
return $this->pushToBatch($queue, $this->createPayload($job, $data),
str_replace('\\', '_', (string) get_class($job)));
$payload = $this->createPayload($job, $data);
return $this->pushToBatch($queue, $payload, $this->getDisplayName($job));
}

public function pushRaw($payload, $queue = null, array $options = [])
{
return $this->pushToBatch($queue, $payload, 'raw-job');
}

/**
* Get the display name for the given job.
*
* @param mixed $job
* @return string
*/
protected function getDisplayName($job)
{
if (is_object($job)) {
return method_exists($job, 'displayName')
? $job->displayName() : get_class($job);
} else {
return is_string($job) ? explode('@', $job)[0] : null;
}
}

/**
* Push a raw payload to the database, then to AWS Batch, with a given delay.
*
* @param string|null $queue
* @param string $payload
* @param string $jobName
* @param int $attempts
*
* @return mixed
*/
public function pushToBatch($queue, $payload, $jobName, $attempts = 0)
protected function pushToBatch($queue, $payload, $jobName)
{
$jobId = $this->pushToDatabase(0, $queue, $payload, $attempts);
$jobId = $this->pushToDatabase(0, $queue, $payload);

return $this->batch->submitJob([
'jobDefinition' => $this->jobDefinition,
'jobName' => $jobName,
'jobQueue' => $this->getQueue($queue),
'parameters' => [
'jobId' => $jobId,
],
]
]);
}

Expand Down
104 changes: 104 additions & 0 deletions tests/BatchQueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

use Mockery as m;
use PHPUnit\Framework\TestCase;

class BatchQueueTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testPushProperlyPushesJobOntoDatabase()
{
/** @var \LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue $queue */
$queue = $this->getMockBuilder('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue')->setMethods(null)->setConstructorArgs([
$database = m::mock('Illuminate\Database\Connection'),
'table',
'default',
'60',
'jobdefinition',
$batch = m::mock('Aws\Batch\BatchClient')
])->getMock();

$database->shouldReceive('table')->with('table')->andReturn($query = m::mock('StdClass'));

$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) {
$this->assertEquals('default', $array['queue']);
$this->assertNotNull($array['payload'], 'Payload is not set');
$this->assertEquals(['data'], json_decode($array['payload'], 1)['data']);
$this->assertEquals('foo', json_decode($array['payload'], 1)['job']);
$this->assertEquals(0, $array['attempts']);
$this->assertNull($array['reserved_at']);
$this->assertInternalType('int', $array['available_at']);

return 100;
});

$batch->shouldReceive('submitJob')->once()->andReturnUsing(function ($array) {
$this->assertEquals('jobdefinition', $array['jobDefinition']);
$this->assertEquals('foo', $array['jobName']);
$this->assertEquals('default', $array['jobQueue']);
$this->assertEquals(['jobId'=>100], $array['parameters']);
});

$queue->push('foo', ['data']);
}

public function testGetJobById()
{
/** @var \LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue $queue */
$queue = $this->getMockBuilder('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue')->setMethods(null)->setConstructorArgs([
$database = m::mock('Illuminate\Database\Connection'),
'table',
'default',
'60',
'jobdefinition',
$batch = m::mock('Aws\Batch\BatchClient')
])->getMock();

$database->shouldReceive('table')->once()->with('table')->andReturn($query = m::mock('StdClass'));
$query->shouldReceive('where')->once()->with('id', 1)->andReturn($results = m::mock('StdClass'));
$results->shouldReceive('first')->once()->andReturn($queryResult = m::mock('StdClass'));
$queryResult->attempts = 0;

$queue->setContainer(m::mock('Illuminate\Container\Container'));

$queue->getJobById(1, 'default');
}

public function testPopThrowsException()
{
$this->expectException('LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException');

/** @var \LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue $queue */
$queue = $this->getMockBuilder('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue')->setMethods(null)->setConstructorArgs([
$database = m::mock('Illuminate\Database\Connection'),
'table',
'default',
'60',
'jobdefinition',
$batch = m::mock('Aws\Batch\BatchClient')
])->getMock();

$queue->pop('default');
}

public function testLaterThrowsException()
{
$this->expectException('LukeWaite\LaravelQueueAwsBatch\Exceptions\UnsupportedException');

/** @var \LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue $queue */
$queue = $this->getMockBuilder('LukeWaite\LaravelQueueAwsBatch\Queues\BatchQueue')->setMethods(null)->setConstructorArgs([
$database = m::mock('Illuminate\Database\Connection'),
'table',
'default',
'60',
'jobdefinition',
$batch = m::mock('Aws\Batch\BatchClient')
])->getMock();

$queue->later(10,'default');
}
}

0 comments on commit e14dff9

Please sign in to comment.