~james-w/udd/management-commands

« back to all changes in this revision

Viewing changes to udd/tests/test_threads.py

  • Committer: Vincent Ladeuil
  • Date: 2011-12-07 10:10:07 UTC
  • mfrom: (550.1.1 899231-kills-spams-log)
  • Revision ID: v.ladeuil+lp@free.fr-20111207101007-9ek9xs011yfe6r51
Only log real kill attempts

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
 
188
188
class Sleeper(threads.SubprocessMonitor):
189
189
 
190
 
    def __init__(self, sleep_time=0.001, max_duration=None):
191
 
        # sleep can't be killed (or interrupted) so we don't sleep for long but
192
 
        # keep sleeping until killed
 
190
    def __init__(self, duration=None, max_duration=None):
 
191
        if duration is None:
 
192
            # Make sure we die long after the test finish (to avoid leaks) but
 
193
            # still use a value (120 seconds) that allow *some* debug (you want
 
194
            # more for *heavy* debug :)
 
195
           duration = 120.0
 
196
        sleep_time = 0.001
193
197
        super(Sleeper, self).__init__(
194
 
            # Make sure we die long after the test finish (to avoid leaks) but
195
 
            # still use a value that allow *some* debug (you want more for
196
 
            # *heavy* debug :)
197
 
            ['-c', 'import time; while xrange(120.0/%s): time.sleep(%s)'
198
 
             % (sleep_time, sleep_time)],
 
198
            ['-c', 'for _ in xrange(int(%s/%s)): '
 
199
             'import time; time.sleep(%s)'
 
200
             % (duration, sleep_time, sleep_time)],
199
201
            max_duration=max_duration)
200
202
 
201
203
 
 
204
class TestSleeper(tests.TestCase):
 
205
    """Simple tests to ensire the Sleeper class is sound.
 
206
 
 
207
    TestThreadDriver needs a process that sleep long enough to do various tests
 
208
    about the driver behavior while processes are left running. If sleeper
 
209
    fails to run properly weird failures occur.
 
210
    """
 
211
    def test_no_errors(self):
 
212
        s = Sleeper(duration=0.01)
 
213
        self.addCleanup(s.join, 0)
 
214
        self.addCleanup(s.kill_with_escalation)
 
215
        s.start()
 
216
        s.started.wait()
 
217
        s.join()
 
218
        self.assertEquals(0, s.retcode)
 
219
        self.assertEquals('', s.out)
 
220
        self.assertEquals('', s.err)
 
221
 
 
222
 
202
223
class TestThreadDriver(tests.TestCase):
203
224
 
204
225
    def get_driver(self, *args, **kwargs):
425
446
        sleepers[0].kill_with_escalation()
426
447
        sleepers[1].kill_with_escalation()
427
448
        driver.stopped.wait()
 
449
        self.assertLength(0, driver.threads)
428
450
 
429
451
    def test_check_quotas(self):
 
452
        q = Queue.Queue(1)
 
453
        self.abusers = None
 
454
        class TestDriver(threads.ThreadDriver):
 
455
 
 
456
            def check_time_quota(driver):
 
457
                abusers = super(TestDriver, driver).check_time_quota()
 
458
                if abusers:
 
459
                    self.abusers = abusers
 
460
        driver = self.get_started_driver(klass=TestDriver,
 
461
                                         queue=q, max_threads=2)
 
462
        driver.started.wait()
 
463
        self.addCleanup(driver.stop.set)
 
464
        self.assertLength(0, driver.threads)
430
465
        # A sleeper that will be killed
431
 
        sleeper = Sleeper(sleep_time=1, max_duration=0.0)
432
 
        q = Queue.Queue(1)
 
466
        sleeper = Sleeper(duration=1.0, max_duration=0.0)
 
467
        q.put(sleeper)
433
468
        self.addCleanup(sleeper.join, 0)
434
469
        self.addCleanup(sleeper.kill_with_escalation)
435
 
        q.put(sleeper)
436
 
        self.killed = None
437
 
        class TestDriver(threads.ThreadDriver):
438
 
 
439
 
            def check_time_quota(driver):
440
 
                killed = super(TestDriver, driver).check_time_quota()
441
 
                if killed:
442
 
                    self.killed = killed
443
 
        driver = self.get_started_driver(klass=TestDriver,
444
 
                                         queue=q, max_threads=2)
445
 
        driver.started.wait()
446
 
        self.addCleanup(driver.stop.set)
447
 
        self.assertLength(0, driver.threads)
448
470
        sleeper.started.wait()
449
471
        # wait longer than the allowed duration
450
472
        time.sleep(0.01)
451
 
        self.assertEquals([sleeper], self.killed)
 
473
        self.assertLength(1, self.abusers)
 
474
        self.assertEquals(sleeper, self.abusers[0][0])
452
475
        driver.stop.set()
453
476
        driver.stopped.wait()
 
477
        self.assertLength(0, driver.threads)
454
478
 
455
479
 
456
480
# FIXME: The ImportController is a bit harder to test as it handles signals and