Beispiel #1
0
    def put(self, jobarg, priority=0):
        """
        Add a job to the queue.

        @param jobarg: The argument that will be passed to self._func when
        this job is launched.

        @param priority: The priority for the job. Lower means more
        important.  Default is zero.

        @raise QueueStopped: if the queue has been stopped.

        @return: a C{Deferred} that will eventually fire with the result of
        calling self._func on jobarg.
        """
        if self.stopped:
            raise QueueStopped()
        else:
            job = Job(jobarg, priority)
            print 'made job. state is ', job.state
            d = job.watch().addBoth(self._jobDone, job)
            print 'called watch'
            print 'made job. state is ', job.state
            self._queue.put(job, priority)
            return d
Beispiel #2
0
 def testLaunchLaunchFails(self):
     """
     Launch a job twice and make sure the second attempt gets a
     C{RuntimeError}.
     """
     job = Job(None, 0)
     job.launch(lambda x: x)
     self.assertRaises(RuntimeError, job.launch, lambda x: x)
Beispiel #3
0
 def put(self, jobarg, priority=0):
     if self.stopped:
         raise QueueStopped()
     else:
         job = Job(jobarg, priority)
         jobarg.job = job
         d = job.watch().addBoth(self._jobDone, job)
         self._queue.put(job, priority)
         return d    
Beispiel #4
0
    def testWatchLaunchSucceedCheckResult(self):
        """
        Watch a job, launch it, and check the C{Deferred} receives a L{Job}
        instance whose result is correct.
        """

        def cb(result):
            self.assertEqual(result.result, 42)

        job = Job(6, 0)
        deferred = job.watch()
        deferred.addCallback(cb)
        job.launch(lambda x: x * 7)
        return deferred
Beispiel #5
0
    def testLaunchSucceedWatch(self):
        """
        Launch a job that succeeds, then watch it and check the C{Deferred}
        receives a L{Job} instance whose state is FINISHED.
        """

        def cb(result):
            self.assertEqual(result.state, Job.FINISHED)

        job = Job(None, 0)
        job.launch(lambda x: x)
        deferred = job.watch()
        deferred.addCallback(cb)
        return deferred
Beispiel #6
0
    def testWatchLaunchSucceedCancelDeferred(self):
        """
        Watch a job that is launched and succeeds, and then call cancel on
        the watching C{Deferred}.  The C{Deferred} will receive a Job instance
        whose state is FINISHED (i.e., the cancel will have no effect).
        """

        def cb(result):
            self.assertEqual(result.state, Job.FINISHED)

        job = Job(None, 0)
        deferred = job.watch()
        deferred.addCallback(cb)
        job.launch(lambda x: x)
        deferred.cancel()
        return deferred
Beispiel #7
0
    def testWatchCancelDeferred(self):
        """
        Watch a job and check that when we cancel the watching C{Deferred}
        before the job has been launched the watcher gets an errback with a
        L{Job} instance whose state is CANCELLED.
        """

        def cb(result):
            self.fail('Unexpectedly succeeded!')

        def eb(failure):
            self.assertTrue(isinstance(failure.value, Job))
            self.assertEqual(failure.value.state, Job.CANCELLED)

        job = Job(None, 0)
        deferred = job.watch()
        deferred.addCallbacks(cb, eb)
        deferred.cancel()
        return deferred
Beispiel #8
0
    def testWatchLaunchFail(self):
        """
        Watch a job that launches and fails. The L{Job} instance in the
        failure must be in the FAILED state. Check the underlying failure
        is as expected (in this case a ZeroDivisionError).
        """

        def cb(result):
            self.fail('Unexpectedly succeeded!')

        def eb(failure):
            self.assertEqual(failure.value.state, Job.FAILED)
            self.assertTrue(isinstance(failure.value.failure.value,
                                       ZeroDivisionError))

        job = Job(None, 0)
        deferred = job.watch()
        deferred.addCallbacks(cb, eb)
        job.launch(lambda x: 1 / 0)
        return deferred
Beispiel #9
0
    def testLaunchFailWatchCancelDeferred(self):
        """
        Launch a job that fails, then watch it, then cancel the watching
        C{Deferred}.  The C{Deferred} will receive a Job instance whose
        state is FAILED (i.e., the cancel will have no effect).
        """

        def cb(result):
            self.fail('Unexpectedly succeeded!')

        def eb(failure):
            self.assertEqual(failure.value.state, Job.FAILED)
            self.assertTrue(isinstance(failure.value.failure.value,
                                       ZeroDivisionError))

        job = Job(None, 0)
        job.launch(lambda x: 1 / 0)
        deferred = job.watch()
        deferred.addCallbacks(cb, eb)
        deferred.cancel()
        return deferred
Beispiel #10
0
    def testWatchLaunchUnderwayCancelDeferred(self):
        """
        Watch a job, launch it with a function that returns a C{Deferred}
        that is slow to fire, then cancel the watching C{Deferred}. Check
        the watching C{Deferred} receives a L{Job} instance whose state is
        CANCELLED.
        """

        def cb(result):
            self.fail('Unexpectedly succeeded!')

        def eb(failure):
            self.assertEqual(failure.value.state, Job.CANCELLED)

        def run(x):
            return task.deferLater(reactor, 3, lambda x: x)

        job = Job(None, 0)
        deferred = job.watch()
        deferred.addCallbacks(cb, eb)
        job.launch(run)
        deferred.cancel()
        return deferred