Commit 25e557a5 authored by Guido Trotter's avatar Guido Trotter
Browse files

Workerpool.AddManyTasks: check tasks type



Each task has to be a sequence, or the RunTask call will fail.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 7ed3248b
......@@ -27,6 +27,8 @@ import collections
import logging
import threading
from ganeti import compat
class BaseWorker(threading.Thread, object):
"""Base worker class for worker pools.
......@@ -211,6 +213,9 @@ class WorkerPool(object):
@param tasks: list of args passed to L{BaseWorker.RunTask}
"""
assert compat.all(isinstance(task, (tuple, list)) for task in tasks), \
"Each task must be a sequence"
self._lock.acquire()
try:
self._WaitWhileQuiescingUnlocked()
......
......@@ -169,6 +169,28 @@ class TestWorkerpool(unittest.TestCase):
self.assertEquals(ctx.GetDoneTasks(), 22)
def testManyTasksSequence(self):
ctx = CountingContext()
wp = workerpool.WorkerPool("Test", 3, CountingBaseWorker)
try:
self._CheckWorkerCount(wp, 3)
self.assertRaises(AssertionError, wp.AddManyTasks,
["Hello world %s" % i for i in range(10)])
self.assertRaises(AssertionError, wp.AddManyTasks,
[i for i in range(10)])
wp.AddManyTasks([(ctx, "Hello world %s" % i, ) for i in range(10)])
wp.AddTask(ctx, "A separate hello")
wp.Quiesce()
self._CheckNoTasks(wp)
finally:
wp.TerminateWorkers()
self._CheckWorkerCount(wp, 0)
self.assertEquals(ctx.GetDoneTasks(), 11)
def _CheckNoTasks(self, wp):
wp._lock.acquire()
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment