osmo-gsm-tester/src/osmo_gsm_tester/core/event_loop.py

122 lines
4.3 KiB
Python
Raw Normal View History

# osmo_gsm_tester: Event loop
#
# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
#
# Author: Pau Espin Pedrol <pespin@sysmocom.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import time
from gi.repository import GLib, GObject
fix and refactor logging: drop 'with', simplify With the recent fix of the junit report related issues, another issue arose: the 'with log.Origin' was changed to disallow __enter__ing an object twice to fix problems, now still code would fail because it tries to do 'with' on the same object twice. The only reason is to ensure that logging is associated with a given object. Instead of complicating even more, implement differently. Refactor logging to simplify use: drop the 'with Origin' style completely, and instead use the python stack to determine which objects are created by which, and which object to associate a log statement with. The new way: we rely on the convention that each class instance has a local 'self' referencing the object instance. If we need to find an origin as a new object's parent, or to associate a log message with, we traverse each stack frame, fetching the first local 'self' object that is a log.Origin class instance. How to use: Simply call log.log() anywhere, and it finds an Origin object to log for, from the stack. Alternatively call self.log() for any Origin() object to skip the lookup. Create classes as child class of log.Origin and make sure to call super().__init__(category, name). This constructor will magically find a parent Origin on the stack. When an exception happens, we first escalate the exception up through call scopes to where ever it is handled by log.log_exn(). This then finds an Origin object in the traceback's stack frames, no need to nest in 'with' scopes. Hence the 'with log.Origin' now "happens implicitly", we can write pure natural python code, no more hassles with scope ordering. Furthermore, any frame can place additional logging information in a frame by calling log.ctx(). This is automatically inserted in the ancestry associated with a log statement / exception. Change-Id: I5f9b53150f2bb6fa9d63ce27f0806f0ca6a45e90
2017-06-09 23:18:27 +00:00
from . import log
class DeferredHandling:
def __init__(self):
self.defer_queue = []
def handle_queue(self):
while self.defer_queue:
handler, args, kwargs = self.defer_queue.pop(0)
handler(*args, **kwargs)
def defer(self, handler, *args, **kwargs):
self.defer_queue.append((handler, args, kwargs))
class WaitRequest:
def __init__(self, condition, condition_args, condition_kwargs, timeout, timestep):
self.timeout_ack = False
self.condition_ack = False
self.timeout_started = time.time()
self.timeout = timeout
self.condition = condition
self.condition_args = condition_args
self.condition_kwargs = condition_kwargs
def condition_check(self):
#print("_wait_condition_check")
waited = time.time() - self.timeout_started
if self.condition(*self.condition_args, **self.condition_kwargs):
self.condition_ack = True
elif waited > self.timeout:
self.timeout_ack = True
class EventLoop:
def __init__(self):
self.poll_funcs = []
self.gloop = GLib.MainLoop()
self.gctx = self.gloop.get_context()
self.deferred_handling = DeferredHandling()
def _trigger_cb_func(self, user_data):
self.defer(user_data)
return True #to retrigger the timeout
def defer(self, handler, *args, **kwargs):
self.deferred_handling.defer(handler, *args, **kwargs)
def register_poll_func(self, func, timestep=1):
id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, func) # in 1/1000th of a sec
self.poll_funcs.append((func, id))
def unregister_poll_func(self, func):
for pair in self.poll_funcs:
f, id = pair
if f == func:
GObject.source_remove(id)
self.poll_funcs.remove(pair)
return
def poll(self, may_block=False):
self.gctx.iteration(may_block)
self.deferred_handling.handle_queue()
def wait_no_raise(self, log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
if not timeout or timeout < 0:
self = log_obj
raise log.Error('wait() *must* time out at some point.', timeout=timeout)
if timestep < 0.1:
timestep = 0.1
wait_req = WaitRequest(condition, condition_args, condition_kwargs, timeout, timestep)
wait_id = GObject.timeout_add(timestep*1000, self._trigger_cb_func, wait_req.condition_check)
while True:
try:
self.poll(may_block=True)
except Exception: # cleanup of temporary resources in the wait scope
GObject.source_remove(wait_id)
raise
if wait_req.condition_ack or wait_req.timeout_ack:
GObject.source_remove(wait_id)
success = wait_req.condition_ack
return success
def wait(self, log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs):
if not self.wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep):
log.ctx(log_obj)
raise log.Error('Wait timeout', condition=condition, timeout=timeout, timestep=timestep)
def sleep(self, log_obj, seconds):
assert seconds > 0.
self.wait_no_raise(log_obj, lambda: False, [], {}, timeout=seconds, timestep=seconds)
MainLoop = EventLoop()
# vim: expandtab tabstop=4 shiftwidth=4