Skip to content

Commit

Permalink
Test _draw_bandwidth_stats()
Browse files Browse the repository at this point in the history
  • Loading branch information
atagar committed Jun 18, 2016
1 parent 28f7b59 commit 0c06b5b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
38 changes: 19 additions & 19 deletions nyx/panel/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def draw(self, subwindow):

if attr.stat.stat_type() == GraphStat.BANDWIDTH:
if subwindow.width <= COLLAPSE_WIDTH:
self._draw_bandwidth_stats(subwindow, attr, subwindow.width)
_draw_bandwidth_stats(subwindow, DEFAULT_CONTENT_HEIGHT + attr.subgraph_height - 4, attr.stat, attr.subgraph_width)

if accounting_stats:
_draw_accounting_stats(subwindow, DEFAULT_CONTENT_HEIGHT + attr.subgraph_height - 2, accounting_stats)
Expand Down Expand Up @@ -683,22 +683,6 @@ def _get_x_axis_labels(self, attr, subgraph_columns):

return x_axis_labels

def _draw_bandwidth_stats(self, subwindow, attr, width):
"""
Replaces the x-axis labeling with bandwidth stats. This is done on small
screens since this information otherwise wouldn't fit.
"""

labeling_line = DEFAULT_CONTENT_HEIGHT + attr.subgraph_height - 4
subwindow.addstr(0, labeling_line, ' ' * width) # clear line

runtime = time.time() - attr.stat.start_time
primary_footer = 'total: %s, avg: %s/sec' % (_size_label(attr.stat.primary.total), _size_label(attr.stat.primary.total / runtime))
secondary_footer = 'total: %s, avg: %s/sec' % (_size_label(attr.stat.secondary.total), _size_label(attr.stat.secondary.total / runtime))

subwindow.addstr(1, labeling_line, primary_footer, PRIMARY_COLOR)
subwindow.addstr(attr.subgraph_width + 1, labeling_line, secondary_footer, SECONDARY_COLOR)

def _update_accounting(self, event):
if not CONFIG['features.graph.bw.accounting.show']:
self._accounting_stats = None
Expand All @@ -725,6 +709,22 @@ def _update_stats(self, event):
self.redraw(True)


def _draw_bandwidth_stats(subwindow, y, stat, subgraph_width):
"""
Replaces the x-axis labeling with bandwidth stats. This is done on small
screens since this information otherwise wouldn't fit.
"""

subwindow.addstr(0, y, ' ' * 500) # clear line

runtime = time.time() - stat.start_time
primary_footer = 'total: %s, avg: %s/sec' % (_size_label(stat.primary.total), _size_label(stat.primary.total / runtime))
secondary_footer = 'total: %s, avg: %s/sec' % (_size_label(stat.secondary.total), _size_label(stat.secondary.total / runtime))

subwindow.addstr(1, y, primary_footer, PRIMARY_COLOR)
subwindow.addstr(subgraph_width + 1, y, secondary_footer, SECONDARY_COLOR)


def _draw_accounting_stats(subwindow, y, accounting):
if tor_controller().is_alive():
hibernate_color = CONFIG['attr.hibernate_color'].get(accounting.status, RED)
Expand All @@ -735,8 +735,8 @@ def _draw_accounting_stats(subwindow, y, accounting):

subwindow.addstr(35, y, 'Time to reset: %s' % str_tools.short_time_label(accounting.time_until_reset))

subwindow.addstr(2, y + 1, '%s / %s' % (str_tools.size_label(accounting.read_bytes), str_tools.size_label(accounting.read_limit)), PRIMARY_COLOR)
subwindow.addstr(37, y + 1, '%s / %s' % (str_tools.size_label(accounting.written_bytes), str_tools.size_label(accounting.write_limit)), SECONDARY_COLOR)
subwindow.addstr(2, y + 1, '%s / %s' % (_size_label(accounting.read_bytes), _size_label(accounting.read_limit)), PRIMARY_COLOR)
subwindow.addstr(37, y + 1, '%s / %s' % (_size_label(accounting.written_bytes), _size_label(accounting.write_limit)), SECONDARY_COLOR)
else:
subwindow.addstr(0, y, 'Accounting:', BOLD)
subwindow.addstr(12, y, 'Connection Closed...')
Expand Down
19 changes: 15 additions & 4 deletions test/panel/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,32 @@
import test

from test import require_curses
from mock import patch
from mock import patch, Mock

EXPECTED_ACCOUNTING = """
Accounting (awake) Time to reset: 01:02
4 KB / 105 KB 2 KB / 9 KB
37.7 Kb / 842.0 Kb 16.0 Kb / 74.1 Kb
""".strip()


class TestGraph(unittest.TestCase):
@require_curses
@patch('time.time', Mock(return_value = 460.0))
def test_draw_bandwidth_stats(self):
stat = Mock()
stat.start_time = 215.0
stat.primary.total = 2306867.2
stat.secondary.total = 1782579.2

rendered = test.render(nyx.panel.graph._draw_bandwidth_stats, 0, stat, 40)
self.assertEqual(' total: 17.6 Mb, avg: 73.5 Kb/sec total: 13.5 Mb, avg: 56.8 Kb/sec', rendered.content)

@require_curses
@patch('nyx.panel.graph.tor_controller')
def test_draw_accounting_stats(self, tor_controller_mock):
tor_controller_mock().is_alive.return_value = True

stat = stem.control.AccountingStats(
accounting_stat = stem.control.AccountingStats(
1410723598.276578,
'awake',
datetime.datetime(2014, 9, 14, 19, 41),
Expand All @@ -34,7 +45,7 @@ def test_draw_accounting_stats(self, tor_controller_mock):
2050, 7440, 9490,
)

rendered = test.render(nyx.panel.graph._draw_accounting_stats, 0, stat)
rendered = test.render(nyx.panel.graph._draw_accounting_stats, 0, accounting_stat)
self.assertEqual(EXPECTED_ACCOUNTING, rendered.content)

@require_curses
Expand Down

0 comments on commit 0c06b5b

Please sign in to comment.