Panasonic Youth rob sanheim writes about software, business, ruby, music, stuff and things



Posted
10 July 2008 @ 2am

Tagged
Open Source, Rails, Ruby

Discuss

Notes on testing Bj (Background Job)

Some thoughts and random notes on testing Bj within a Rails integration test (or spec).

  • You have to turn transactions off for the scope of the test, or suffer very confusing issues, since Bj itself wraps the job submittal within a transaction. The way I did this was just overriding the use_transactional_fixtures method in the one specific spec.

    describe Foo
      def self.use_transactional_fixtures
        false
      end

  • Remember, bj = background job. This may seem obvious, but whatever you submit to bj will be running in an entirely different process, so in our spec you need to wait for that job to complete before trying to assert things. You can do something as simple as this:
    MAX_TIME = 10.0
        seconds = 0.0
        while(job.pending?) do
          job.reload
          seconds += 0.5
          sleep 0.5
          raise if seconds> MAX_TIME
        end
    # normal assertions here

    This gives your job up to 10 seconds to finish, and will timeout if it takes too long, which usually means something has gone wrong.

  • You now have to watch multiple logs to figure out what is going on. So tail your test.log and tail the bj log as well, and run the script in isolation to make sure you understand where exceptions and syntax errors will go. I wasted some time scanning logs when I really need to check the job.stderr field that bj populates, so be sure to output that for common test failures.

Overall, I've been pleased with bj, besides some open questions I've still been working out by perusing the source. Check it out if you need a easy to use persistent job queue.


2 Comments

Posted by
Tony Hillerson
16 July 2008 @ 12am

Thanks for the tip, this is good. I need to test a Background job in an integration test. Here’s what I have to turn off the transactions:

def initialize(*args)
Test::Unit::TestCase.use_transactional_fixtures = false
super(*args)
end

Should that work? I don’t like it at all because I can’t turn them off again after I’m done, but I’m not sure what I’m doing here…


Posted by
Tony Hillerson
17 July 2008 @ 12pm

One other tip is to make sure that you have blank fixtures for the Bj tables so those get cleared out before tests.

http://thillerson.blogspot.com/2008/07/use-blank-fixtures-for-bj-tables-when.html


Leave a Comment

CapGun and LogBuddy updated to 0.0.5 Quick: Find the Bug or Gotcha with named_scope