def test_getWeightedNodes(self): mock_node_1 = mock.Mock() mock_node_2 = mock.Mock() mock_node_3 = mock.Mock() mock_nodes = [ mock_node_1, mock_node_2, mock_node_3, ] mock_weights = { mock_node_1: 1.0, mock_node_2: 2.0, mock_node_3: 3.0, } ret = path_util.getWeightedNodes(mock_nodes, mock_weights) expected = [ (mock_node_1, 1.0 / 6.0), (mock_node_2, 3.0 / 6.0), (mock_node_3, 6.0 / 6.0), ] self.assertEqual(ret, expected)
def test_getWeightedNodes(self): mock_node_1 = mock.Mock() mock_node_2 = mock.Mock() mock_node_3 = mock.Mock() mock_nodes = [ mock_node_1, mock_node_2, mock_node_3, ] mock_weights = { mock_node_1: 1.0, mock_node_2: 2.0, mock_node_3: 3.0, } ret = path_util.getWeightedNodes(mock_nodes, mock_weights) expected = [ (mock_node_1, 1.0/6.0), (mock_node_2, 3.0/6.0), (mock_node_3, 6.0/6.0), ] self.assertEqual(ret, expected)
def selectExitNode(bw_weights, bwweightscale, cons_rel_stats, descriptors, fast, stable, internal, port): exits = filterExits(cons_rel_stats, descriptors, fast, stable, internal, port) if len(exits) == 0: raise ValueError("No usable exit nodes for requested path.") if len(exits) == 1: return exits[0] weight_pos = 'm' if internal is True else 'e' weights = path_util.getPositionWeights(exits, cons_rel_stats, weight_pos, bw_weights, bwweightscale) weighted_exits = path_util.getWeightedNodes(exits, weights) return path_util.selectWeightedNode(weighted_exits)
def selectMiddleNode(bw_weights, bwweightscale, cons_rel_stats, descriptors, fast, stable, exit_desc, exit_status_entry, guard_desc, guard_status_entry): middles = filterMiddles(cons_rel_stats, descriptors, fast, stable, exit_desc, exit_status_entry, guard_desc, guard_status_entry) if len(middles) == 0: raise ValueError("No usable middle nodes for requested path.") if len(middles) == 1: return middles[0] weights = path_util.getPositionWeights(middles, cons_rel_stats, 'm', bw_weights, bwweightscale) weighted_middles = path_util.getWeightedNodes(middles, weights) return path_util.selectWeightedNode(weighted_middles)