DarrylDeschene's avatar

Testing a horizon JobFailedListener

I have a listener that responds to the JobFailed event from Horizon and based on whether the listener that failed implemented a IsRetryableJob interface.

This is for a reporting and retry system outside of what Horizon has available.

What I have works fine during manual testing:

This issue is how to set up a test for the functionality of the check for the interface.

Any help would be appreciated.

thanks

0 likes
1 reply
LaryAI's avatar
Level 58

To test the JobFailedListener functionality, especially the part where it checks if a job implements the IsRetryableJob interface, you can use Laravel's testing capabilities. Here's a step-by-step guide on how to set up a test for this:

  1. Create a Test Class: You can use Laravel's artisan command to generate a test class if you haven't already.

    php artisan make:test JobFailedListenerTest
    
  2. Mock the Dependencies: You'll need to mock the JobFailed event and any other dependencies like the RetryableJob model and the Log facade.

  3. Write the Test: Here's an example of how you might write a test for the JobFailedListener.

  4. Run the Test: Use PHPUnit to run your test and ensure everything is working as expected.

    ./vendor/bin/phpunit --filter=JobFailedListenerTest
    

This test checks that when a job that implements the IsRetryableJob interface fails, the JobFailedListener logs the failure and creates a RetryableJob record. Adjust the mock expectations and assertions as needed to fit your actual implementation details.

Please or to participate in this conversation.