ShouldBeUnique causing jobs to fail tests #40233
Replies: 5 comments
-
I made one of my jobs |
Beta Was this translation helpful? Give feedback.
-
Here's a way to remove lock for jobs in fake queue: foreach (Queue::pushedJobs() as $list) {
foreach ($list as $item) {
if ($item['job'] instanceof \Illuminate\Contracts\Queue\ShouldBeUnique) {
$cache = $this->app->make(\Illuminate\Contracts\Cache\Repository::class);
(new \Illuminate\Bus\UniqueLock($cache))->release($item['job']);
}
}
} this however is not very useful. More useful would be to remove all existing locks. Note that Here's another one (but only for redis) $db = \cache()->getStore()->lockConnection();
$prefix = $db->getOptions()->prefix?->getPrefix();
foreach ($db->keys('*') as $key) {
if (strpos($key, 'laravel_unique_job') !== false) {
$db->del($prefix ? substr($key, strlen($prefix)) : $key);
}
} |
Beta Was this translation helpful? Give feedback.
-
Did anyone find a good solutions for this? I'm having the same issue here @alecpl @ngunyimacharia ? |
Beta Was this translation helpful? Give feedback.
-
@rubentebogt I've been using this little workaround in my jobs that implement public function uniqueId(): string
{
return app()->environment('testing') ? microtime() : $this->model->uuid;
} |
Beta Was this translation helpful? Give feedback.
-
In my case I overrode the cache driver to be array for the specific test, since the actual job functionality is tested separately, this is only asserting it gets queued. |
Beta Was this translation helpful? Give feedback.
-
When running tests in Laravel, after mocking the queue using
Queue::fake();
, Laravel will still try to acquire the unique lock for the job. This causes failure of jobs as the lock is only released when the job is complete which never happens.When the job with the same
uniqueId
is dispatched again the lock is not acquired thus the test isn'tpushed
to the Queue ordispatched
to theBus
.Is this the ideal behavior and is there a suggested workaround?
Beta Was this translation helpful? Give feedback.
All reactions