starcluster.tests.test_threadpool

Package starcluster :: Package tests :: Module test_threadpool |
[hide private]
[frames] | no frames]

# Source Code for Module starcluster.tests.test_threadpool

      1  #!/usr/bin/env python
      2
      3  import logging
      4  import tempfile
      5  logging.disable(logging.WARN)
      6
      7  from starcluster import tests
      8  from starcluster import exception
      9  from starcluster import threadpool
     10
     11
     12 -class TestThreadPool(tests.StarClusterTest):
     13
     14      _jobs = 5
     15      _mykw = 'StarCluster!!!'
     16      _pool = None
     17
     18      @property
     19 -    def pool(self):
     20          if not self._pool:
     21              self._pool = threadpool.get_thread_pool(10, disable_threads=False)
     22              fd = tempfile.TemporaryFile()
     23              self._pool.progress_bar.fd = fd
     24          return self._pool
     25
     26 -    def _no_args(self):
     27          pass
     28
     29 -    def _args_only(self, i):
     30          return i
     31
     32 -    def _kwargs_only(self, mykw=None):
     33          return mykw
     34
     35 -    def _args_and_kwargs(self, i, mykw=None):
     36          return (i, dict(mykw=mykw))
     37
     38 -    def test_no_args(self):
     39          pool = self.pool
     40          try:
     41              for i in range(self._jobs):
     42                  pool.simple_job(self._no_args, jobid=i)
     43              results = pool.wait(numtasks=self._jobs)
     44              print "no_args: %s" % results
     45              assert results.count(None) == self._jobs
     46          except exception.ThreadPoolException, e:
     47              raise Exception(e.format_excs())
     48
     49 -    def test_args_only(self):
     50          try:
     51              pool = self.pool
     52              for i in range(self._jobs):
     53                  pool.simple_job(self._args_only, i, jobid=i)
     54              results = pool.wait(numtasks=self._jobs)
     55              results.sort()
     56              print "args_only: %s" % results
     57              assert results == range(self._jobs)
     58          except exception.ThreadPoolException, e:
     59              raise Exception(e.format_excs())
     60
     61 -    def test_kwargs_only(self):
     62          pool = self.pool
     63          try:
     64              for i in range(self._jobs):
     65                  pool.simple_job(self._kwargs_only,
     66                                  kwargs=dict(mykw=self._mykw), jobid=i)
     67              results = pool.wait(numtasks=self._jobs)
     68              print "kwargs_only: %s" % results
     69              assert results.count(self._mykw) == self._jobs
     70          except exception.ThreadPoolException, e:
     71              raise Exception(e.format_excs())
     72
     73 -    def test_args_and_kwargs(self):
     74          pool = self.pool
     75          try:
     76              for i in range(self._jobs):
     77                  pool.simple_job(self._args_and_kwargs, i,
     78                                  kwargs=dict(mykw=self._mykw), jobid=i)
     79              results = pool.wait(numtasks=self._jobs)
     80              results.sort()
     81              print "args_and_kwargs: %s" % results
     82              assert results == zip(range(self._jobs),
     83                                    [dict(mykw=self._mykw)] * self._jobs)
     84          except exception.ThreadPoolException, e:
     85              raise Exception(e.format_excs())
     86
     87 -    def test_threadpool_map(self):
     88          try:
     89              r = 20
     90              ref = map(lambda x: x ** 2, range(r))
     91              calc = self.pool.map(lambda x: x ** 2, range(r))
     92              calc.sort()
     93              assert ref == calc
     94              for i in range(r):
     95                  self.pool.simple_job(lambda x: x ** 2, i, jobid=i)
     96              self.pool.wait(return_results=False)
     97              calc = self.pool.map(lambda x: x ** 2, range(r))
     98              calc.sort()
     99              assert ref == calc
    100          except exception.ThreadPoolException, e:
    101              raise Exception(e.format_excs())
    102