<commit_before>import unittest

from docker.errors import NotFound

from useintest.common import docker_client
from useintest.services.builders import DockerisedServiceControllerTypeBuilder

NoopServiceController = DockerisedServiceControllerTypeBuilder(
    name="NoopController",
    repository="alpine",
    ports=[],
    tag="3.6",
    additional_run_settings={"entrypoint": "tail", "command": ["-f", "/etc/hosts"]},
    start_detector=lambda log_line: log_line.strip() != "",
).build()


class TestDockerisedServiceController(unittest.TestCase):
    """
    Tests for `DockerisedServiceController`.
    """
    def setUp(self):
        self._service_controller = NoopServiceController()

    def test_context_manager(self):
        with self._service_controller.start_service() as service:
            self.assertEqual("running", service.container.status)
            container_id = service.container_id
        self.assertIsNone(service.container)
        self.assertRaises(NotFound, docker_client.containers.get, container_id)

    def test_context_manager_exit_when_service_stopped(self):
        with self._service_controller.start_service() as service:
            self._service_controller.stop_service(service)
            self.assertIsNone(service.container)
<commit_msg>Adds unittest to show problem when service goes away on start.<commit_after>import unittest
from copy import copy

from docker.errors import NotFound

from useintest.common import docker_client
from useintest.services.builders import DockerisedServiceControllerTypeBuilder

NoopServiceController = DockerisedServiceControllerTypeBuilder(
    name="NoopController",
    repository="alpine",
    ports=[],
    tag="3.6",
    additional_run_settings={"entrypoint": "tail", "command": ["-f", "/etc/hosts"]},
    start_detector=lambda log_line: log_line.strip() != "",
).build()


class TestDockerisedServiceController(unittest.TestCase):
    """
    Tests for `DockerisedServiceController`.
    """
    def setUp(self):
        self._service_controller = NoopServiceController()

    def test_context_manager(self):
        with self._service_controller.start_service() as service:
            self.assertEqual("running", service.container.status)
            container_id = service.container_id
        self.assertIsNone(service.container)
        self.assertRaises(NotFound, docker_client.containers.get, container_id)

    def test_context_manager_exit_when_service_stopped(self):
        with self._service_controller.start_service() as service:
            self._service_controller.stop_service(service)
            self.assertIsNone(service.container)

    def test_service_stopped_on_start_detection(self):
        ExitingController = DockerisedServiceControllerTypeBuilder(
            name="ExitingController",
            repository="alpine",
            ports=[],
            tag="3.6",
            start_detector=lambda line: False,
        ).build()
        ExitingController().start_service()

{'fraction_non_alphanumeric': 0.09745478901540522, 'fraction_numerical': 0.0020093770931011385, 'mean_word_length': 3.9617940199335546, 'pattern_counts': {'":': 4, '<': 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': '3424480', 'n_tokens_mistral': 820, 'n_tokens_neox': 795, 'n_words': 147}