commit 7b30d6fdecfe2b404188e57f81cf5fd4f8b03859 Author: Damian Johnson atagar@torproject.org Date: Sun Apr 10 11:45:30 2016 -0700
Test _draw_fingerprint_and_fd_usage() --- nyx/panel/header.py | 69 ++++++++++++++++++++++++++-------------------------- test/panel/header.py | 46 ++++++++++++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 35 deletions(-)
diff --git a/nyx/panel/header.py b/nyx/panel/header.py index 9e8e57d..da0ba7f 100644 --- a/nyx/panel/header.py +++ b/nyx/panel/header.py @@ -193,7 +193,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread): _draw_resource_usage(subwindow, left_width, 0, right_width, vals, pause_time)
if vals.is_relay: - self._draw_fingerprint_and_fd_usage(subwindow, left_width, 1, right_width, vals) + _draw_fingerprint_and_fd_usage(subwindow, left_width, 1, right_width, vals) self._draw_flags(subwindow, 0, 2, left_width, vals) self._draw_exit_policy(subwindow, left_width, 2, right_width, vals) elif vals.is_connected: @@ -202,7 +202,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread): _draw_resource_usage(subwindow, 0, 2, left_width, vals, pause_time)
if vals.is_relay: - self._draw_fingerprint_and_fd_usage(subwindow, 0, 3, left_width, vals) + _draw_fingerprint_and_fd_usage(subwindow, 0, 3, left_width, vals) self._draw_flags(subwindow, 0, 4, left_width, vals)
if self._message: @@ -213,37 +213,6 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread): else: subwindow.addstr(0, subwindow.height - 1, 'Paused', HIGHLIGHT)
- def _draw_fingerprint_and_fd_usage(self, subwindow, x, y, width, vals): - """ - Presents our fingerprint, and our file descriptor usage if we're running - out... - - fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B, file desc: 900 / 1000 (90%) - """ - - initial_x, space_left = x, width - - x = subwindow.addstr(x, y, vals.format('fingerprint: {fingerprint}', width)) - space_left -= x - initial_x - - if space_left >= 30 and vals.fd_used and vals.fd_limit != -1: - fd_percent = 100 * vals.fd_used / vals.fd_limit - - if fd_percent >= SHOW_FD_THRESHOLD: - if fd_percent >= 95: - percentage_format = (RED, BOLD) - elif fd_percent >= 90: - percentage_format = (RED,) - elif fd_percent >= 60: - percentage_format = (YELLOW,) - else: - percentage_format = () - - x = subwindow.addstr(x, y, ', file descriptors' if space_left >= 37 else ', file desc') - x = subwindow.addstr(x, y, vals.format(': {fd_used} / {fd_limit} (')) - x = subwindow.addstr(x, y, '%i%%' % fd_percent, *percentage_format) - subwindow.addstr(x, y, ')') - def _draw_flags(self, subwindow, x, y, width, vals): """ Presents flags held by our relay... @@ -377,7 +346,7 @@ def _sampling(**attr): def format(self, message, crop_width = None): formatted_msg = message.format(**self._attr)
- if crop_width: + if crop_width is not None: formatted_msg = str_tools.crop(formatted_msg, crop_width)
return formatted_msg @@ -561,3 +530,35 @@ def _draw_resource_usage(subwindow, x, y, width, vals, pause_time): subwindow.addstr(x + start, y, label) else: break + + +def _draw_fingerprint_and_fd_usage(subwindow, x, y, width, vals): + """ + Presents our fingerprint, and our file descriptor usage if we're running + out... + + fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B, file desc: 900 / 1000 (90%) + """ + + initial_x, space_left = x, width + + x = subwindow.addstr(x, y, vals.format('fingerprint: {fingerprint}', width)) + space_left -= x - initial_x + + if space_left >= 30 and vals.fd_used and vals.fd_limit != -1: + fd_percent = 100 * vals.fd_used / vals.fd_limit + + if fd_percent >= SHOW_FD_THRESHOLD: + if fd_percent >= 95: + percentage_format = (RED, BOLD) + elif fd_percent >= 90: + percentage_format = (RED,) + elif fd_percent >= 60: + percentage_format = (YELLOW,) + else: + percentage_format = () + + x = subwindow.addstr(x, y, ', file descriptors' if space_left >= 37 else ', file desc') + x = subwindow.addstr(x, y, vals.format(': {fd_used} / {fd_limit} (')) + x = subwindow.addstr(x, y, '%i%%' % fd_percent, *percentage_format) + subwindow.addstr(x, y, ')') diff --git a/test/panel/header.py b/test/panel/header.py index 3828804..0d80f0f 100644 --- a/test/panel/header.py +++ b/test/panel/header.py @@ -31,7 +31,7 @@ class TestHeader(unittest.TestCase): 30: 'nyx - odin (Linux 3.5.0-54...)', 20: 'nyx - odin (Linu...)', 10: 'nyx - odin', - 0: 'nyx - odin', + 0: '', }
for width, expected in test_input.items(): @@ -112,3 +112,47 @@ class TestHeader(unittest.TestCase):
for width, expected in test_input.items(): self.assertEqual(expected, test.render(nyx.panel.header._draw_resource_usage, 0, 0, width, vals, None).content) + + @require_curses + def test_draw_fingerprint_and_fd_usage(self): + vals = nyx.panel.header._sampling( + fingerprint = '1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', + fd_used = None, + ) + + test_input = { + 80: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', + 70: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', + 60: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B', + 50: 'fingerprint: 1A94D1A794FCB2F8B6CBC179EF8FDD4008...', + 40: 'fingerprint: 1A94D1A794FCB2F8B6CBC179...', + 30: 'fingerprint: 1A94D1A794FCB2...', + 20: 'fingerprint: 1A94...', + 10: 'fingerp...', + 0: '', + } + + for width, expected in test_input.items(): + self.assertEqual(expected, test.render(nyx.panel.header._draw_fingerprint_and_fd_usage, 0, 0, width, vals).content) + + @require_curses + def test_draw_fingerprint_and_fd_usage_with_fd_count(self): + test_input = { + 59: 'fingerprint: <stub>', + 60: 'fingerprint: <stub>, file descriptors: 60 / 100 (60%)', + 75: 'fingerprint: <stub>, file descriptors: 75 / 100 (75%)', + 89: 'fingerprint: <stub>, file descriptors: 89 / 100 (89%)', + 90: 'fingerprint: <stub>, file descriptors: 90 / 100 (90%)', + 95: 'fingerprint: <stub>, file descriptors: 95 / 100 (95%)', + 99: 'fingerprint: <stub>, file descriptors: 99 / 100 (99%)', + 100: 'fingerprint: <stub>, file descriptors: 100 / 100 (100%)', + } + + for fd_used, expected in test_input.items(): + vals = nyx.panel.header._sampling( + fingerprint = '<stub>', + fd_used = fd_used, + fd_limit = 100, + ) + + self.assertEqual(expected, test.render(nyx.panel.header._draw_fingerprint_and_fd_usage, 0, 0, 80, vals).content)
tor-commits@lists.torproject.org