46 lines
1.1 KiB
Python
46 lines
1.1 KiB
Python
# pylint: disable=redefined-outer-name
|
|
from tempfile import TemporaryDirectory
|
|
from os.path import join
|
|
from queue import Queue
|
|
from json import dumps, loads
|
|
|
|
import pytest
|
|
|
|
from .pipe_io_handler import PipeIOHandler
|
|
|
|
|
|
@pytest.fixture
|
|
def pipe_io():
|
|
with TemporaryDirectory() as tmpdir:
|
|
pipeio = PipeIOHandler(
|
|
join(tmpdir, 'in'),
|
|
join(tmpdir, 'out'),
|
|
permissions=0o600
|
|
)
|
|
pipeio.connector = MockConnector()
|
|
yield pipeio
|
|
pipeio.cleanup()
|
|
|
|
|
|
class MockConnector:
|
|
def __init__(self):
|
|
self.messages = Queue()
|
|
|
|
def send_message(self, msg):
|
|
self.messages.put(msg)
|
|
|
|
|
|
def test_pipe_io_handler_recv(pipe_io):
|
|
test_msg = {'key': 'cica'}
|
|
with open(pipe_io.in_pipe, 'w') as ofile:
|
|
ofile.write(dumps(test_msg) + '\n')
|
|
|
|
assert pipe_io.connector.messages.get() == test_msg
|
|
|
|
|
|
def test_pipe_io_handler_send(pipe_io):
|
|
test_msg = {'key': 'cica'}
|
|
pipe_io.handle_event(test_msg, None)
|
|
with open(pipe_io.out_pipe, 'r') as ifile:
|
|
assert loads(ifile.readline()) == test_msg
|