<commit_before># (C) Datadog, Inc. 2010-2016
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

# stdlibb
import time

# 3p
from nose.plugins.attrib import attr

# project
from tests.checks.common import AgentCheckTest

RESULTS_TIMEOUT = 20

CONFIG = {
    'init_config': {},
    'instances': [{
        'host': '127.0.0.1',
        'port': 65530,
        'timeout': 1.5,
        'name': 'DownService'
    }, {
        'host': '192.168.127.12',
        'port': 65530,
        'timeout': 1.5,
        'name': 'DownService2',
        'tags': ['test1']
    }, {
        'host': 'datadoghq.com',
        'port': 80,
        'timeout': 1.5,
        'name': 'UpService',
        'tags': ['test2']
    }, {
        'host': 'datadoghq.com',
        'port': 80,
        'timeout': 1,
        'name': 'response_time',
        'tags': ['test3'],
        'collect_response_time': True
    }]
}


@attr(requires='tcp_check')
class TCPCheckTest(AgentCheckTest):
    CHECK_NAME = 'tcp_check'

    def tearDown(self):
        self.check.stop()

    def wait_for_async(self, method, attribute, count):
        """
        Loop on `self.check.method` until `self.check.attribute >= count`.

        Raise after
        """

        # Check the initial values to see if we already have results before waiting for the async
        # instances to finish
        initial_values = getattr(self, attribute)

        i = 0
        while i < RESULTS_TIMEOUT:
            self.check._process_results()
            if len(getattr(self.check, attribute)) + len(initial_values) >= count:
                return getattr(self.check, method)() + initial_values
            time.sleep(1.1)
            i += 1
        raise Exception("Didn't get the right count of service checks in time, {0}/{1} in {2}s: {3}"
                        .format(len(getattr(self.check, attribute)), count, i,
                                getattr(self.check, attribute)))

    def test_event_deprecation(self):
        """
        Deprecate events usage for service checks.
        """
        # Run the check
        self.run_check(CONFIG)

        # Overrides self.service_checks attribute when values are available
        self.warnings = self.wait_for_async('get_warnings', 'warnings', len(CONFIG['instances']))

        # Assess warnings
        self.assertWarning(
            "Using events for service checks is deprecated in "
            "favor of monitors and will be removed in future versions of the "
            "Datadog Agent.",
            count=len(CONFIG['instances'])
        )

    def test_check(self):
        """
        Check coverage.
        """
        # Run the check
        self.run_check(CONFIG)

        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', len(CONFIG['instances']))
        self.metrics = self.check.get_metrics()

        expected_tags = ["instance:DownService", "target_host:127.0.0.1", "port:65530"]
        self.assertServiceCheckCritical("tcp.can_connect", tags=expected_tags)

        expected_tags = ["instance:DownService2", "target_host:192.168.127.12", "port:65530", "test1"]<commit_msg>tentatively make the tcp_check check less flaky<commit_after># (C) Datadog, Inc. 2010-2016
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

# stdlibb
import time

# 3p
from nose.plugins.attrib import attr

# project
from tests.checks.common import AgentCheckTest

RESULTS_TIMEOUT = 40

CONFIG = {
    'init_config': {},
    'instances': [{
        'host': '127.0.0.1',
        'port': 65530,
        'timeout': 1.5,
        'name': 'DownService',
        'skip_event': True
    }, {
        'host': '192.168.127.12',
        'port': 65530,
        'timeout': 1.5,
        'name': 'DownService2',
        'tags': ['test1'],
        'skip_event': True
    }, {
        'host': 'datadoghq.com',
        'port': 80,
        'timeout': 1.5,
        'name': 'UpService',
        'tags': ['test2'],
        'skip_event': True
    }, {
        'host': 'datadoghq.com',
        'port': 80,
        'timeout': 1,
        'name': 'response_time',
        'tags': ['test3'],
        'collect_response_time': True,
        'skip_event': True
    }]
}

CONFIG_EVENTS = {
    'init_config': {},
    'instances': [{
        'host': '127.0.0.1',
        'port': 65530,
        'timeout': 1.5,
        'name': 'DownService',
        'skip_event': False
    }]
}


@attr(requires='tcp_check')
class TCPCheckTest(AgentCheckTest):
    CHECK_NAME = 'tcp_check'

    def tearDown(self):
        self.check.stop()

    def wait_for_async(self, method, attribute, count):
        """
        Loop on `self.check.method` until `self.check.attribute >= count`.

        Raise after
        """

        # Check the initial values to see if we already have results before waiting for the async
        # instances to finish
        initial_values = getattr(self, attribute)

        i = 0
        while i < RESULTS_TIMEOUT:
            self.check._process_results()
            if len(getattr(self.check, attribute)) + len(initial_values) >= count:
                return getattr(self.check, method)() + initial_values
            time.sleep(1.1)
            i += 1
        raise Exception("Didn't get the right count of service checks in time, {0}/{1} in {2}s: {3}"
                        .format(len(getattr(self.check, attribute)), count, i,
                                getattr(self.check, attribute)))

    def test_event_deprecation(self):
        """
        Deprecate events usage for service checks.
        """
        # Run the check
        self.run_check(CONFIG_EVENTS)

        # Overrides self.service_checks attribute when values are available
        self.warnings = self.wait_for_async('get_warnings', 'warnings', len(CONFIG_EVENTS['instances']))

        # Assess warnings
        self.assertWarning(
            "Using events for service checks is deprecated in "
            "favor of monitors and will be removed in future versions of the "
            "Datadog Agent.",
            count=len(CONFIG_EVENTS['instances'])
        )

    def test_check(self):
        """
        Check coverage.
        """
        # Run the check
        self.run_check(CONFIG)

        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks', 'service_checks', len(CONFIG['instances']))
        self.metrics = self.check.get_metrics()

        expected_tags = ["instance:DownService", "target_host:127.0.0.1", "port:65530"]
        self.assertServiceCheckCritical("tcp.can_connect", tags=expected_tags)

        expected_tags = ["instance:DownService2", "target_host:192.168.127.12", "port:65530", "test1"]
{'fraction_non_alphanumeric': 0.1284981684981685, 'fraction_numerical': 0.02827838827838828, 'mean_word_length': 2.2551263710061993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 9, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6584280', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2026, 'n_words': 498}