本文整理汇总了Python中effect.testing.SequenceDispatcher类的典型用法代码示例。如果您正苦于以下问题:Python SequenceDispatcher类的具体用法?Python SequenceDispatcher怎么用?Python SequenceDispatcher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SequenceDispatcher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_print
def test_print(self):
sequence = SequenceDispatcher([
(Print('What... is your quest?'), lambda _:None),
])
with sequence.consume():
sync_perform(sequence, program())
示例2: test_throttling
def test_throttling(self):
"""
When the throttler function returns a bracketing function, it's used to
throttle the request.
"""
def throttler(stype, method, tid):
if (stype == ServiceType.CLOUD_SERVERS and
method == 'get' and tid == 1):
return bracket
bracket = object()
svcreq = service_request(
ServiceType.CLOUD_SERVERS, 'GET', 'servers').intent
response = stub_pure_response({}, 200)
seq = SequenceDispatcher([
(_Throttle(bracket=bracket, effect=mock.ANY),
nested_sequence([
(Authenticate(authenticator=self.authenticator,
tenant_id=1,
log=self.log),
lambda i: ('token', fake_service_catalog)),
(Request(method='GET', url='http://dfw.openstack/servers',
headers=headers('token'), log=self.log),
lambda i: response),
])),
])
eff = self._concrete(svcreq, throttler=throttler)
with seq.consume():
result = sync_perform(seq, eff)
self.assertEqual(result, (response[0], {}))
示例3: test_echo
def test_echo(self, line):
sequence = SequenceDispatcher([
(Print('What... is your quest?'), lambda _:None),
(Readline(), lambda _:line),
(Print(line), lambda _:None),
])
with sequence.consume():
sync_perform(sequence, echo())
示例4: test_without_metrics
def test_without_metrics(self):
"""
Doesnt add metrics to blueflood if metrics config is not there
"""
sequence = SequenceDispatcher([(GetAllValidGroups(), const(self.groups))])
self.get_dispatcher.return_value = sequence
del self.config["metrics"]
with sequence.consume():
d = collect_metrics("reactor", self.config, self.log)
self.assertEqual(self.successResultOf(d), "metrics")
self.assertFalse(self.add_to_cloud_metrics.called)
示例5: test_challenge
def test_challenge(self, line):
sequence = SequenceDispatcher([
(Print('What... is your quest?'), lambda _:None),
(Readline(), lambda _: line),
(Print('What... is your quest?'), lambda _:None),
(Readline(), lambda _:'To seek the Holy Grail.\n'),
(Print('What... is your favourite colour?'), lambda _:None),
])
with sequence.consume():
dispatcher = ComposedDispatcher([
sequence,
base_dispatcher,
])
sync_perform(dispatcher, challenge())
示例6: test_remove_nodes_from_clb_success_failures
def test_remove_nodes_from_clb_success_failures(self):
"""
:obj:`AddNodesToCLB` succeeds if the CLB is not in existence (has been
deleted or is not found).
"""
successes = [CLBNotFoundError(lb_id=u'12345'),
CLBDeletedError(lb_id=u'12345'),
NoSuchCLBError(lb_id=u'12345')]
eff = RemoveNodesFromCLB(lb_id='12345',
node_ids=pset(['1', '2'])).as_effect()
for exc in successes:
seq = SequenceDispatcher([(eff.intent, lambda i: raise_(exc))])
with seq.consume():
self.assertEquals(sync_perform(seq, eff),
(StepResult.SUCCESS, []))
示例7: test_add_nodes_to_clb_success_response_codes
def test_add_nodes_to_clb_success_response_codes(self):
"""
:obj:`AddNodesToCLB` succeeds on 202.
"""
eff = self._add_one_node_to_clb()
seq = SequenceDispatcher([
(eff.intent, lambda i: (StubResponse(202, {}), '')),
(Log(ANY, ANY), lambda _: None)
])
expected = (
StepResult.RETRY,
[ErrorReason.String('must re-gather after adding to CLB in order '
'to update the active cache')])
with seq.consume():
self.assertEquals(sync_perform(seq, eff), expected)
示例8: test_remove_nodes_from_clb_terminal_failures
def test_remove_nodes_from_clb_terminal_failures(self):
"""
:obj:`AddNodesToCLB` fails if there are any 4xx errors, then
the error is propagated up and the result is a failure.
"""
terminals = (APIError(code=403, body="You're out of luck."),
APIError(code=422, body="Oh look another 422."))
eff = RemoveNodesFromCLB(lb_id='12345',
node_ids=pset(['1', '2'])).as_effect()
for exc in terminals:
seq = SequenceDispatcher([(eff.intent, lambda i: raise_(exc))])
with seq.consume():
self.assertEquals(
sync_perform(seq, eff),
(StepResult.FAILURE, [ErrorReason.Exception(
matches(ContainsAll([type(exc), exc])))]))
示例9: test_performs_tenant_scope
def test_performs_tenant_scope(self, deferred_lock_run):
"""
:func:`perform_tenant_scope` performs :obj:`TenantScope`, and uses the
default throttler
"""
# We want to ensure
# 1. the TenantScope can be performed
# 2. the ServiceRequest is run within a lock, since it matches the
# default throttling policy
set_config_data(
{"cloud_client": {"throttling": {"create_server_delay": 1,
"delete_server_delay": 0.4}}})
self.addCleanup(set_config_data, {})
clock = Clock()
authenticator = object()
log = object()
dispatcher = get_cloud_client_dispatcher(clock, authenticator, log,
make_service_configs())
svcreq = service_request(ServiceType.CLOUD_SERVERS, 'POST', 'servers')
tscope = TenantScope(tenant_id='111', effect=svcreq)
def run(f, *args, **kwargs):
result = f(*args, **kwargs)
result.addCallback(
lambda x: (x[0], assoc(x[1], 'locked', True)))
return result
deferred_lock_run.side_effect = run
response = stub_pure_response({}, 200)
seq = SequenceDispatcher([
(Authenticate(authenticator=authenticator,
tenant_id='111', log=log),
lambda i: ('token', fake_service_catalog)),
(Request(method='POST', url='http://dfw.openstack/servers',
headers=headers('token'), log=log),
lambda i: response),
])
disp = ComposedDispatcher([seq, dispatcher])
with seq.consume():
result = perform(disp, Effect(tscope))
self.assertNoResult(result)
clock.advance(1)
self.assertEqual(self.successResultOf(result),
(response[0], {'locked': True}))
示例10: test_remove_nodes_from_clb_non_terminal_failures_to_retry
def test_remove_nodes_from_clb_non_terminal_failures_to_retry(self):
"""
:obj:`RemoveNodesFromCLB` retries if the CLB is temporarily locked,
or if the request was rate-limited, or if there was an API error and
the error is unknown but not a 4xx.
"""
non_terminals = (CLBImmutableError(lb_id=u"12345"),
CLBRateLimitError(lb_id=u"12345"),
APIError(code=500, body="oops!"),
TypeError("You did something wrong in your code."))
eff = RemoveNodesFromCLB(lb_id='12345',
node_ids=pset(['1', '2'])).as_effect()
for exc in non_terminals:
seq = SequenceDispatcher([(eff.intent, lambda i: raise_(exc))])
with seq.consume():
self.assertEquals(
sync_perform(seq, eff),
(StepResult.RETRY, [ErrorReason.Exception(
matches(ContainsAll([type(exc), exc])))]))
示例11: test_add_nodes_to_clb_terminal_failures
def test_add_nodes_to_clb_terminal_failures(self):
"""
:obj:`AddNodesToCLB` fails if the CLB is not found or deleted, or
if there is any other 4xx error, then
the error is propagated up and the result is a failure.
"""
terminals = (CLBNotFoundError(lb_id=u"12345"),
CLBDeletedError(lb_id=u"12345"),
NoSuchCLBError(lb_id=u"12345"),
CLBNodeLimitError(lb_id=u"12345", node_limit=25),
APIError(code=403, body="You're out of luck."),
APIError(code=422, body="Oh look another 422."))
eff = self._add_one_node_to_clb()
for exc in terminals:
seq = SequenceDispatcher([(eff.intent, lambda i: raise_(exc))])
with seq.consume():
self.assertEquals(
sync_perform(seq, eff),
(StepResult.FAILURE, [ErrorReason.Exception(
matches(ContainsAll([type(exc), exc])))]))
示例12: setUp
def setUp(self):
"""
mock dependent functions
"""
self.connect_cass_servers = patch(
self, 'otter.metrics.connect_cass_servers')
self.client = mock.Mock(spec=['disconnect'])
self.client.disconnect.return_value = succeed(None)
self.connect_cass_servers.return_value = self.client
self.log = mock_log()
self.get_all_metrics = patch(self, 'otter.metrics.get_all_metrics',
return_value=succeed("metrics"))
self.groups = [
{"tenantId": "t1", "groupId": "g1",
"launch_config": '{"type": "launch_server"}'},
{"tenantId": "t1", "groupId": "g2",
"launch_config": '{"type": "launch_server"}'},
{"tenantId": "t1", "groupId": "g12",
"launch_config": '{"type": "launch_stack"}'},
{"tenantId": "t3", "groupId": "g3",
"launch_config": '{"type": "launch_stack"}'},
{"tenantId": "t2", "groupId": "g11",
"launch_config": '{"type": "launch_server"}'}]
self.lc_groups = {"t1": self.groups[:2], "t2": [self.groups[-1]]}
self.add_to_cloud_metrics = patch(
self, 'otter.metrics.add_to_cloud_metrics',
side_effect=intent_func("atcm"))
self.config = {'cassandra': 'c', 'identity': identity_config,
'metrics': {'service': 'ms', 'tenant_id': 'tid',
'region': 'IAD',
'ttl': 200, "last_tenant_fpath": "lpath"},
'region': 'r', 'cloudServersOpenStack': 'nova',
'cloudLoadBalancers': 'clb',
'cloudOrchestration': 'orch',
'rackconnect': 'rc',
"non-convergence-tenants": ["ct"]}
self.sequence = SequenceDispatcher([
(GetAllValidGroups(), const(self.groups)),
(TenantScope(mock.ANY, "tid"),
nested_sequence([
(("atcm", 200, "r", "metrics", 2, self.config,
self.log, False), noop)
]))
])
self.get_dispatcher = patch(self, "otter.metrics.get_dispatcher",
return_value=self.sequence)
示例13: setUp
def setUp(self):
"""
mock dependent functions
"""
self.connect_cass_servers = patch(self, "otter.metrics.connect_cass_servers")
self.client = mock.Mock(spec=["disconnect"])
self.client.disconnect.return_value = succeed(None)
self.connect_cass_servers.return_value = self.client
self.log = mock_log()
self.get_all_metrics = patch(self, "otter.metrics.get_all_metrics", return_value=succeed("metrics"))
self.groups = [
{"tenantId": "t1", "groupId": "g1", "launch_config": '{"type": "launch_server"}'},
{"tenantId": "t1", "groupId": "g2", "launch_config": '{"type": "launch_server"}'},
{"tenantId": "t1", "groupId": "g12", "launch_config": '{"type": "launch_stack"}'},
{"tenantId": "t3", "groupId": "g3", "launch_config": '{"type": "launch_stack"}'},
{"tenantId": "t2", "groupId": "g11", "launch_config": '{"type": "launch_server"}'},
]
self.lc_groups = {"t1": self.groups[:2], "t2": [self.groups[-1]]}
self.add_to_cloud_metrics = patch(self, "otter.metrics.add_to_cloud_metrics", side_effect=intent_func("atcm"))
self.config = {
"cassandra": "c",
"identity": identity_config,
"metrics": {"service": "ms", "tenant_id": "tid", "region": "IAD", "ttl": 200, "last_tenant_fpath": "lpath"},
"region": "r",
"cloudServersOpenStack": "nova",
"cloudLoadBalancers": "clb",
"cloudOrchestration": "orch",
"rackconnect": "rc",
"non-convergence-tenants": ["ct"],
}
self.sequence = SequenceDispatcher(
[
(GetAllValidGroups(), const(self.groups)),
(
TenantScope(mock.ANY, "tid"),
nested_sequence([(("atcm", 200, "r", "metrics", 2, self.config, self.log, False), noop)]),
),
]
)
self.get_dispatcher = patch(self, "otter.metrics.get_dispatcher", return_value=self.sequence)
示例14: setUp
def setUp(self):
"""
mock dependent functions
"""
self.connect_cass_servers = patch(
self, 'otter.metrics.connect_cass_servers')
self.client = mock.Mock(spec=['disconnect'])
self.client.disconnect.return_value = succeed(None)
self.connect_cass_servers.return_value = self.client
self.get_todays_scaling_groups = patch(
self, 'otter.metrics.get_todays_scaling_groups',
side_effect=intent_func("gtsg"))
self.log = mock_log()
self.metrics = [GroupMetrics('t', 'g1', 3, 2, 0),
GroupMetrics('t2', 'g1', 4, 4, 1),
GroupMetrics('t2', 'g', 100, 20, 0)]
self.get_all_metrics = patch(self, 'otter.metrics.get_all_metrics',
return_value=succeed(self.metrics))
self.groups = {"t": "t1group", "t2": "2 groups"}
self.add_to_cloud_metrics = patch(
self, 'otter.metrics.add_to_cloud_metrics',
side_effect=intent_func("atcm"))
self.config = {'cassandra': 'c', 'identity': identity_config,
'metrics': {'service': 'ms', 'tenant_id': 'tid',
'region': 'IAD',
'ttl': 200, "last_tenant_fpath": "lpath"},
'region': 'r', 'cloudServersOpenStack': 'nova',
'cloudLoadBalancers': 'clb', 'rackconnect': 'rc',
"convergence-tenants": ["ct"]}
self.sequence = SequenceDispatcher([
(("gtsg", ["ct"], "lpath"), const(self.groups)),
(TenantScope(mock.ANY, "tid"),
nested_sequence([
(("atcm", 200, "r", 107, 26, 1, 2, 3, self.log), noop)
]))
])
self.get_dispatcher = patch(self, "otter.metrics.get_dispatcher",
return_value=self.sequence)
示例15: CollectMetricsTests
class CollectMetricsTests(SynchronousTestCase):
"""
Tests for :func:`collect_metrics`
"""
def setUp(self):
"""
mock dependent functions
"""
self.connect_cass_servers = patch(self, "otter.metrics.connect_cass_servers")
self.client = mock.Mock(spec=["disconnect"])
self.client.disconnect.return_value = succeed(None)
self.connect_cass_servers.return_value = self.client
self.log = mock_log()
self.get_all_metrics = patch(self, "otter.metrics.get_all_metrics", return_value=succeed("metrics"))
self.groups = [
{"tenantId": "t1", "groupId": "g1", "launch_config": '{"type": "launch_server"}'},
{"tenantId": "t1", "groupId": "g2", "launch_config": '{"type": "launch_server"}'},
{"tenantId": "t1", "groupId": "g12", "launch_config": '{"type": "launch_stack"}'},
{"tenantId": "t3", "groupId": "g3", "launch_config": '{"type": "launch_stack"}'},
{"tenantId": "t2", "groupId": "g11", "launch_config": '{"type": "launch_server"}'},
]
self.lc_groups = {"t1": self.groups[:2], "t2": [self.groups[-1]]}
self.add_to_cloud_metrics = patch(self, "otter.metrics.add_to_cloud_metrics", side_effect=intent_func("atcm"))
self.config = {
"cassandra": "c",
"identity": identity_config,
"metrics": {"service": "ms", "tenant_id": "tid", "region": "IAD", "ttl": 200, "last_tenant_fpath": "lpath"},
"region": "r",
"cloudServersOpenStack": "nova",
"cloudLoadBalancers": "clb",
"cloudOrchestration": "orch",
"rackconnect": "rc",
"non-convergence-tenants": ["ct"],
}
self.sequence = SequenceDispatcher(
[
(GetAllValidGroups(), const(self.groups)),
(
TenantScope(mock.ANY, "tid"),
nested_sequence([(("atcm", 200, "r", "metrics", 2, self.config, self.log, False), noop)]),
),
]
)
self.get_dispatcher = patch(self, "otter.metrics.get_dispatcher", return_value=self.sequence)
def test_metrics_collected(self):
"""
Metrics is collected after getting groups from cass and servers
from nova and it is added to blueflood
"""
_reactor = mock.Mock()
with self.sequence.consume():
d = collect_metrics(_reactor, self.config, self.log)
self.assertEqual(self.successResultOf(d), "metrics")
self.connect_cass_servers.assert_called_once_with(_reactor, "c")
self.get_all_metrics.assert_called_once_with(
self.get_dispatcher.return_value, self.lc_groups, self.log, _print=False
)
self.client.disconnect.assert_called_once_with()
def test_with_client(self):
"""
Uses client provided and does not disconnect it before returning
"""
client = mock.Mock(spec=["disconnect"])
with self.sequence.consume():
d = collect_metrics("reactr", self.config, self.log, client=client)
self.assertEqual(self.successResultOf(d), "metrics")
self.assertFalse(self.connect_cass_servers.called)
self.assertFalse(client.disconnect.called)
def test_with_authenticator(self):
"""
Uses authenticator provided instead of creating new
"""
_reactor, auth = mock.Mock(), mock.Mock()
with self.sequence.consume():
d = collect_metrics(_reactor, self.config, self.log, authenticator=auth)
self.assertEqual(self.successResultOf(d), "metrics")
self.get_dispatcher.assert_called_once_with(_reactor, auth, self.log, mock.ANY, mock.ANY)
def test_without_metrics(self):
"""
Doesnt add metrics to blueflood if metrics config is not there
"""
sequence = SequenceDispatcher([(GetAllValidGroups(), const(self.groups))])
self.get_dispatcher.return_value = sequence
del self.config["metrics"]
with sequence.consume():
d = collect_metrics("reactor", self.config, self.log)
self.assertEqual(self.successResultOf(d), "metrics")
self.assertFalse(self.add_to_cloud_metrics.called)