On Dec 21, 2004, at 5:31 PM, Michael Schuerig wrote:

> I'm working on a class that interacts with the Spring framework's
> application context and the Quartz scheduler. I'm pretty new to the
> latter, thus I'm testing my understanding of Quartz just as much as I'm
> testing my code. In effect, I can't (yet) build the fixture for my code
> completely with mocks as I have to ascertain what these mocks are
> supposed to do first.
>
> The complication here is that Quartz uses its own "hidden" thread(s) to
> fulfill its duties. It is such a thread that effects the change that I
> need to check for. As I don't want to test Quartz, of course, I'm
> content with checking that my scheduled job is executed at all. Thus I
> schedule it for immediate execution, wait for 2 secs to give Quartz
> some time before the fixture is torn down again.
>
> Now, all this works surprisingly well. Still, I don't like the part of
> waiting some arbitrary time.

I tried writing some learning tests for Quartz a couple months back. 
For what it's worth, I ran into the same timing issues.

Before reaching for sleep(), I tried to refactor Quartz innards so that
the internal clock could be programmatically advanced.  Unfortunately,
a direct call to System.currentTimeMillis() was duplicated in many
places and even after consolidating all those calls through an
interface I was unable to mock the clock.

I resorted to using sleep() with short schedules with the rationale
being that I was primarily trying to learn how to use the Quartz API to
schedule jobs, regardless of when those jobs ran.  Here's an example
test:

     public void testRunEverySecondForTwoSeconds() {

         CronTrigger trigger = new CronTrigger("trigger", "testGroup",
"0/1 * * * * ?");
         scheduler.scheduleJob(jobDetail, trigger);
         scheduler.start();
         sleep(2);
         assertRanXTimes(2);
         boolean waitForJobsToComplete = true;
         scheduler.shutdown(waitForJobsToComplete);
     }

     private void assertRanXTimes(int count) {
         assertEquals(count, jobDetail.getJobDataMap().getInt("count"));
         assertTrue(jobDetail.getJobDataMap().getBoolean("executed?"));
     }

Hope that helps somehow.

--
Mike Clark
Clarkware Consulting, Inc.
http://clarkware.com


Yahoo! Groups Sponsor
ADVERTISEMENT
click here


Yahoo! Groups Links

Reply via email to