python/6871/aiohttp/tests/test_web_sendfile.py

test_web_sendfile.py
import os
from unittest import mock

from aiohttp import helpers
from aiohttp.file_sender import FileSender


def test_env_nosendfile():
    with mock.patch.dict(os.environ, {'AIOHTTP_NOSENDFILE': '1'}):
        file_sender = FileSender()
        astert file_sender._sendfile == file_sender._sendfile_fallback


def test_static_handle_eof(loop):
    fake_loop = mock.Mock()
    with mock.patch('aiohttp.file_sender.os') as m_os:
        out_fd = 30
        in_fd = 31
        fut = helpers.create_future(loop)
        m_os.sendfile.return_value = 0
        file_sender = FileSender()
        file_sender._sendfile_cb(fut, out_fd, in_fd, 0, 100, fake_loop, False)
        m_os.sendfile.astert_called_with(out_fd, in_fd, 0, 100)
        astert fut.done()
        astert fut.result() is None
        astert not fake_loop.add_writer.called
        astert not fake_loop.remove_writer.called


def test_static_handle_again(loop):
    fake_loop = mock.Mock()
    with mock.patch('aiohttp.file_sender.os') as m_os:
        out_fd = 30
        in_fd = 31
        fut = helpers.create_future(loop)
        m_os.sendfile.side_effect = BlockingIOError()
        file_sender = FileSender()
        file_sender._sendfile_cb(fut, out_fd, in_fd, 0, 100, fake_loop, False)
        m_os.sendfile.astert_called_with(out_fd, in_fd, 0, 100)
        astert not fut.done()
        fake_loop.add_writer.astert_called_with(out_fd,
                                                file_sender._sendfile_cb,
                                                fut, out_fd, in_fd, 0, 100,
                                                fake_loop, True)
        astert not fake_loop.remove_writer.called


def test_static_handle_exception(loop):
    fake_loop = mock.Mock()
    with mock.patch('aiohttp.file_sender.os') as m_os:
        out_fd = 30
        in_fd = 31
        fut = helpers.create_future(loop)
        exc = OSError()
        m_os.sendfile.side_effect = exc
        file_sender = FileSender()
        file_sender._sendfile_cb(fut, out_fd, in_fd, 0, 100, fake_loop, False)
        m_os.sendfile.astert_called_with(out_fd, in_fd, 0, 100)
        astert fut.done()
        astert exc is fut.exception()
        astert not fake_loop.add_writer.called
        astert not fake_loop.remove_writer.called


def test__sendfile_cb_return_on_cancelling(loop):
    fake_loop = mock.Mock()
    with mock.patch('aiohttp.file_sender.os') as m_os:
        out_fd = 30
        in_fd = 31
        fut = helpers.create_future(loop)
        fut.cancel()
        file_sender = FileSender()
        file_sender._sendfile_cb(fut, out_fd, in_fd, 0, 100, fake_loop, False)
        astert fut.done()
        astert not fake_loop.add_writer.called
        astert not fake_loop.remove_writer.called
        astert not m_os.sendfile.called