1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 00:14:04 +00:00

Close tenant connections after jobs (#1128)

* Test that the context is central after jobs get processed

* Revert to central context when jobs get processed, delete `$previousTenant` logic

* Test that the tenant connections get closed after jobs get processed

* Bring back the previous tenant logic

* Adapt tests to the previous tenant logic

* Delete assertion
This commit is contained in:
lukinovec 2023-08-18 07:38:07 +02:00 committed by GitHub
parent bd9bbe8b41
commit 9d6356aea4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 7 deletions

View file

@ -110,6 +110,8 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
pest()->artisan('queue:work --once');
expect(! tenancy()->initialized)->toBe($shouldEndTenancy);
expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id);
@ -117,7 +119,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]);
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
withFailedJobs();
@ -144,12 +146,21 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
pest()->artisan('queue:work --once');
expect(! tenancy()->initialized)->toBe($shouldEndTenancy);
expect(DB::connection('central')->table('failed_jobs')->count())->toBe(1);
expect(pest()->valuestore->get('tenant_id'))->toBeNull(); // job failed
pest()->artisan('queue:retry all');
if ($shouldEndTenancy) {
tenancy()->end();
}
pest()->artisan('queue:work --once');
expect(! tenancy()->initialized)->toBe($shouldEndTenancy);
expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id); // job succeeded
@ -182,6 +193,22 @@ test('the tenant used by the job doesnt change when the current tenant changes',
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme');
});
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());
});
function createValueStore(): void
{
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';