2017-05-22 14:38:49 +00:00
|
|
|
# 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
|
2017-06-03 07:51:45 +00:00
|
|
|
# it under the terms of the GNU General Public License as
|
2017-05-22 14:38:49 +00:00
|
|
|
# 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
|
2017-06-03 07:51:45 +00:00
|
|
|
# GNU General Public License for more details.
|
2017-05-22 14:38:49 +00:00
|
|
|
#
|
2017-06-03 07:51:45 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2017-05-22 14:38:49 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import time
|
2018-03-26 17:13:32 +00:00
|
|
|
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
|
|
|
|
|
2018-03-26 17:13:32 +00:00
|
|
|
class DeferredHandling:
|
|
|
|
defer_queue = []
|
|
|
|
|
|
|
|
def handle_queue(self):
|
|
|
|
while DeferredHandling.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:
|
|
|
|
timeout_ack = False
|
|
|
|
condition_ack = False
|
|
|
|
|
|
|
|
def __init__(self, condition, condition_args, condition_kwargs, timeout, timestep):
|
|
|
|
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:
|
|
|
|
poll_funcs = []
|
|
|
|
gloop = None
|
|
|
|
gctx = None
|
|
|
|
deferred_handling = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
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:
|
|
|
|
self.poll(may_block=True)
|
|
|
|
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')
|
|
|
|
|
|
|
|
def sleep(self, log_obj, seconds):
|
|
|
|
assert seconds > 0.
|
|
|
|
self.wait_no_raise(log_obj, lambda: False, [], {}, timeout=seconds, timestep=seconds)
|
|
|
|
|
|
|
|
|
2018-03-28 17:17:34 +00:00
|
|
|
MainLoop = EventLoop()
|
2017-05-22 14:38:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
# vim: expandtab tabstop=4 shiftwidth=4
|