当前位置: 首页>>代码示例>>Python>>正文


Python Controller.get_effective_rate方法代码示例

本文整理汇总了Python中stem.control.Controller.get_effective_rate方法的典型用法代码示例。如果您正苦于以下问题:Python Controller.get_effective_rate方法的具体用法?Python Controller.get_effective_rate怎么用?Python Controller.get_effective_rate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stem.control.Controller的用法示例。


在下文中一共展示了Controller.get_effective_rate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_effective_rate [as 别名]

#.........这里部分代码省略.........
        )

        response = "".join(["%s\r\n" % " ".join(entry) for entry in valid_streams])

        with patch("stem.control.Controller.get_info", Mock(return_value=response)):
            streams = self.controller.get_streams()
            self.assertEqual(len(valid_streams), len(streams))

            for index, stream in enumerate(streams):
                self.assertEqual(valid_streams[index][0], stream.id)
                self.assertEqual(valid_streams[index][1], stream.status)
                self.assertEqual(valid_streams[index][2], stream.circ_id)
                self.assertEqual(valid_streams[index][3], stream.target)

    def test_attach_stream(self):
        """
    Exercises the attach_stream() method.
    """

        # Response when the stream is in a state where it can't be attached (for
        # instance, it's already open).

        response = stem.response.ControlMessage.from_str("555 Connection is not managed by controller.\r\n")

        with patch("stem.control.Controller.msg", Mock(return_value=response)):
            self.assertRaises(UnsatisfiableRequest, self.controller.attach_stream, "stream_id", "circ_id")

    def test_parse_circ_path(self):
        """
    Exercises the _parse_circ_path() helper function.
    """

        # empty input

        self.assertEqual([], _parse_circ_path(None))
        self.assertEqual([], _parse_circ_path(""))

        # check the pydoc examples

        pydoc_examples = {
            "$999A226EBED397F331B612FE1E4CFAE5C1F201BA=piyaz": [("999A226EBED397F331B612FE1E4CFAE5C1F201BA", "piyaz")],
            "$E57A476CD4DFBD99B4EE52A100A58610AD6E80B9,hamburgerphone,PrivacyRepublic14": [
                ("E57A476CD4DFBD99B4EE52A100A58610AD6E80B9", None),
                (None, "hamburgerphone"),
                (None, "PrivacyRepublic14"),
            ],
        }

        for test_input, expected in pydoc_examples.items():
            self.assertEqual(expected, _parse_circ_path(test_input))

        # exercise with some invalid inputs

        malformed_inputs = [
            "=piyaz",  # no fingerprint
            "999A226EBED397F331B612FE1E4CFAE5C1F201BA=piyaz",  # fingerprint missing prefix
            "$999A226EBED397F331B612FE1E4CFAE5C1F201BAA=piyaz",  # fingerprint too long
            "$999A226EBED397F331B612FE1E4CFAE5C1F201B=piyaz",  # fingerprint too short
            "$999A226EBED397F331B612FE1E4CFAE5C1F201Bz=piyaz",  # invalid character in fingerprint
            "$999A226EBED397F331B612FE1E4CFAE5C1F201BA=",  # no nickname
        ]

        for test_input in malformed_inputs:
            self.assertRaises(ProtocolError, _parse_circ_path, test_input)

    @patch("stem.control.Controller.get_conf")
    def test_get_effective_rate(self, get_conf_mock):
        """
    Exercise the get_effective_rate() method.
    """

        # check default if nothing was set

        get_conf_mock.side_effect = lambda param, **kwargs: {
            "BandwidthRate": "1073741824",
            "BandwidthBurst": "1073741824",
            "RelayBandwidthRate": "0",
            "RelayBandwidthBurst": "0",
            "MaxAdvertisedBandwidth": "1073741824",
        }[param]

        self.assertEqual(1073741824, self.controller.get_effective_rate())
        self.assertEqual(1073741824, self.controller.get_effective_rate(burst=True))

        get_conf_mock.side_effect = ControllerError("nope, too bad")
        self.assertRaises(ControllerError, self.controller.get_effective_rate)
        self.assertEqual("my_default", self.controller.get_effective_rate("my_default"))

    @patch("stem.control.Controller.get_version")
    def test_drop_guards(self, get_version_mock):
        """
    Exercises the drop_guards() method.
    """

        get_version_mock.return_value = stem.version.Version("0.1.0.14")
        self.assertRaises(UnsatisfiableRequest, self.controller.drop_guards)

        with patch("stem.control.Controller.msg", Mock(return_value=None)):
            get_version_mock.return_value = stem.version.Version("0.2.5.2")
            self.controller.drop_guards()
