diff options
Diffstat (limited to 'plugins/jobs/oooold/tests/Jobs')
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Amqp/BrokerTest.php | 20 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Amqp/ErrorJob.php | 22 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Amqp/Job.php | 26 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/BaseTest.php | 115 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Beanstalk/BrokerTest.php | 20 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Beanstalk/ErrorJob.php | 22 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Beanstalk/Job.php | 26 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Local/BrokerTest.php | 20 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Local/ErrorJob.php | 22 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Local/Job.php | 26 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/OptionsTest.php | 34 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/RegistryTest.php | 43 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/ShortCircuitTest.php | 90 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Sqs/BrokerTest.php | 20 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Sqs/ErrorJob.php | 22 | ||||
-rw-r--r-- | plugins/jobs/oooold/tests/Jobs/Sqs/Job.php | 26 |
16 files changed, 554 insertions, 0 deletions
diff --git a/plugins/jobs/oooold/tests/Jobs/Amqp/BrokerTest.php b/plugins/jobs/oooold/tests/Jobs/Amqp/BrokerTest.php new file mode 100644 index 00000000..637c14d6 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Amqp/BrokerTest.php @@ -0,0 +1,20 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Amqp; + +use Spiral\Jobs\Tests\BaseTest; + +class BrokerTest extends BaseTest +{ + public const JOB = Job::class; + public const ERROR_JOB = ErrorJob::class; +} diff --git a/plugins/jobs/oooold/tests/Jobs/Amqp/ErrorJob.php b/plugins/jobs/oooold/tests/Jobs/Amqp/ErrorJob.php new file mode 100644 index 00000000..82b6e7e0 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Amqp/ErrorJob.php @@ -0,0 +1,22 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Amqp; + +use Spiral\Jobs\JobHandler; + +class ErrorJob extends JobHandler +{ + public function invoke(string $id): void + { + throw new \Error('something is wrong'); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Amqp/Job.php b/plugins/jobs/oooold/tests/Jobs/Amqp/Job.php new file mode 100644 index 00000000..2c6ad819 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Amqp/Job.php @@ -0,0 +1,26 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Amqp; + +use Spiral\Jobs\JobHandler; + +class Job extends JobHandler +{ + public const JOB_FILE = __DIR__ . '/../../local.job'; + + public function invoke(string $id, array $payload): void + { + file_put_contents(self::JOB_FILE, json_encode( + $payload + compact('id') + )); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/BaseTest.php b/plugins/jobs/oooold/tests/Jobs/BaseTest.php new file mode 100644 index 00000000..67f280b5 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/BaseTest.php @@ -0,0 +1,115 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests; + +use PHPUnit\Framework\TestCase; +use Spiral\Core\Container; +use Spiral\Goridge\RPC; +use Spiral\Goridge\SocketRelay; +use Spiral\Jobs\Options; +use Spiral\Jobs\Queue; +use Spiral\Jobs\Registry\ContainerRegistry; + +abstract class BaseTest extends TestCase +{ + public const JOB = null; + public const ERROR_JOB = null; + + private $job; + private $errorJob; + + public function setUp(): void + { + $this->job = static::JOB; + $this->errorJob = static::ERROR_JOB; + } + + protected function tearDown(): void + { + if (file_exists((static::JOB)::JOB_FILE)) { + unlink((static::JOB)::JOB_FILE); + } + } + + public function testJob(): void + { + $jobs = $this->makeJobs(); + + $id = $jobs->push($this->job, ['data' => 100]); + + $this->assertNotEmpty($id); + + $this->waitForJob(); + $this->assertFileExists($this->job::JOB_FILE); + + $data = json_decode(file_get_contents($this->job::JOB_FILE), true); + $this->assertSame($id, $data['id']); + $this->assertSame(100, $data['data']); + } + + public function testErrorJob(): void + { + $jobs = $this->makeJobs(); + + $id = $jobs->push($this->errorJob, ['data' => 100]); + $this->assertNotEmpty($id); + } + + public function testDelayJob(): void + { + $jobs = $this->makeJobs(); + + $id = $jobs->push($this->job, ['data' => 100], Options::delayed(1)); + + $this->assertNotEmpty($id); + + $this->assertTrue($this->waitForJob() > 1); + $this->assertFileExists($this->job::JOB_FILE); + + $data = json_decode(file_get_contents($this->job::JOB_FILE), true); + $this->assertSame($id, $data['id']); + $this->assertSame(100, $data['data']); + } + + /** + * @expectedException \Spiral\Jobs\Exception\JobException + */ + public function testConnectionException(): void + { + $jobs = new Queue( + new RPC(new SocketRelay('localhost', 6002)), + new ContainerRegistry(new Container()) + ); + + $jobs->push($this->job, ['data' => 100]); + } + + public function makeJobs(): Queue + { + return new Queue( + new RPC(new SocketRelay('localhost', 6001)), + new ContainerRegistry(new Container()) + ); + } + + private function waitForJob(): float + { + $start = microtime(true); + $try = 0; + while (!file_exists($this->job::JOB_FILE) && $try < 10) { + usleep(250000); + $try++; + } + + return microtime(true) - $start; + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Beanstalk/BrokerTest.php b/plugins/jobs/oooold/tests/Jobs/Beanstalk/BrokerTest.php new file mode 100644 index 00000000..d1ea4682 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Beanstalk/BrokerTest.php @@ -0,0 +1,20 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Beanstalk; + +use Spiral\Jobs\Tests\BaseTest; + +class BrokerTest extends BaseTest +{ + public const JOB = Job::class; + public const ERROR_JOB = ErrorJob::class; +} diff --git a/plugins/jobs/oooold/tests/Jobs/Beanstalk/ErrorJob.php b/plugins/jobs/oooold/tests/Jobs/Beanstalk/ErrorJob.php new file mode 100644 index 00000000..c4349871 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Beanstalk/ErrorJob.php @@ -0,0 +1,22 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Beanstalk; + +use Spiral\Jobs\JobHandler; + +class ErrorJob extends JobHandler +{ + public function invoke(string $id): void + { + throw new \Error('something is wrong'); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Beanstalk/Job.php b/plugins/jobs/oooold/tests/Jobs/Beanstalk/Job.php new file mode 100644 index 00000000..f8bd541a --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Beanstalk/Job.php @@ -0,0 +1,26 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Beanstalk; + +use Spiral\Jobs\JobHandler; + +class Job extends JobHandler +{ + public const JOB_FILE = __DIR__ . '/../../local.job'; + + public function invoke(string $id, array $payload): void + { + file_put_contents(self::JOB_FILE, json_encode( + $payload + compact('id') + )); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Local/BrokerTest.php b/plugins/jobs/oooold/tests/Jobs/Local/BrokerTest.php new file mode 100644 index 00000000..9ba83de6 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Local/BrokerTest.php @@ -0,0 +1,20 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Local; + +use Spiral\Jobs\Tests\BaseTest; + +class BrokerTest extends BaseTest +{ + public const JOB = Job::class; + public const ERROR_JOB = ErrorJob::class; +} diff --git a/plugins/jobs/oooold/tests/Jobs/Local/ErrorJob.php b/plugins/jobs/oooold/tests/Jobs/Local/ErrorJob.php new file mode 100644 index 00000000..70b1365b --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Local/ErrorJob.php @@ -0,0 +1,22 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Local; + +use Spiral\Jobs\JobHandler; + +class ErrorJob extends JobHandler +{ + public function invoke(string $id): void + { + throw new \Error('something is wrong'); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Local/Job.php b/plugins/jobs/oooold/tests/Jobs/Local/Job.php new file mode 100644 index 00000000..2f5803c8 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Local/Job.php @@ -0,0 +1,26 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Local; + +use Spiral\Jobs\JobHandler; + +class Job extends JobHandler +{ + public const JOB_FILE = __DIR__ . '/../../local.job'; + + public function invoke(string $id, array $payload): void + { + file_put_contents(self::JOB_FILE, json_encode( + $payload + compact('id') + )); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/OptionsTest.php b/plugins/jobs/oooold/tests/Jobs/OptionsTest.php new file mode 100644 index 00000000..5d00794e --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/OptionsTest.php @@ -0,0 +1,34 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests; + +use PHPUnit\Framework\TestCase; +use Spiral\Jobs\Options; + +class OptionsTest extends TestCase +{ + public function testDelay(): void + { + $o = new Options(); + $this->assertNull($o->getDelay()); + $o = $o->withDelay(10); + $this->assertSame(10, $o->getDelay()); + } + + public function testPipeline(): void + { + $o = new Options(); + $this->assertNull($o->getPipeline()); + $o = $o->withPipeline('custom'); + $this->assertSame('custom', $o->getPipeline()); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/RegistryTest.php b/plugins/jobs/oooold/tests/Jobs/RegistryTest.php new file mode 100644 index 00000000..7abd75f7 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/RegistryTest.php @@ -0,0 +1,43 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests; + +use PHPUnit\Framework\TestCase; +use Spiral\Core\Container; +use Spiral\Jobs\Registry\ContainerRegistry; +use Spiral\Jobs\Tests\Local\Job; + +class RegistryTest extends TestCase +{ + public function testMakeJob(): void + { + $factory = new ContainerRegistry(new Container()); + + $j = $factory->getHandler('spiral.jobs.tests.local.job'); + $this->assertInstanceOf(Job::class, $j); + + $this->assertSame(json_encode(['data' => 200]), $j->serialize( + 'spiral.jobs.tests.local.job', + ['data' => 200] + )); + } + + /** + * @expectedException \Spiral\Jobs\Exception\JobException + */ + public function testMakeUndefined(): void + { + $factory = new ContainerRegistry(new Container()); + + $factory->getHandler('spiral.jobs.undefined'); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/ShortCircuitTest.php b/plugins/jobs/oooold/tests/Jobs/ShortCircuitTest.php new file mode 100644 index 00000000..c3306385 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/ShortCircuitTest.php @@ -0,0 +1,90 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests; + +use PHPUnit\Framework\TestCase; +use Spiral\Core\Container; +use Spiral\Jobs\Options; +use Spiral\Jobs\Registry\ContainerRegistry; +use Spiral\Jobs\ShortCircuit; +use Spiral\Jobs\Tests\Local\ErrorJob; +use Spiral\Jobs\Tests\Local\Job; + +class ShortCircuitTest extends TestCase +{ + protected function tearDown(): void + { + if (file_exists(Job::JOB_FILE)) { + unlink(Job::JOB_FILE); + } + } + + public function testLocal(): void + { + $c = new ContainerRegistry(new Container()); + $jobs = new ShortCircuit($c, $c); + + $id = $jobs->push(Job::class, ['data' => 100]); + + $this->assertNotEmpty($id); + + $this->assertFileExists(Job::JOB_FILE); + + $data = json_decode(file_get_contents(Job::JOB_FILE), true); + $this->assertSame($id, $data['id']); + $this->assertSame(100, $data['data']); + } + + public function testLocalDelayed(): void + { + $c = new ContainerRegistry(new Container()); + $jobs = new ShortCircuit($c, $c); + + $t = microtime(true); + $id = $jobs->push(Job::class, ['data' => 100], Options::delayed(1)); + + $this->assertTrue(microtime(true) - $t >= 1); + + $this->assertNotEmpty($id); + + $this->assertFileExists(Job::JOB_FILE); + + $data = json_decode(file_get_contents(Job::JOB_FILE), true); + $this->assertSame($id, $data['id']); + $this->assertSame(100, $data['data']); + } + + /** + * @expectedException \Spiral\Jobs\Exception\JobException + */ + public function testError(): void + { + $c = new ContainerRegistry(new Container()); + $jobs = new ShortCircuit($c, $c); + $jobs->push(ErrorJob::class); + } + + public function testLocalDelay(): void + { + $c = new ContainerRegistry(new Container()); + $jobs = new ShortCircuit($c, $c); + + $id = $jobs->push(Job::class, ['data' => 100], Options::delayed(1)); + $this->assertNotEmpty($id); + + $this->assertFileExists(Job::JOB_FILE); + + $data = json_decode(file_get_contents(Job::JOB_FILE), true); + $this->assertSame($id, $data['id']); + $this->assertSame(100, $data['data']); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Sqs/BrokerTest.php b/plugins/jobs/oooold/tests/Jobs/Sqs/BrokerTest.php new file mode 100644 index 00000000..ccaa96de --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Sqs/BrokerTest.php @@ -0,0 +1,20 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Sqs; + +use Spiral\Jobs\Tests\BaseTest; + +class BrokerTest extends BaseTest +{ + public const JOB = Job::class; + public const ERROR_JOB = ErrorJob::class; +} diff --git a/plugins/jobs/oooold/tests/Jobs/Sqs/ErrorJob.php b/plugins/jobs/oooold/tests/Jobs/Sqs/ErrorJob.php new file mode 100644 index 00000000..738b9f2b --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Sqs/ErrorJob.php @@ -0,0 +1,22 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Sqs; + +use Spiral\Jobs\JobHandler; + +class ErrorJob extends JobHandler +{ + public function invoke(string $id): void + { + throw new \Error('something is wrong'); + } +} diff --git a/plugins/jobs/oooold/tests/Jobs/Sqs/Job.php b/plugins/jobs/oooold/tests/Jobs/Sqs/Job.php new file mode 100644 index 00000000..e22483a8 --- /dev/null +++ b/plugins/jobs/oooold/tests/Jobs/Sqs/Job.php @@ -0,0 +1,26 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ + +declare(strict_types=1); + +namespace Spiral\Jobs\Tests\Sqs; + +use Spiral\Jobs\JobHandler; + +class Job extends JobHandler +{ + public const JOB_FILE = __DIR__ . '/../../local.job'; + + public function invoke(string $id, array $payload): void + { + file_put_contents(self::JOB_FILE, json_encode( + $payload + compact('id') + )); + } +} |