Beanstalkd for jobs

This commit is contained in:
Juan Pablo Vial
2025-07-14 14:29:41 -04:00
parent e6e7470bb2
commit 501151a90e
26 changed files with 693 additions and 390 deletions

View File

@ -0,0 +1,101 @@
<?php
namespace Incoviba\Test\Service\MQTT;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use xobotyi\beansclient\BeansClient;
use xobotyi\beansclient\Connection;
use xobotyi\beansclient\Exception\JobException;
use xobotyi\beansclient\Job;
use Incoviba\Exception\MQTT\MissingJob;
use Incoviba\Service\MQTT\Beanstalkd;
class BeanstalkdTest extends TestCase
{
protected LoggerInterface $logger;
protected BeansClient $client;
protected function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->client = $this->getMockBuilder(BeansClient::class)->disableOriginalConstructor()->getMock();
}
public function testExists(): void
{
$stats = ['current-jobs-ready' => 1];
$this->client->method('watchTube')->willReturn($this->client);
$this->client->method('statsTube')->willReturn($stats);
$service = new Beanstalkd($this->logger, $this->client);
$this->assertTrue($service->exists());
}
public function testNotExists(): void
{
$stats = ['current-jobs-ready' => 0];
$this->client->method('watchTube')->willReturn($this->client);
$this->client->method('statsTube')->willReturn($stats);
$service = new Beanstalkd($this->logger, $this->client);
$this->assertFalse($service->exists());
}
public function testGet(): void
{
$jobData = [
'id' => 1,
'configuration' => [
'type' => 'service',
],
'created_at' => '2020-01-01 00:00:00',
];
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$connection->method('isActive')->willReturn(true);
$this->client->method('getConnection')->willReturn($connection);
$this->client->method('watchTube')->willReturn($this->client);
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 1]);
$job = new Job($this->client, 1, 'ready', json_encode($jobData));
$this->client->method('reserve')->willReturn($job);
$service = new Beanstalkd($this->logger, $this->client);
$this->assertEquals(json_encode($jobData), $service->get());
}
public function testGetException(): void
{
$this->client->method('watchTube')->willReturn($this->client);
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 0]);
$service = new Beanstalkd($this->logger, $this->client);
$this->expectException(MissingJob::class);
$service->get();
$this->client->method('statsTube')->willReturn(['current-jobs-ready' => 1]);
$exception = new JobException();
$this->client->method('reserve')->willThrowException($exception);
$this->expectException(MissingJob::class);
$service->get();
}
public function testSet(): void
{
$this->client->method('useTube')->willReturn($this->client);
$this->client->method('put');
$service = new Beanstalkd($this->logger, $this->client);
$service->set('test');
$this->assertTrue(true);
}
public function testSetException(): void
{
$this->client->method('useTube')->willReturn($this->client);
$exception = new JobException();
$this->client->method('put')->willThrowException($exception);
$service = new Beanstalkd($this->logger, $this->client);
$service->set('test');
$this->assertTrue(true);
}
public function testRemove(): void
{
$this->client->method('useTube')->willReturn($this->client);
$this->client->method('delete')->willReturn(true);
$service = new Beanstalkd($this->logger, $this->client);
$service->remove(1);
$this->assertTrue(true);
$this->client->method('delete')->willReturn(false);
$service->remove(1);
$this->assertTrue(true);
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Incoviba\Test\Service;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use xobotyi\beansclient\BeansClient;
use Incoviba\Exception\MQTT\MissingClient;
use Incoviba\Service\MQTT;
use Incoviba\Service\MQTT\Beanstalkd;
class MQTTTest extends TestCase
{
public function testRegisterAndClientExistsAndGet(): void
{
$mqtt = new MQTT();
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
$mqtt->register('beanstalkd', $beanstalkd);
$this->assertTrue($mqtt->clientExists('beanstalkd'));
}
public function testGetClient(): void
{
$mqtt = new MQTT();
$logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
$client = $this->getMockBuilder(BeansClient::class)->disableOriginalConstructor()->getMock();
$beanstalkd = new Beanstalkd($logger, $client);
$mqtt->register('beanstalkd', $beanstalkd);
$this->assertEquals($beanstalkd, $mqtt->getClient('beanstalkd'));
}
public function testGetClientException(): void
{
$mqtt = new MQTT();
$this->expectException(MissingClient::class);
$mqtt->getClient('test');
}
public function testExists(): void
{
$mqtt = new MQTT();
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
$beanstalkd->method('exists')->willReturn(true);
$mqtt->register('beanstalkd', $beanstalkd);
$this->assertTrue($mqtt->exists('beanstalkd'));
}
public function testGet(): void
{
$mqtt = new MQTT();
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
$beanstalkd->method('get')->willReturn('test');
$mqtt->register('beanstalkd', $beanstalkd);
$this->assertEquals('test', $mqtt->get('beanstalkd'));
}
public function testSet(): void
{
$mqtt = new MQTT();
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
$beanstalkd->method('set');
$mqtt->register('beanstalkd', $beanstalkd);
$mqtt->set('test', 0, 'beanstalkd');
$this->assertTrue(true);
}
public function testRemove(): void
{
$mqtt = new MQTT();
$beanstalkd = $this->getMockBuilder(Beanstalkd::class)->disableOriginalConstructor()->getMock();
$beanstalkd->method('remove');
$mqtt->register('beanstalkd', $beanstalkd);
$mqtt->remove(0, 'beanstalkd');
$this->assertTrue(true);
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace Incoviba\Test\Service;
use Psr\Log\LoggerInterface;
use PHPUnit\Framework\TestCase;
use Incoviba\Service\Job;
use Incoviba\Service\Queue;
use Incoviba\Service\Worker;
use Incoviba\Model;
class QueueTest extends TestCase
{
protected LoggerInterface $logger;
protected Job $jobService;
protected Worker $defaultWorker;
protected function setUp(): void
{
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->jobService = $this->getMockBuilder(Job::class)
->disableOriginalConstructor()
->getMock();
$this->defaultWorker = $this->getMockBuilder(Worker::class)
->disableOriginalConstructor()
->getMock();
}
public function testRegister(): void
{
$queue = new Queue($this->logger, $this->jobService, $this->defaultWorker);
$worker = $this->getMockBuilder(Worker::class)
->disableOriginalConstructor()
->getMock();
$queue->register('test', $worker);
$this->assertTrue(true);
}
public function testEnqueue(): void
{
$queue = new Queue($this->logger, $this->jobService, $this->defaultWorker);
$jobData = ['test' => 'test'];
$result = $queue->enqueue($jobData);
$this->assertTrue($result);
$result = $queue->push($jobData);
$this->assertTrue($result);
}
public function testRun(): void
{
$queue = new Queue($this->logger, $this->jobService, $this->defaultWorker);
$result = $queue->run();
$this->assertTrue($result);
$jobData = [
'type' => 'test',
];
$job = new Model\Job();
$job->id = 1;
$job->configuration = $jobData;
$this->jobService->method('isPending')->willReturn(true);
$this->jobService->method('get')->willReturn($job);
$result = $queue->run();
$this->assertTrue($result);
}
}