[tor-commits] [stem/master] Support new CIRC_BW delivered and overhead attributes

atagar at torproject.org atagar at torproject.org
Tue May 15 19:11:00 UTC 2018


commit aa692e62bfda5be8b87e463c3c899cb13968d32a
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue May 15 12:08:55 2018 -0700

    Support new CIRC_BW delivered and overhead attributes
    
    Stem support for the attributes added in...
    
      https://gitweb.torproject.org/torspec.git/commit/?id=fbb38ec
    
    Description is pending...
    
      https://trac.torproject.org/projects/tor/ticket/26110
---
 docs/change_log.rst          |  1 +
 stem/response/events.py      | 34 ++++++++++++++++++++++++++++++----
 test/unit/response/events.py | 11 +++++++++++
 3 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index b080af50..54a1e628 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -51,6 +51,7 @@ The following are only available within Stem's `git repository
   * Added a timeout argument to :class:`~stem.control.Controller` methods that could await a response (:trac:`26056`)
   * Stacktrace if :func:`stem.connection.connect` had a string port argument
   * More reliable ExitPolicy resolution (:trac:`25739`)
+  * Added the delivered_read, delivered_written, overhead_read, and overhead_written attributes to :class:`~stem.response.events.CircuitBandwidthEvent` (:spec:`fbb38ec`)
   * Replaced socket's :func:`~stem.socket.ControlPort.get_address`, :func:`~stem.socket.ControlPort.get_port`, and :func:`~stem.socket.ControlSocketFile.get_socket_path` with attributes
   * Removed 'raw' argument from :func:`~stem.socket.ControlSocket.send`
 
diff --git a/stem/response/events.py b/stem/response/events.py
index de52c08f..1048e2ba 100644
--- a/stem/response/events.py
+++ b/stem/response/events.py
@@ -1209,9 +1209,17 @@ class CircuitBandwidthEvent(Event):
   .. versionchanged:: 1.6.0
      Added the time attribute.
 
+  .. versionchanged:: 1.7.0
+     Added the delivered_read, delivered_written, overhead_read, and
+     overhead_written attributes.
+
   :var str id: circuit identifier
   :var int read: bytes received by tor that second
   :var int written: bytes sent by tor that second
+  :var int delivered_read: description pending :trac:`26110`
+  :var int delivered_written: description pending :trac:`26110`
+  :var int overhead_read: description pending :trac:`26110`
+  :var int overhead_written: description pending :trac:`26110`
   :var datetime time: time when the measurement was recorded
   """
 
@@ -1219,6 +1227,10 @@ class CircuitBandwidthEvent(Event):
     'ID': 'id',
     'READ': 'read',
     'WRITTEN': 'written',
+    'DELIVERED_READ': 'delivered_read',
+    'DELIVERED_WRITTEN': 'delivered_written',
+    'OVERHEAD_READ': 'overhead_read',
+    'OVERHEAD_WRITTEN': 'overhead_written',
     'TIME': 'time',
   }
 
@@ -1231,15 +1243,29 @@ class CircuitBandwidthEvent(Event):
       raise stem.ProtocolError('CIRC_BW event is missing its read value')
     elif not self.written:
       raise stem.ProtocolError('CIRC_BW event is missing its written value')
-    elif not self.read.isdigit() or not self.written.isdigit():
-      raise stem.ProtocolError("A CIRC_BW event's bytes sent and received should be a positive numeric value, received: %s" % self)
+    elif not self.read.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's bytes received should be a positive numeric value, received: %s" % self)
+    elif not self.written.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's bytes sent should be a positive numeric value, received: %s" % self)
+    elif self.delivered_read and not self.delivered_read.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's delivered bytes received should be a positive numeric value, received: %s" % self)
+    elif self.delivered_written and not self.delivered_written.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's delivered bytes sent should be a positive numeric value, received: %s" % self)
+    elif self.overhead_read and not self.overhead_read.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's overhead bytes received should be a positive numeric value, received: %s" % self)
+    elif self.overhead_written and not self.overhead_written.isdigit():
+      raise stem.ProtocolError("A CIRC_BW event's overhead bytes sent should be a positive numeric value, received: %s" % self)
     elif not tor_tools.is_valid_circuit_id(self.id):
       raise stem.ProtocolError("Circuit IDs must be one to sixteen alphanumeric characters, got '%s': %s" % (self.id, self))
 
-    self.read = INT_TYPE(self.read)
-    self.written = INT_TYPE(self.written)
     self.time = self._iso_timestamp(self.time)
 
+    for attr in ('read', 'written', 'delivered_read', 'delivered_written', 'overhead_read', 'overhead_written'):
+      value = getattr(self, attr)
+
+      if value:
+        setattr(self, attr, INT_TYPE(value))
+
 
 class CellStatsEvent(Event):
   """
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
index 87f3e329..69a3624b 100644
--- a/test/unit/response/events.py
+++ b/test/unit/response/events.py
@@ -491,6 +491,7 @@ CIRC_BW_WITH_TIMESTAMP = '650 CIRC_BW ID=11 READ=272 WRITTEN=817 TIME=2012-12-06
 CIRC_BW_BAD_WRITTEN_VALUE = '650 CIRC_BW ID=11 READ=272 WRITTEN=817.7'
 CIRC_BW_BAD_MISSING_ID = '650 CIRC_BW READ=272 WRITTEN=817'
 CIRC_BW_MALFORMED_TIMESTAMP = '650 CIRC_BW ID=11 READ=272 WRITTEN=817 TIME=boom'
+CIRC_BW_WITH_EXTRA_COUNTS = '650 CIRC_BW ID=11 READ=272 WRITTEN=817 TIME=2012-12-06T13:51:11.433755 DELIVERED_READ=12 OVERHEAD_READ=34 DELIVERED_WRITTEN=56 OVERHEAD_WRITTEN=78'
 
 CELL_STATS_1 = '650 CELL_STATS ID=14 \
 OutboundQueue=19403 OutboundConn=15 \
@@ -1524,6 +1525,16 @@ class TestEvents(unittest.TestCase):
     self.assertEqual(817, event.written)
     self.assertEqual(datetime.datetime(2012, 12, 6, 13, 51, 11, 433755), event.time)
 
+    event = _get_event(CIRC_BW_WITH_EXTRA_COUNTS)
+    self.assertEqual('11', event.id)
+    self.assertEqual(272, event.read)
+    self.assertEqual(817, event.written)
+    self.assertEqual(datetime.datetime(2012, 12, 6, 13, 51, 11, 433755), event.time)
+    self.assertEqual(12, event.delivered_read)
+    self.assertEqual(56, event.delivered_written)
+    self.assertEqual(34, event.overhead_read)
+    self.assertEqual(78, event.overhead_written)
+
     self.assertRaises(ProtocolError, _get_event, CIRC_BW_BAD_WRITTEN_VALUE)
     self.assertRaises(ProtocolError, _get_event, CIRC_BW_BAD_MISSING_ID)
     self.assertRaises(ProtocolError, _get_event, CIRC_BW_MALFORMED_TIMESTAMP)



More information about the tor-commits mailing list