[tor-commits] [nyx/master] Test _draw_bandwidth_stats()

atagar at torproject.org atagar at torproject.org
Sun Jun 26 00:48:01 UTC 2016


commit 0c06b5b49e717849c11ac8e27ca6ce82a2a23a95
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Apr 20 09:51:16 2016 -0700

    Test _draw_bandwidth_stats()
---
 nyx/panel/graph.py  | 38 +++++++++++++++++++-------------------
 test/panel/graph.py | 19 +++++++++++++++----
 2 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py
index 1b2a067..c35df13 100644
--- a/nyx/panel/graph.py
+++ b/nyx/panel/graph.py
@@ -562,7 +562,7 @@ class GraphPanel(nyx.panel.Panel):
 
     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)
@@ -683,22 +683,6 @@ class GraphPanel(nyx.panel.Panel):
 
     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
@@ -725,6 +709,22 @@ class GraphPanel(nyx.panel.Panel):
         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)
@@ -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...')
diff --git a/test/panel/graph.py b/test/panel/graph.py
index 7ffc48e..40c07fb 100644
--- a/test/panel/graph.py
+++ b/test/panel/graph.py
@@ -11,21 +11,32 @@ import nyx.panel.graph
 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),
@@ -34,7 +45,7 @@ class TestGraph(unittest.TestCase):
       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





More information about the tor-commits mailing list