<commit_before>import asyncio
import pytest

from lightbus.api import registry, Api, Event
from lightbus.bus import BusNode
from lightbus.plugins.state import StatePlugin
from lightbus.transports.debug import DebugEventTransport


pytestmark = pytest.mark.unit


class TestApi(Api):
    my_event = Event(arguments=[])

    class Meta:
        name = 'example.test'


@pytest.mark.run_loop
async def test_before_server_start(dummy_bus: BusNode, loop, event_consumer):
    registry.add(TestApi())
    await dummy_bus.example.test.my_event.listen_async(lambda: None)

    state_plugin = StatePlugin()
    state_plugin.do_ping = False
    await state_plugin.before_server_start(bus_client=dummy_bus.bus_client, loop=loop)

    event_messages = event_consumer()
    assert len(event_messages) == 1
    event_message = event_messages[0]

    assert event_message.api_name == 'internal.state'
    assert event_message.event_name == 'server_started'

    assert event_message.kwargs['api_names'] == ['example.test']
    assert event_message.kwargs['listening_for'] == ['example.test.my_event']
    assert event_message.kwargs['metrics_enabled'] == False
    assert event_message.kwargs['ping_enabled'] == False
    assert event_message.kwargs['ping_interval'] == 60


@pytest.mark.run_loop
async def test_ping(dummy_bus: BusNode, loop, event_consumer):
    # We check the pings message contains a list of registries, so register one
    registry.add(TestApi())
    # Likewise for event listeners
    await dummy_bus.example.test.my_event.listen_async(lambda: None)

    # Let the state plugin send a ping then cancel it
    state_plugin = StatePlugin()
    state_plugin.ping_interval = 0.1
    task = asyncio.ensure_future(
        state_plugin._send_ping(bus_client=dummy_bus.bus_client),
        loop=loop
    )
    await asyncio.sleep(0.15)
    task.cancel()

    event_messages = event_consumer()

    assert len(event_messages) == 1
    event_message = event_messages[0]

    assert event_message.api_name == 'internal.state'
    assert event_message.event_name == 'server_ping'

    assert event_message.kwargs['api_names'] == ['example.test']
    assert event_message.kwargs['listening_for'] == ['example.test.my_event']
    assert event_message.kwargs['metrics_enabled'] == False
    assert event_message.kwargs['ping_enabled'] == True
    assert event_message.kwargs['ping_interval'] == 0.1


@pytest.mark.run_loop
async def test_after_server_stopped(dummy_bus: BusNode, loop, mocker):
    async def dummy_coroutine(*args, **kwargs):
        pass
    m = mocker.patch.object(DebugEventTransport, 'send_event', return_value=dummy_coroutine())

    registry.add(TestApi())
    await dummy_bus.example.test.my_event.listen_async(lambda: None)

    await StatePlugin().after_server_stopped(bus_client=dummy_bus.bus_client, loop=loop)
    assert m.called
    (event_message, ), _ = m.call_args
    assert event_message.api_name == 'internal.state'
    assert event_message.event_name == 'server_stopped'<commit_msg>Refactoring test_after_server_stopped()<commit_after>import asyncio
import pytest

from lightbus.api import registry, Api, Event
from lightbus.bus import BusNode
from lightbus.plugins.state import StatePlugin
from lightbus.transports.debug import DebugEventTransport


pytestmark = pytest.mark.unit


class TestApi(Api):
    my_event = Event(arguments=[])

    class Meta:
        name = 'example.test'


@pytest.mark.run_loop
async def test_before_server_start(dummy_bus: BusNode, loop, event_consumer):
    registry.add(TestApi())
    await dummy_bus.example.test.my_event.listen_async(lambda: None)

    state_plugin = StatePlugin()
    state_plugin.do_ping = False
    await state_plugin.before_server_start(bus_client=dummy_bus.bus_client, loop=loop)

    event_messages = event_consumer()
    assert len(event_messages) == 1
    event_message = event_messages[0]

    assert event_message.api_name == 'internal.state'
    assert event_message.event_name == 'server_started'

    assert event_message.kwargs['api_names'] == ['example.test']
    assert event_message.kwargs['listening_for'] == ['example.test.my_event']
    assert event_message.kwargs['metrics_enabled'] == False
    assert event_message.kwargs['ping_enabled'] == False
    assert event_message.kwargs['ping_interval'] == 60


@pytest.mark.run_loop
async def test_ping(dummy_bus: BusNode, loop, event_consumer):
    # We check the pings message contains a list of registries, so register one
    registry.add(TestApi())
    # Likewise for event listeners
    await dummy_bus.example.test.my_event.listen_async(lambda: None)

    # Let the state plugin send a ping then cancel it
    state_plugin = StatePlugin()
    state_plugin.ping_interval = 0.1
    task = asyncio.ensure_future(
        state_plugin._send_ping(bus_client=dummy_bus.bus_client),
        loop=loop
    )
    await asyncio.sleep(0.15)
    task.cancel()

    event_messages = event_consumer()

    assert len(event_messages) == 1
    event_message = event_messages[0]

    assert event_message.api_name == 'internal.state'
    assert event_message.event_name == 'server_ping'

    assert event_message.kwargs['api_names'] == ['example.test']
    assert event_message.kwargs['listening_for'] == ['example.test.my_event']
    assert event_message.kwargs['metrics_enabled'] == False
    assert event_message.kwargs['ping_enabled'] == True
    assert event_message.kwargs['ping_interval'] == 0.1


@pytest.mark.run_loop
async def test_after_server_stopped(dummy_bus: BusNode, loop, mocker, event_consumer):
    registry.add(TestApi())
    await dummy_bus.example.test.my_event.listen_async(lambda: None)

    await StatePlugin().after_server_stopped(bus_client=dummy_bus.bus_client, loop=loop)

    event_messages = event_consumer()
    assert len(event_messages) == 1
    event_message = event_messages[0]

    assert event_message.api_name == 'internal.state'
    assert event_message.event_name == 'server_stopped'
    assert event_message.kwargs['process_name'] == 'foo'
{'fraction_non_alphanumeric': 0.1030071440438611, 'fraction_numerical': 0.004651935537464695, 'mean_word_length': 4.615671641791045, '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': '2125402', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1844, 'n_words': 374}