Пример #1
0
    def __new__(cls, name, bases, attrs):
        # Command key initialization
        command_key = attrs.get('command_key') or name
        new_class = type.__new__(cls, command_key, bases, attrs)

        if name in cls.__blacklist__:
            return new_class

        # TODO: Check instance CommandProperties here?
        command_properties_defaults = attrs.get('command_properties_defaults')
        if command_properties_defaults is None:
            command_properties_defaults = CommandProperties.setter()

        # Properties initialization
        properties_strategy = attrs.get('properties_strategy')
        if properties_strategy is None:
            properties_strategy = CommandProperties(
                command_key, command_properties_defaults)

        setattr(new_class, 'properties', properties_strategy)

        # Pool key
        # This defines which pool this command should run on.
        # It uses the pool_key if provided, then defaults to use Group key.
        # It can then be overridden by a property if defined so it can be
        # changed at runtime.
        pool_key = attrs.get('pool_key')

        # Group key initialization
        group_key = attrs.get('group_key') or '{}Group'.format(command_key)
        NewGroup = type(group_key, (Group, ),
                        dict(group_key=group_key, pool_key=pool_key))

        setattr(new_class, 'group', NewGroup())
        setattr(new_class, 'group_key', group_key)
        setattr(new_class, 'command_key', command_key)

        # Metrics initialization
        command_metrics_key = '{}CommandMetrics'.format(command_key)
        # TODO: Check instance CommandMetrics here?
        metrics = attrs.get('metrics')
        if metrics is None:
            NewCommandMetrics = type(
                command_metrics_key, (CommandMetrics, ),
                dict(command_metrics_key=command_metrics_key,
                     group_key=group_key,
                     pool_key=pool_key))
            metrics = NewCommandMetrics(properties=properties_strategy)

        setattr(new_class, 'metrics', metrics)

        return new_class
Пример #2
0
import time

from hystrix.command import Command
from hystrix.command_metrics import CommandMetrics
from hystrix.command_properties import CommandProperties
from hystrix.strategy.eventnotifier.event_notifier_default import (
    EventNotifierDefault)

from .test_command_properties import get_unit_test_properties_setter, as_mock

setter = CommandProperties.setter()
properties = CommandProperties('TEST', setter, 'unit_test_prefix')
event_notifier = EventNotifierDefault.get_instance()


def test_default_command_metrics_key():
    class Test(CommandMetrics):
        pass

    commandmetrics = Test(None, 'command_group', None, properties,
                          event_notifier)
    assert commandmetrics.command_metrics_key == 'TestCommandMetrics'


def test_manual_command_metrics_key():
    class Test(CommandMetrics):
        command_metrics_key = 'MyTestCommandMetrics'
        pass

    commandmetrics = Test(None, 'command_group', None, properties,
                          event_notifier)
Пример #3
0
def as_mock(setter):
    return CommandProperties('TEST', setter, 'unit_test_prefix')