开发者ID:Fuzew,项目名称:sharp-stem,代码行数:104,代码来源:controller.py

示例2: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_effective_rate [as 别名]

#.........这里部分代码省略.........
    response = ''.join(['%s\r\n' % ' '.join(entry) for entry in valid_streams])

    with patch('stem.control.Controller.get_info', Mock(return_value = response)):
      streams = self.controller.get_streams()
      self.assertEqual(len(valid_streams), len(streams))

      for index, stream in enumerate(streams):
        self.assertEqual(valid_streams[index][0], stream.id)
        self.assertEqual(valid_streams[index][1], stream.status)
        self.assertEqual(valid_streams[index][2], stream.circ_id)
        self.assertEqual(valid_streams[index][3], stream.target)

  def test_attach_stream(self):
    """
    Exercises the attach_stream() method.
    """

    # Response when the stream is in a state where it can't be attached (for
    # instance, it's already open).

    response = stem.response.ControlMessage.from_str('555 Connection is not managed by controller.\r\n')

    with patch('stem.control.Controller.msg', Mock(return_value = response)):
      self.assertRaises(UnsatisfiableRequest, self.controller.attach_stream, 'stream_id', 'circ_id')

  def test_parse_circ_path(self):
    """
    Exercises the _parse_circ_path() helper function.
    """

    # empty input

    self.assertEqual([], _parse_circ_path(None))
    self.assertEqual([], _parse_circ_path(''))

    # check the pydoc examples

    pydoc_examples = {
      '$999A226EBED397F331B612FE1E4CFAE5C1F201BA=piyaz':
        [('999A226EBED397F331B612FE1E4CFAE5C1F201BA', 'piyaz')],
      '$E57A476CD4DFBD99B4EE52A100A58610AD6E80B9,hamburgerphone,PrivacyRepublic14':
        [
          ('E57A476CD4DFBD99B4EE52A100A58610AD6E80B9', None),
          (None, 'hamburgerphone'),
          (None, 'PrivacyRepublic14'),
        ],
    }

    for test_input, expected in pydoc_examples.items():
      self.assertEqual(expected, _parse_circ_path(test_input))

    # exercise with some invalid inputs

    malformed_inputs = [
      '=piyaz',  # no fingerprint
      '999A226EBED397F331B612FE1E4CFAE5C1F201BA=piyaz',  # fingerprint missing prefix
      '$999A226EBED397F331B612FE1E4CFAE5C1F201BAA=piyaz',  # fingerprint too long
      '$999A226EBED397F331B612FE1E4CFAE5C1F201B=piyaz',  # fingerprint too short
      '$999A226EBED397F331B612FE1E4CFAE5C1F201Bz=piyaz',  # invalid character in fingerprint
      '$999A226EBED397F331B612FE1E4CFAE5C1F201BA=',  # no nickname
    ]

    for test_input in malformed_inputs:
      self.assertRaises(ProtocolError, _parse_circ_path, test_input)

  @patch('stem.control.Controller.get_conf')
  def test_get_effective_rate(self, get_conf_mock):
    """
    Exercise the get_effective_rate() method.
    """

    # check default if nothing was set

    get_conf_mock.side_effect = lambda param, **kwargs: {
      'BandwidthRate': '1073741824',
      'BandwidthBurst': '1073741824',
      'RelayBandwidthRate': '0',
      'RelayBandwidthBurst': '0',
      'MaxAdvertisedBandwidth': '1073741824',
    }[param]

    self.assertEqual(1073741824, self.controller.get_effective_rate())
    self.assertEqual(1073741824, self.controller.get_effective_rate(burst = True))

    get_conf_mock.side_effect = ControllerError('nope, too bad')
    self.assertRaises(ControllerError, self.controller.get_effective_rate)
    self.assertEqual('my_default', self.controller.get_effective_rate('my_default'))

  @patch('stem.control.Controller.get_version')
  def test_drop_guards(self, get_version_mock):
    """
    Exercises the drop_guards() method.
    """

    get_version_mock.return_value = stem.version.Version('0.1.0.14')
    self.assertRaises(UnsatisfiableRequest, self.controller.drop_guards)

    with patch('stem.control.Controller.msg', Mock(return_value = None)):
      get_version_mock.return_value = stem.version.Version('0.2.5.2')
      self.controller.drop_guards()
开发者ID:patrickod,项目名称:stem,代码行数:104,代码来源:controller.py


注:本文中的stem.control.Controller.get_effective_rate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。