[core+tests] Improved poll handling

- mock poll instead of epoll
- increase timeout for poll (1ms is a lot of system load for nothing)
This commit is contained in:
Tobias Witek 2019-06-23 16:51:28 +02:00
parent ff82604036
commit 928c37a972
4 changed files with 29 additions and 20 deletions

View file

@ -25,15 +25,15 @@ def is_terminated():
def read_input(inp): def read_input(inp):
"""Read i3bar input and execute callbacks""" """Read i3bar input and execute callbacks"""
epoll = select.poll() poll = select.poll()
epoll.register(sys.stdin.fileno()) poll.register(sys.stdin.fileno(), select.POLLIN)
log.debug("starting click event processing") log.debug("starting click event processing")
while inp.running: while inp.running:
if is_terminated(): if is_terminated():
return return
try: try:
events = epoll.poll(1) events = poll.poll(1000)
except Exception: except Exception:
continue continue
for fileno, event in events: for fileno, event in events:
@ -52,8 +52,8 @@ def read_input(inp):
except ValueError as e: except ValueError as e:
log.debug("failed to parse event: {}".format(e)) log.debug("failed to parse event: {}".format(e))
log.debug("exiting click event processing") log.debug("exiting click event processing")
epoll.unregister(sys.stdin.fileno()) poll.unregister(sys.stdin.fileno())
epoll.close() poll.close()
inp.has_event = True inp.has_event = True
inp.clean_exit = True inp.clean_exit = True

View file

@ -17,8 +17,11 @@ Parameters:
from __future__ import absolute_import from __future__ import absolute_import
import datetime import datetime
import locale import locale
import pytz try:
import tzlocal import pytz
import tzlocal
except:
pass
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
@ -40,7 +43,10 @@ class Module(bumblebee.engine.Module):
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self.next_tz) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self.next_tz)
engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self.prev_tz) engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self.prev_tz)
self._fmt = self.parameter("format", default_format(self.name)) self._fmt = self.parameter("format", default_format(self.name))
self._timezones = self.parameter("timezone", tzlocal.get_localzone().zone).split(",") try:
self._timezones = self.parameter("timezone", tzlocal.get_localzone().zone).split(",")
except:
self._timezones = ""
self._current_tz = 0 self._current_tz = 0
l = locale.getdefaultlocale() l = locale.getdefaultlocale()
@ -54,10 +60,13 @@ class Module(bumblebee.engine.Module):
def get_time(self, widget): def get_time(self, widget):
try: try:
tz = pytz.timezone(self._timezones[self._current_tz].strip()) try:
retval = datetime.datetime.now(tz=tzlocal.get_localzone()).astimezone(tz).strftime(self._fmt) tz = pytz.timezone(self._timezones[self._current_tz].strip())
except pytz.exceptions.UnknownTimeZoneError: retval = datetime.datetime.now(tz=tzlocal.get_localzone()).astimezone(tz).strftime(self._fmt)
retval = "[Unknown timezone: {}]".format(self._timezones[self._current_tz].strip()) except pytz.exceptions.UnknownTimeZoneError:
retval = "[Unknown timezone: {}]".format(self._timezones[self._current_tz].strip())
except:
retval = "[n/a]"
enc = locale.getpreferredencoding() enc = locale.getpreferredencoding()
if hasattr(retval, "decode"): if hasattr(retval, "decode"):

View file

@ -14,7 +14,7 @@ def rand(cnt):
return "".join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(cnt)) return "".join(random.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(cnt))
def setup_test(test, Module): def setup_test(test, Module):
test._stdin, test._select, test.stdin, test.select = epoll_mock("bumblebee.input") test._stdin, test._select, test.stdin, test.select = poll_mock("bumblebee.input")
test.popen = MockPopen() test.popen = MockPopen()
@ -33,19 +33,19 @@ def teardown_test(test):
test._select.stop() test._select.stop()
test.popen.cleanup() test.popen.cleanup()
def epoll_mock(module=""): def poll_mock(module=""):
if len(module) > 0: module = "{}.".format(module) if len(module) > 0: module = "{}.".format(module)
stdin = mock.patch("{}sys.stdin".format(module)) stdin = mock.patch("{}sys.stdin".format(module))
select = mock.patch("{}select".format(module)) select = mock.patch("{}select".format(module))
epoll = mock.Mock() poll = mock.Mock()
stdin_mock = stdin.start() stdin_mock = stdin.start()
select_mock = select.start() select_mock = select.start()
stdin_mock.fileno.return_value = 1 stdin_mock.fileno.return_value = 1
select_mock.epoll.return_value = epoll select_mock.poll.return_value = poll
epoll.poll.return_value = [(stdin_mock.fileno.return_value, 100)] poll.poll.return_value = [(stdin_mock.fileno.return_value, 100)]
return stdin, select, stdin_mock, select_mock return stdin, select, stdin_mock, select_mock

View file

@ -20,10 +20,10 @@ class TestI3BarInput(unittest.TestCase):
self.popen = mocks.MockPopen() self.popen = mocks.MockPopen()
self.stdin.fileno.return_value = 1 self.stdin.fileno.return_value = 1
epoll = mock.Mock() poll = mock.Mock()
self.select.epoll.return_value = epoll self.select.poll.return_value = poll
epoll.poll.return_value = [(self.stdin.fileno.return_value, 2)] poll.poll.return_value = [(self.stdin.fileno.return_value, 2)]
self.anyModule = mock.Mock() self.anyModule = mock.Mock()
self.anyModule.id = mocks.rand(10) self.anyModule.id = mocks.rand(10)