/usr/lib/python3/dist-packages/oslo_service/tests/test_threadgroup.py is in python3-oslo.service 1.8.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | # Copyright (c) 2012 Rackspace Hosting
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Unit Tests for thread groups
"""
import time
from eventlet import event
from oslotest import base as test_base
from oslo_service import threadgroup
class ThreadGroupTestCase(test_base.BaseTestCase):
"""Test cases for thread group."""
def setUp(self):
super(ThreadGroupTestCase, self).setUp()
self.tg = threadgroup.ThreadGroup()
self.addCleanup(self.tg.stop)
def test_add_dynamic_timer(self):
def foo(*args, **kwargs):
pass
initial_delay = 1
periodic_interval_max = 2
self.tg.add_dynamic_timer(foo, initial_delay, periodic_interval_max,
'arg', kwarg='kwarg')
self.assertEqual(1, len(self.tg.timers))
timer = self.tg.timers[0]
self.assertTrue(timer._running)
self.assertEqual(('arg',), timer.args)
self.assertEqual({'kwarg': 'kwarg'}, timer.kw)
def test_stop_current_thread(self):
stop_event = event.Event()
quit_event = event.Event()
def stop_self(*args, **kwargs):
if args[0] == 1:
time.sleep(1)
self.tg.stop()
stop_event.send('stop_event')
quit_event.wait()
for i in range(0, 4):
self.tg.add_thread(stop_self, i, kwargs='kwargs')
stop_event.wait()
self.assertEqual(1, len(self.tg.threads))
quit_event.send('quit_event')
def test_stop_immediately(self):
def foo(*args, **kwargs):
time.sleep(1)
start_time = time.time()
self.tg.add_thread(foo, 'arg', kwarg='kwarg')
time.sleep(0)
self.tg.stop()
end_time = time.time()
self.assertEqual(0, len(self.tg.threads))
self.assertTrue(end_time - start_time < 1)
def test_stop_gracefully(self):
def foo(*args, **kwargs):
time.sleep(1)
start_time = time.time()
self.tg.add_thread(foo, 'arg', kwarg='kwarg')
self.tg.stop(True)
end_time = time.time()
self.assertEqual(0, len(self.tg.threads))
self.assertTrue(end_time - start_time >= 1)
def test_stop_timers(self):
def foo(*args, **kwargs):
pass
self.tg.add_timer('1234', foo)
self.assertEqual(1, len(self.tg.timers))
self.tg.stop_timers()
self.assertEqual(0, len(self.tg.timers))
def test_add_and_remove_timer(self):
def foo(*args, **kwargs):
pass
timer = self.tg.add_timer('1234', foo)
self.assertEqual(1, len(self.tg.timers))
timer.stop()
self.assertEqual(1, len(self.tg.timers))
self.tg.timer_done(timer)
self.assertEqual(0, len(self.tg.timers))
def test_add_and_remove_dynamic_timer(self):
def foo(*args, **kwargs):
pass
initial_delay = 1
periodic_interval_max = 2
timer = self.tg.add_dynamic_timer(foo, initial_delay,
periodic_interval_max)
self.assertEqual(1, len(self.tg.timers))
self.assertTrue(timer._running)
timer.stop()
self.assertEqual(1, len(self.tg.timers))
self.tg.timer_done(timer)
self.assertEqual(0, len(self.tg.timers))
|