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
def test_integer_code_default(): setter = CommandProperties.setter() properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') result1 = CommandProperties.default_metrics_rolling_statistical_window result2 = properties.metrics_rolling_statistical_window_in_milliseconds() assert result1 == result2
def test_boolean_setter_override2(): setter = CommandProperties.setter().with_circuit_breaker_force_closed( False) properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') # The setter override should take precedence over default assert False == properties.circuit_breaker_force_closed()
def test_integer_setter_override(): setter = CommandProperties.setter( ).with_metrics_rolling_statistical_window_in_milliseconds(5000) properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') # The setter override should take precedence over default_value assert 5000 == properties.metrics_rolling_statistical_window_in_milliseconds( )
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
def get_unit_test_properties_setter(): return CommandProperties.setter() \ .with_execution_timeout_in_milliseconds(1000) \ .with_execution_isolation_strategy(0) \ .with_execution_isolation_thread_interrupt_on_timeout(True) \ .with_circuit_breaker_force_open(False) \ .with_circuit_breaker_error_threshold_percentage(40) \ .with_metrics_rolling_statistical_window_in_milliseconds(5000) \ .with_metrics_rolling_statistical_window_buckets(5) \ .with_circuit_breaker_request_volume_threshold(0) \ .with_circuit_breaker_sleep_window_in_milliseconds(5000000) \ .with_circuit_breaker_enabled(True) \ .with_request_log_enabled(True) \ .with_execution_isolation_semaphore_max_concurrent_requests(20) \ .with_fallback_isolation_semaphore_max_concurrent_requests(10) \ .with_fallback_enabled(True) \ .with_circuit_breaker_force_closed(False) \ .with_metrics_rolling_percentile_enabled(True) \ .with_request_cache_enabled(True) \ .with_metrics_rolling_percentile_window_in_milliseconds(60000) \ .with_metrics_rolling_percentile_window_buckets(12) \ .with_metrics_rolling_percentile_bucket_size(1000) \ .with_metrics_health_snapshot_interval_in_milliseconds(0)
def test_integer_setter_override(): setter = CommandProperties.setter().with_metrics_rolling_statistical_window_in_milliseconds(5000) properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') # The setter override should take precedence over default_value assert 5000 == properties.metrics_rolling_statistical_window_in_milliseconds()
def test_boolean_code_default(): setter = CommandProperties.setter() properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') assert CommandProperties.default_circuit_breaker_force_closed == properties.circuit_breaker_force_closed()
def test_boolean_setter_override2(): setter = CommandProperties.setter().with_circuit_breaker_force_closed(False) properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') # The setter override should take precedence over default assert False == properties.circuit_breaker_force_closed()
from hystrix.metrics import Metrics from hystrix.command_properties import CommandProperties from hystrix.rolling_number import RollingNumber, RollingNumberEvent setter = CommandProperties.setter() properties = CommandProperties('TEST', setter, 'unit_test_prefix') counter = RollingNumber(properties.metrics_rolling_statistical_window_in_milliseconds(), properties.metrics_rolling_statistical_window_buckets()) def test_metrics_cumulative_count(): metrics = Metrics(counter) assert metrics.cumulative_count(RollingNumberEvent.THREAD_MAX_ACTIVE) == 0 def test_metrics_rolling_count(): metrics = Metrics(counter) assert metrics.rolling_count(RollingNumberEvent.SUCCESS) == 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)
def test_boolean_code_default(): setter = CommandProperties.setter() properties = PropertiesCommandTest('TEST', setter, 'unitTestPrefix') assert CommandProperties.default_circuit_breaker_force_closed == properties.circuit_breaker_force_closed( )
def as_mock(setter): return CommandProperties('TEST', setter, 'unit_test_prefix')
from hystrix.metrics import Metrics from hystrix.command_properties import CommandProperties from hystrix.rolling_number import RollingNumber, RollingNumberEvent setter = CommandProperties.setter() properties = CommandProperties('TEST', setter, 'unit_test_prefix') counter = RollingNumber( properties.metrics_rolling_statistical_window_in_milliseconds(), properties.metrics_rolling_statistical_window_buckets()) def test_metrics_cumulative_count(): metrics = Metrics(counter) assert metrics.cumulative_count(RollingNumberEvent.THREAD_MAX_ACTIVE) == 0 def test_metrics_rolling_count(): metrics = Metrics(counter) assert metrics.rolling_count(RollingNumberEvent.SUCCESS) == 0