# Path: aiosmtplib/sync.py
# def async_to_sync(
#     coro: Awaitable, loop: Optional[asyncio.AbstractEventLoop] = None
# ) -> Any:
#     if loop is None:
#         loop = asyncio.get_event_loop()
# 
#     if loop.is_running():
#         raise RuntimeError("Event loop is already running.")
# 
#     result = loop.create_future()
# 
#     try:
#         loop.run_until_complete(_await_with_future(coro, result))
#     finally:
#         shutdown_loop(loop)
# 
#     return result.result()


import pytest
from aiosmtplib.sync import async_to_sync
def test_send_message_sync_when_connected(event_loop, smtp_client_threaded, message):
    event_loop.run_until_complete(smtp_client_threaded.connect())

    errors, response = smtp_client_threaded.send_message_sync(message)

    assert not errors
    assert isinstance(errors, dict)
    assert response != ""


def test_async_to_sync_without_loop(event_loop):
    async def test_func():
        return 7

    result = async_to_sync(test_func())

    assert result == 7


def test_async_to_sync_with_exception(event_loop):
    async def test_func():
        raise ZeroDivisionError

    with pytest.raises(ZeroDivisionError):
        async_to_sync(test_func(), loop=event_loop)


@pytest.mark.asyncio
async def test_async_to_sync_with_running_loop(event_loop):
    with pytest.raises(RuntimeError):

# Path: aiosmtplib/sync.py
# def async_to_sync(
#     coro: Awaitable, loop: Optional[asyncio.AbstractEventLoop] = None
# ) -> Any:
#     if loop is None:
#         loop = asyncio.get_event_loop()
# 
#     if loop.is_running():
#         raise RuntimeError("Event loop is already running.")
# 
#     result = loop.create_future()
# 
#     try:
#         loop.run_until_complete(_await_with_future(coro, result))
#     finally:
#         shutdown_loop(loop)
# 
#     return result.result()


# Path: tests/test_sync.py
import pytest
from aiosmtplib.sync import async_to_sync

def test_send_message_sync_when_connected(event_loop, smtp_client_threaded, message):
    event_loop.run_until_complete(smtp_client_threaded.connect())

    errors, response = smtp_client_threaded.send_message_sync(message)

    assert not errors
    assert isinstance(errors, dict)
    assert response != ""


def test_async_to_sync_without_loop(event_loop):
    async def test_func():
        return 7

    result = async_to_sync(test_func())

    assert result == 7


def test_async_to_sync_with_exception(event_loop):
    async def test_func():
        raise ZeroDivisionError

    with pytest.raises(ZeroDivisionError):
        async_to_sync(test_func(), loop=event_loop)


@pytest.mark.asyncio
async def test_async_to_sync_with_running_loop(event_loop):
    with pytest.raises(RuntimeError):

        async_to_sync(None)