-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make connection pool for each threads in client
- Loading branch information
Showing
4 changed files
with
188 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
FROM python:3.9-slim | ||
|
||
COPY . . | ||
RUN pip install -r requirements.txt | ||
COPY requirements.txt requirements.txt | ||
RUN pip install -r requirements.txt | ||
|
||
COPY . . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
import zmq | ||
|
||
from zero.zero_mq.queue_device.worker import ZeroMQWorker | ||
|
||
|
||
class TestWorker(unittest.TestCase): | ||
def test_create_zeromq_worker(self): | ||
worker = ZeroMQWorker(1) | ||
self.assertIsInstance(worker, ZeroMQWorker) | ||
self.assertEqual(worker.worker_id, 1) | ||
# self.assertEqual(worker.context, zmq.Context.instance()) | ||
# self.assertEqual(worker.socket, worker.context.socket(zmq.DEALER)) | ||
self.assertIsNotNone(worker.context) | ||
self.assertIsNotNone(worker.socket) | ||
self.assertEqual(worker.socket.getsockopt(zmq.LINGER), 0) | ||
self.assertEqual(worker.socket.getsockopt(zmq.SNDTIMEO), 2000) | ||
self.assertEqual(worker.socket.getsockopt(zmq.RCVTIMEO), -1) | ||
worker.close() | ||
self.assertEqual(worker.socket.closed, True) | ||
self.assertEqual(worker.context.closed, True) | ||
|
||
@pytest.mark.skip(reason="hard to test infinite loop") | ||
def test_listen_timeout(self): | ||
worker = ZeroMQWorker(1) | ||
mock_msg_handler = Mock(return_value=b"response") | ||
with ( | ||
patch.object(worker.socket, "connect", return_value=None), | ||
patch.object(worker, "_recv_and_process", side_effect=zmq.error.Again), | ||
): | ||
worker.listen("tcp://example.com:5555", mock_msg_handler) | ||
|
||
def test_recv_and_process(self): | ||
worker = ZeroMQWorker(1) | ||
mock_msg_handler = Mock(return_value=b"response") | ||
with ( | ||
patch.object( | ||
worker.socket, "recv_multipart", return_value=[b"ident", b"request"] | ||
), | ||
patch.object(worker.socket, "send_multipart", return_value=None), | ||
): | ||
worker._recv_and_process(mock_msg_handler) | ||
mock_msg_handler.assert_called_once() | ||
worker.socket.send_multipart.assert_called_once() | ||
worker.socket.recv_multipart.assert_called_once() | ||
|
||
def test_recv_and_process_invalid_message(self): | ||
worker = ZeroMQWorker(1) | ||
mock_msg_handler = Mock(return_value=b"response") | ||
with ( | ||
patch.object(worker.socket, "recv_multipart", return_value=[b"ident"]), | ||
patch.object(worker.socket, "send_multipart", return_value=None), | ||
): | ||
worker._recv_and_process(mock_msg_handler) | ||
mock_msg_handler.assert_not_called() | ||
worker.socket.send_multipart.assert_not_called() | ||
worker.socket.recv_multipart.assert_called_once() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.