Exemplo n.º 1
0
    def testTasksCanRunFunctionsWithParameters(self):
        global functionWasRan
        functionWasRan = False

        def setFunctionWasRanTo(value):
            global functionWasRan
            functionWasRan = value

        task = Task(setFunctionWasRanTo, (True, ))

        task.run()

        self.assertTrue(functionWasRan)
Exemplo n.º 2
0
    def testTasksCanRunFunctionsWithoutParameters(self):
        global functionWasRan
        functionWasRan = False

        def setFunctionWasRan():
            global functionWasRan
            functionWasRan = True

        task = Task(setFunctionWasRan)

        task.run()

        self.assertTrue(functionWasRan)
Exemplo n.º 3
0
    def testWorkersStayAliveEvenIfTaskRaisesAnException(self):
        taskDoneEvent = Event()

        def setTaskDone():
            taskDoneEvent.set()

        def raiseException():
            raise Exception("Foo")

        self.poolWorker.start()

        self.workerPool.tasks().put((1, Task(raiseException)))
        self.workerPool.tasks().put((1, Task(setTaskDone)))

        workerKeptWorking = taskDoneEvent.wait(1)

        self.assertTrue(self.poolWorker.isAlive())
        self.assertTrue(workerKeptWorking)
Exemplo n.º 4
0
    def addTask(self, function, *args):
        if not self._isRunning:
            self._logger.debug("Task not added to (stopped) queue: %s%s.",
                               function.__name__, str(args))
            return

        self._logger.debug("Adding task to queue: %s%s.", function.__name__,
                           str(args))
        task = Task(function, args)
        self._tasksQueue.put((3, task))

        return task
Exemplo n.º 5
0
    def testWorkersRunTasksFromTheirQueue(self):
        taskDoneEvent = Event()

        def setTaskDone():
            taskDoneEvent.set()

        self.poolWorker.start()

        self.workerPool.tasks().put((1, Task(setTaskDone)))

        taskDone = taskDoneEvent.wait(1)

        self.assertTrue(taskDone)
Exemplo n.º 6
0
    def addHighPriorityTask(self, function, *args):
        if not self._isRunning:
            self._logger.debug(
                "HIGH PRIORITY task not added to (stopped) queue: %s%s.",
                function.__name__, str(args))
            return

        self._logger.debug("Adding HIGH PRIORITY task to queue: %s%s.",
                           function.__name__, str(args))
        task = Task(function, args)
        self._tasksQueue.put((0, task))

        return task
Exemplo n.º 7
0
    def testWorkerThreadsRunAsDaemons(self):
        global isRunningAsDaemon
        isRunningAsDaemon = False

        def setIsRunningAsDaemon():
            global isRunningAsDaemon
            isRunningAsDaemon = current_thread().isDaemon()

        self.poolWorker.start()

        self.workerPool.tasks().put((1, Task(setIsRunningAsDaemon)))
        self.workerPool.tasks().join()

        self.assertTrue(isRunningAsDaemon)