Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: Event loop |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 20 | import time |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 21 | from . import log |
| 22 | |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 23 | poll_funcs = [] |
| 24 | |
| 25 | def register_poll_func(func): |
| 26 | global poll_funcs |
| 27 | poll_funcs.append(func) |
| 28 | |
| 29 | def unregister_poll_func(func): |
| 30 | global poll_funcs |
| 31 | poll_funcs.remove(func) |
| 32 | |
| 33 | def poll(): |
| 34 | global poll_funcs |
| 35 | for func in poll_funcs: |
| 36 | func() |
| 37 | |
| 38 | def wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep): |
| 39 | if not timeout or timeout < 0: |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 40 | self = log_obj |
| 41 | raise log.Error('wait() *must* time out at some point.', timeout=timeout) |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 42 | if timestep < 0.1: |
| 43 | timestep = 0.1 |
| 44 | |
| 45 | started = time.time() |
| 46 | while True: |
| 47 | poll() |
| 48 | if condition(*condition_args, **condition_kwargs): |
| 49 | return True |
| 50 | waited = time.time() - started |
| 51 | if waited > timeout: |
| 52 | return False |
| 53 | time.sleep(timestep) |
| 54 | |
| 55 | def wait(log_obj, condition, *condition_args, timeout=300, timestep=1, **condition_kwargs): |
| 56 | if not wait_no_raise(log_obj, condition, condition_args, condition_kwargs, timeout, timestep): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 57 | log.ctx(log_obj) |
| 58 | raise log.Error('Wait timeout') |
Pau Espin Pedrol | 927344b | 2017-05-22 16:38:49 +0200 | [diff] [blame] | 59 | |
| 60 | def sleep(log_obj, seconds): |
| 61 | assert seconds > 0. |
| 62 | wait_no_raise(log_obj, lambda: False, [], {}, timeout=seconds, timestep=min(seconds, 1)) |
| 63 | |
| 64 | |
| 65 | # vim: expandtab tabstop=4 shiftwidth=4 |