views:

309

answers:

3

I want to test how priorities are working in the delayed_job plugin. Im using the mailit app from railscasts. I think i want to send 100 messages with a high priority and 100 with a lower priority. And i want to see if the messages with a lower priority will be delivered on time or they will be put aside.

How can i do a test like this.

A: 

If you are interested in finding out how fast those mails would reach real recipients out there, you can just do one thing: Send those mails. Without actually going through the full delivery process, there is absolutely no way to tell how many of those mails will get delayed due to greylisting, malfunctioning MXs, congested MXs, broken content scanners (or worse, broken content scanners in SMTP proxy mode), broken internet connection on the receiving servers and so on.

cite
+1  A: 

Creating such a test shouldn't be a huge problem. Generate 100 emails with high priority, then 100 with low. Or vice versa. Don't start your workers until you've checked the database to see that every job is there and accounted for. Start youR workers and watch how the jobs are being executed--and in the right order. Remember that priority in d_j is ascending, so priority 1 is higher than 10.

Now, 200 jobs is not a lot. Your workers will likely execute them all fairly quickly. If you truly want to test whether your low priority emails still get sent, you'll likely have to depend on what else you've got in your queue--provided that you use d_j for more than just email. I'd advise to populate the queue with at least a couple of thousands of jobs--or whatever you can envision the "worst case scenario" being--and running the test with that. If non-email jobs have a higher priority than the low-priority emails, they're more likely to affect the email delivery than the 100 high-priority ones.

vonconrad
A: 

Hi,

I actually implemented the test to test DelayedJob processing sending emails. The test is actually pretty simple.

You should enqueue 2 jobs (instead of 100), one with high priority, and one with lower priority. You then can use the "Delayed::Job.work_off" method to execute the first job, then assert that the lower priority job is still in the db waiting. If you are still unsure about how to execute a job, take a look at the library. The code is pretty well written.

Here's the code excerpt from my application. Basically I need to send out reminder emails to users, so I need to make sure that the jobs are executed properly and the mailer doesn't bomb out. I put this test in the reminder_test.rb unit test file since the Reminder model knows all about how to enqueue and send out emails.

# enqueue the jobs here

assert_difference 'Delayed::Job.count', -1, 'Job should execute successfully' do
  assert_difference 'ActionMailer::Base.deliveries.count' do
    Delayed::Job.work_off
  end 
end

# make sure the email was properly delivered
email = ActionMailer::Base.deliveries.last
assert_equal email.to[0], @user.email
assert (Time.now - @reminder.reload.sent_at) < 1.seconds

Cheers! Hope this help

Alex

Alex Le