~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/job/tests/test_runner.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-31 13:22:33 UTC
  • mfrom: (13785.5.4 simplify-twisted-runner-2)
  • Revision ID: launchpad@pqm.canonical.com-20110831132233-2tv7635m2ukgeru7
[r=deryck][bug=833888] Simplify Twisted job runner.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from lp.services.job.interfaces.job import (
24
24
    IRunnableJob,
25
25
    JobStatus,
 
26
    LeaseHeld,
26
27
    SuspendJobException,
27
28
    )
28
29
from lp.services.job.model.job import Job
500
501
        self.x = '*' * (10 ** 6)
501
502
 
502
503
 
 
504
class NoJobs(StaticJobSource):
 
505
 
 
506
    done = False
 
507
 
 
508
    jobs = []
 
509
 
 
510
 
 
511
class LeaseHeldJob(StaticJobSource):
 
512
 
 
513
    implements(IRunnableJob)
 
514
 
 
515
    jobs = [()]
 
516
 
 
517
    done = False
 
518
 
 
519
    def __init__(self, id):
 
520
        self.job = Job()
 
521
        self.id = id
 
522
 
 
523
    def acquireLease(self):
 
524
        raise LeaseHeld()
 
525
 
 
526
 
503
527
class TestTwistedJobRunner(ZopeTestInSubProcess, TestCaseWithFactory):
504
528
 
505
529
    layer = ZopelessDatabaseLayer
608
632
        oops = self.getOopsReport(runner, 0)
609
633
        self.assertEqual('MemoryError', oops.type)
610
634
 
 
635
    def test_no_jobs(self):
 
636
        logger = BufferLogger()
 
637
        logger.setLevel(logging.INFO)
 
638
        runner = TwistedJobRunner.runFromSource(
 
639
            NoJobs, 'branchscanner', logger)
 
640
        self.assertEqual(
 
641
            (0, 0), (len(runner.completed_jobs), len(runner.incomplete_jobs)))
 
642
 
 
643
    def test_lease_held_handled(self):
 
644
        """Jobs that raise LeaseHeld are handled correctly."""
 
645
        logger = BufferLogger()
 
646
        logger.setLevel(logging.DEBUG)
 
647
        runner = TwistedJobRunner.runFromSource(
 
648
            LeaseHeldJob, 'branchscanner', logger)
 
649
        self.assertIn('Could not acquire lease', logger.getLogBuffer())
 
650
        self.assertEqual(
 
651
            (0, 1), (len(runner.completed_jobs), len(runner.incomplete_jobs)))
 
652
 
611
653
 
612
654
class TestJobCronScript(ZopeTestInSubProcess, TestCaseWithFactory):
613
655