<commit_before>import unittest

import socket
import requests

import pygen
import http_manager


class HttpManagerTest(unittest.TestCase):
    def setUp(self):
        self.generate_count = 0

        self.app = pygen.PyGen(template='#', swarm_manager=True)

        def counting_generate():
            self.generate_count += 1
            return 'Generated %d times' % self.generate_count

        self.app.generate = counting_generate

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

    def test_http_post_triggers_update(self):
        status, data = self._call_updater()

        self.assertEqual(200, status)
        self.assertEqual('OK', data)

        self.assertEqual(1, self.generate_count)

    def test_multiple_calls(self):
        self._call_updater()
        self._call_updater()
        self._call_updater()

        self.assertEqual(3, self.generate_count)

    def _call_updater(self):
        response = requests.post('http://localhost:%d' % self.app.swarm_manager.port)

        return response.status_code, response.text.strip()

    def test_retries(self):
        manager = http_manager.Manager(None, ['test'], 3)

        calls = list()

        def mock_action(*args):
            calls.append(1)
            return 500, 'Error'
        
        manager._send_action_request = mock_action

        def mock_getaddrinfo(*args):
            yield (0, 0, 0, 0, ('test-host', 1234))

        socket.getaddrinfo = mock_getaddrinfo

        manager.send_action('signal', 'test', 'HUP')

        self.assertEqual(4, sum(calls))
<commit_msg>Fix unit tests<commit_after>import unittest

import socket
import requests

import pygen
import http_manager


class HttpManagerTest(unittest.TestCase):
    def setUp(self):
        self.generate_count = 0

        self.app = pygen.PyGen(template='#', swarm_manager=True)

        def counting_generate():
            self.generate_count += 1
            return 'Generated %d times' % self.generate_count

        self.app.generate = counting_generate

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

    def test_http_post_triggers_update(self):
        status, data = self._call_updater()

        self.assertEqual(200, status)
        self.assertEqual('OK', data)

        self.assertEqual(1, self.generate_count)

    def test_multiple_calls(self):
        self._call_updater()
        self._call_updater()
        self._call_updater()

        self.assertEqual(3, self.generate_count)

    def _call_updater(self):
        response = requests.post('http://localhost:%d' % self.app.swarm_manager.port)

        return response.status_code, response.text.strip()

    def test_retries(self):
        manager = http_manager.Manager(None, ['test'], 3)

        calls = list()

        def mock_action(*args):
            calls.append(1)
            return 500, 'Error'
        
        manager._send_action_request = mock_action

        def mock_getaddrinfo(*args):
            yield (0, 0, 0, 0, ('test-host', 1234))
        
        original_getaddrinfo = socket.getaddrinfo

        try:
            socket.getaddrinfo = mock_getaddrinfo

            manager.send_action('signal', 'test', 'HUP')

            self.assertEqual(4, sum(calls))

        finally:
            socket.getaddrinfo = original_getaddrinfo

{'fraction_non_alphanumeric': 0.10606522401706797, 'fraction_numerical': 0.012800975312404754, 'mean_word_length': 2.4656810982048576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '4410307', 'n_tokens_mistral': 1010, 'n_tokens_neox': 959, 'n_words': 190}