mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 09:54:03 +00:00
[4.x] Queue logic refactor (#1289)
* simplify QueueTenancyBootstrapper * wip: add persistent queue bootstrapper, minor testcase refactor * ci: run persistent queue tests * simplify persistent queue bootstrapper * Fix code style (php-cs-fixer) * phpstan fixes, clarify previousTenant use * remove false positive regression test --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
0e223e0484
commit
8f958d5779
7 changed files with 216 additions and 114 deletions
|
|
@ -20,12 +20,6 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
|||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
test('sqlite ATTACH statements can be blocked', function (bool $disallow) {
|
||||
try {
|
||||
readlink(base_path('vendor'));
|
||||
} catch (\Throwable) {
|
||||
symlink(base_path('vendor'), '/var/www/html/vendor');
|
||||
}
|
||||
|
||||
if (php_uname('m') == 'aarch64') {
|
||||
// Escape testbench prison. Can't hardcode /var/www/html/extensions/... here
|
||||
// since GHA doesn't mount the filesystem on the container's workdir
|
||||
|
|
|
|||
|
|
@ -22,16 +22,12 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\PersistentQueueTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->mockConsoleOutput = false;
|
||||
|
||||
config([
|
||||
'tenancy.bootstrappers' => [
|
||||
QueueTenancyBootstrapper::class,
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
],
|
||||
'tenancy.bootstrappers' => [DatabaseTenancyBootstrapper::class],
|
||||
'queue.default' => 'redis',
|
||||
]);
|
||||
|
||||
|
|
@ -45,7 +41,22 @@ afterEach(function () {
|
|||
pest()->valuestore->flush();
|
||||
});
|
||||
|
||||
test('tenant id is passed to tenant queues', function () {
|
||||
dataset('queue_bootstrappers', [
|
||||
QueueTenancyBootstrapper::class,
|
||||
PersistentQueueTenancyBootstrapper::class,
|
||||
]);
|
||||
|
||||
function withQueueBootstrapper(string $class) {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
DatabaseTenancyBootstrapper::class,
|
||||
$class,
|
||||
]]);
|
||||
|
||||
$class::__constructStatic(app());
|
||||
}
|
||||
|
||||
test('tenant id is passed to tenant queues', function (string $bootstrapper) {
|
||||
withQueueBootstrapper($bootstrapper);
|
||||
withTenantDatabases();
|
||||
|
||||
config(['queue.default' => 'sync']);
|
||||
|
|
@ -61,9 +72,10 @@ test('tenant id is passed to tenant queues', function () {
|
|||
Event::assertDispatched(JobProcessing::class, function ($event) {
|
||||
return $event->job->payload()['tenant_id'] === tenant('id');
|
||||
});
|
||||
});
|
||||
})->with('queue_bootstrappers');
|
||||
|
||||
test('tenant id is not passed to central queues', function () {
|
||||
test('tenant id is not passed to central queues', function (string $bootstrapper) {
|
||||
withQueueBootstrapper($bootstrapper);
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
|
@ -82,9 +94,10 @@ test('tenant id is not passed to central queues', function () {
|
|||
Event::assertDispatched(JobProcessing::class, function ($event) {
|
||||
return ! isset($event->job->payload()['tenant_id']);
|
||||
});
|
||||
});
|
||||
})->with('queue_bootstrappers');
|
||||
|
||||
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
||||
test('tenancy is initialized inside queues', function (bool $shouldEndTenancy, string $bootstrapper) {
|
||||
withQueueBootstrapper($bootstrapper);
|
||||
withTenantDatabases();
|
||||
withFailedJobs();
|
||||
|
||||
|
|
@ -117,7 +130,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
|
|||
$tenant->run(function () use ($user) {
|
||||
expect($user->fresh()->name)->toBe('Bar');
|
||||
});
|
||||
})->with([true, false]);
|
||||
})->with([true, false])->with('queue_bootstrappers');
|
||||
|
||||
test('changing the shouldQueue static property in parent class affects child classes unless the property is redefined', function () {
|
||||
// Parent – $shouldQueue is true
|
||||
|
|
@ -142,7 +155,8 @@ test('changing the shouldQueue static property in parent class affects child cla
|
|||
expect(app(ShouldNotQueueListener::class)->shouldQueue(new stdClass()))->toBeFalse();
|
||||
});
|
||||
|
||||
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
|
||||
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy, string $bootstrapper) {
|
||||
withQueueBootstrapper($bootstrapper);
|
||||
withFailedJobs();
|
||||
withTenantDatabases();
|
||||
|
||||
|
|
@ -189,9 +203,10 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
|
|||
$tenant->run(function () use ($user) {
|
||||
expect($user->fresh()->name)->toBe('Bar');
|
||||
});
|
||||
})->with([true, false]);
|
||||
})->with([true, false])->with('queue_bootstrappers');
|
||||
|
||||
test('the tenant used by the job doesnt change when the current tenant changes', function () {
|
||||
test('the tenant used by the job doesnt change when the current tenant changes', function (string $bootstrapper) {
|
||||
withQueueBootstrapper($bootstrapper);
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant1 = Tenant::create();
|
||||
|
|
@ -208,26 +223,11 @@ test('the tenant used by the job doesnt change when the current tenant changes',
|
|||
pest()->artisan('queue:work --once');
|
||||
|
||||
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant1->getTenantKey());
|
||||
});
|
||||
|
||||
test('tenant connections do not persist after tenant jobs get processed', function() {
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
dispatch(new TestJob(pest()->valuestore));
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
pest()->artisan('queue:work --once');
|
||||
|
||||
expect(collect(DB::select('SHOW FULL PROCESSLIST'))->pluck('db'))->not()->toContain($tenant->database()->getName());
|
||||
});
|
||||
})->with('queue_bootstrappers');
|
||||
|
||||
// Regression test for #1277
|
||||
test('dispatching a job from a tenant run arrow function dispatches it immediately', function () {
|
||||
test('dispatching a job from a tenant run arrow function dispatches it immediately', function (string $bootstrapper) {
|
||||
withQueueBootstrapper($bootstrapper);
|
||||
withTenantDatabases();
|
||||
|
||||
$tenant = Tenant::create();
|
||||
|
|
@ -241,7 +241,7 @@ test('dispatching a job from a tenant run arrow function dispatches it immediate
|
|||
expect(tenant())->toBe(null);
|
||||
|
||||
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->getTenantKey());
|
||||
});
|
||||
})->with('queue_bootstrappers');
|
||||
|
||||
function createValueStore(): void
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
|||
use Stancl\Tenancy\Bootstrappers\UrlGeneratorBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\BroadcastChannelPrefixBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
|
||||
abstract class TestCase extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
|
|
@ -85,11 +86,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'--realpath' => true,
|
||||
]);
|
||||
|
||||
// Laravel 6.x support todo@refactor clean up
|
||||
$testResponse = class_exists('Illuminate\Testing\TestResponse') ? 'Illuminate\Testing\TestResponse' : 'Illuminate\Foundation\Testing\TestResponse';
|
||||
$testResponse::macro('assertContent', function ($content) {
|
||||
$assertClass = class_exists('Illuminate\Testing\Assert') ? 'Illuminate\Testing\Assert' : 'Illuminate\Foundation\Testing\Assert';
|
||||
$assertClass::assertSame($content, $this->baseResponse->getContent());
|
||||
\Illuminate\Testing\TestResponse::macro('assertContent', function ($content) {
|
||||
\Illuminate\Testing\Assert::assertSame($content, $this->baseResponse->getContent());
|
||||
|
||||
return $this;
|
||||
});
|
||||
|
|
@ -175,18 +173,25 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
'tenancy.models.tenant' => Tenant::class, // Use test tenant w/ DBs & domains
|
||||
]);
|
||||
|
||||
$app->singleton(RedisTenancyBootstrapper::class); // todo@samuel use proper approach eg config for singleton registration
|
||||
$app->singleton(CacheTenancyBootstrapper::class); // todo@samuel use proper approach eg config for singleton registration
|
||||
// Since we run the TSP with no bootstrappers enabled, we need
|
||||
// to manually register bootstrappers as singletons here.
|
||||
$app->singleton(RedisTenancyBootstrapper::class);
|
||||
$app->singleton(CacheTenancyBootstrapper::class);
|
||||
$app->singleton(BroadcastingConfigBootstrapper::class);
|
||||
$app->singleton(BroadcastChannelPrefixBootstrapper::class);
|
||||
$app->singleton(PostgresRLSBootstrapper::class);
|
||||
$app->singleton(MailConfigBootstrapper::class);
|
||||
$app->singleton(RootUrlBootstrapper::class);
|
||||
$app->singleton(UrlGeneratorBootstrapper::class);
|
||||
$app->singleton(FilesystemTenancyBootstrapper::class);
|
||||
}
|
||||
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
TenancyServiceProvider::$configure = function () {
|
||||
config(['tenancy.bootstrappers' => []]);
|
||||
};
|
||||
|
||||
return [
|
||||
TenancyServiceProvider::class,
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue