[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

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



commit 3e3443b3f995dc44a7c3886d5d5c2a8da11f5a7f
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sat Apr 9 23:47:10 2016 -0700

    Test _draw_resource_usage()
---
 nyx/panel/header.py  | 70 +++++++++++++++++++++++++++-------------------------
 test/panel/header.py | 28 +++++++++++++++++++++
 2 files changed, 64 insertions(+), 34 deletions(-)

diff --git a/nyx/panel/header.py b/nyx/panel/header.py
index 922b907..9e8e57d 100644
--- a/nyx/panel/header.py
+++ b/nyx/panel/header.py
@@ -180,6 +180,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
 
     left_width = max(subwindow.width / 2, 77) if is_wide else subwindow.width
     right_width = subwindow.width - left_width
+    pause_time = self.get_pause_time() if self.is_paused() else None
 
     _draw_platform_section(subwindow, 0, 0, left_width, vals)
 
@@ -189,7 +190,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
       _draw_disconnected(subwindow, 0, 1, vals.last_heartbeat)
 
     if is_wide:
-      self._draw_resource_usage(subwindow, left_width, 0, right_width, vals)
+      _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)
@@ -198,7 +199,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
       elif vals.is_connected:
         self._draw_newnym_option(subwindow, left_width, 1, right_width, vals)
     else:
-      self._draw_resource_usage(subwindow, 0, 2, left_width, vals)
+      _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)
@@ -212,38 +213,6 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
     else:
       subwindow.addstr(0, subwindow.height - 1, 'Paused', HIGHLIGHT)
 
-  def _draw_resource_usage(self, subwindow, x, y, width, vals):
-    """
-    System resource usage of the tor process...
-
-      cpu: 0.0% tor, 1.0% nyx    mem: 0 (0.0%)       pid: 16329  uptime: 12-20:42:07
-    """
-
-    if vals.start_time:
-      if not vals.is_connected:
-        now = vals.connection_time
-      elif self.is_paused():
-        now = self.get_pause_time()
-      else:
-        now = time.time()
-
-      uptime = str_tools.short_time_label(now - vals.start_time)
-    else:
-      uptime = ''
-
-    sys_fields = (
-      (0, vals.format('cpu: {tor_cpu}% tor, {nyx_cpu}% nyx')),
-      (27, vals.format('mem: {memory} ({memory_percent}%)')),
-      (47, vals.format('pid: {pid}')),
-      (59, 'uptime: %s' % uptime),
-    )
-
-    for (start, label) in sys_fields:
-      if width >= start + len(label):
-        subwindow.addstr(x + start, y, label)
-      else:
-        break
-
   def _draw_fingerprint_and_fd_usage(self, subwindow, x, y, width, vals):
     """
     Presents our fingerprint, and our file descriptor usage if we're running
@@ -559,3 +528,36 @@ def _draw_disconnected(subwindow, x, y, last_heartbeat):
   x = subwindow.addstr(x, y, 'Tor Disconnected', RED, BOLD)
   last_heartbeat_str = time.strftime('%H:%M %m/%d/%Y', time.localtime(last_heartbeat))
   subwindow.addstr(x, y, ' (%s, press r to reconnect)' % last_heartbeat_str)
+
+
+def _draw_resource_usage(subwindow, x, y, width, vals, pause_time):
+  """
+  System resource usage of the tor process...
+
+    cpu: 0.0% tor, 1.0% nyx    mem: 0 (0.0%)       pid: 16329  uptime: 12-20:42:07
+  """
+
+  if vals.start_time:
+    if not vals.is_connected:
+      now = vals.connection_time
+    elif pause_time:
+      now = pause_time
+    else:
+      now = time.time()
+
+    uptime = str_tools.short_time_label(now - vals.start_time)
+  else:
+    uptime = ''
+
+  sys_fields = (
+    (0, vals.format('cpu: {tor_cpu}% tor, {nyx_cpu}% nyx')),
+    (27, vals.format('mem: {memory} ({memory_percent}%)')),
+    (47, vals.format('pid: {pid}')),
+    (59, 'uptime: %s' % uptime),
+  )
+
+  for (start, label) in sys_fields:
+    if width >= start + len(label):
+      subwindow.addstr(x + start, y, label)
+    else:
+      break
diff --git a/test/panel/header.py b/test/panel/header.py
index 603d213..3828804 100644
--- a/test/panel/header.py
+++ b/test/panel/header.py
@@ -84,3 +84,31 @@ class TestHeader(unittest.TestCase):
   def test_draw_disconnected(self, localtime_mock):
     localtime_mock.return_value = time.strptime('22:43 04/09/2016', '%H:%M %m/%d/%Y')
     self.assertEqual('Tor Disconnected (22:43 04/09/2016, press r to reconnect)', test.render(nyx.panel.header._draw_disconnected, 0, 0, 1460267022.231895).content)
+
+  @require_curses
+  def test_draw_resource_usage(self):
+    vals = nyx.panel.header._sampling(
+      start_time = 1460166022.231895,
+      connection_time = 1460267022.231895,
+      is_connected = False,
+      tor_cpu = '2.1',
+      nyx_cpu = '5.4',
+      memory = '118 MB',
+      memory_percent = '3.0',
+      pid = '22439',
+    )
+
+    test_input = {
+      80: 'cpu: 2.1% tor, 5.4% nyx    mem: 118 MB (3.0%)  pid: 22439  uptime: 1-04:03:20',
+      70: 'cpu: 2.1% tor, 5.4% nyx    mem: 118 MB (3.0%)  pid: 22439',
+      60: 'cpu: 2.1% tor, 5.4% nyx    mem: 118 MB (3.0%)  pid: 22439',
+      50: 'cpu: 2.1% tor, 5.4% nyx    mem: 118 MB (3.0%)',
+      40: 'cpu: 2.1% tor, 5.4% nyx',
+      30: 'cpu: 2.1% tor, 5.4% nyx',
+      20: '',
+      10: '',
+      0: '',
+    }
+
+    for width, expected in test_input.items():
+      self.assertEqual(expected, test.render(nyx.panel.header._draw_resource_usage, 0, 0, width, vals, None).content)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits