forked from dpkp/kafka-python
-
Notifications
You must be signed in to change notification settings - Fork 2
Fix thread not waking up when there is still data to be sent #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -7,7 +7,9 @@ | |
| import mock | ||
| import pytest | ||
|
|
||
| from kafka.conn import BrokerConnection, ConnectionStates, collect_hosts | ||
| from kafka.conn import BrokerConnection, ConnectionStates, SSLWantWriteError, collect_hosts | ||
| from kafka.metrics.metrics import Metrics | ||
| from kafka.metrics.stats.sensor import Sensor | ||
| from kafka.protocol.api import RequestHeader | ||
| from kafka.protocol.metadata import MetadataRequest | ||
| from kafka.protocol.produce import ProduceRequest | ||
|
|
@@ -31,8 +33,20 @@ def _socket(mocker): | |
|
|
||
|
|
||
| @pytest.fixture | ||
| def conn(_socket, dns_lookup): | ||
| conn = BrokerConnection('localhost', 9092, socket.AF_INET) | ||
| def metrics(mocker): | ||
| metrics = mocker.MagicMock(Metrics) | ||
| metrics.mocked_sensors = {} | ||
| def sensor(name, **kwargs): | ||
| if name not in metrics.mocked_sensors: | ||
| metrics.mocked_sensors[name] = mocker.MagicMock(Sensor) | ||
| return metrics.mocked_sensors[name] | ||
| metrics.sensor.side_effect = sensor | ||
| return metrics | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def conn(_socket, dns_lookup, metrics): | ||
| conn = BrokerConnection('localhost', 9092, socket.AF_INET, metrics=metrics) | ||
| return conn | ||
|
|
||
|
|
||
|
|
@@ -161,6 +175,46 @@ def test_send_response(_socket, conn): | |
| assert len(conn.in_flight_requests) == 1 | ||
|
|
||
|
|
||
| def test_send_async_request_while_other_request_is_already_in_buffer(_socket, conn, metrics): | ||
| conn.connect() | ||
| assert conn.state is ConnectionStates.CONNECTED | ||
| assert 'node-0.bytes-sent' in metrics.mocked_sensors | ||
| bytes_sent_sensor = metrics.mocked_sensors['node-0.bytes-sent'] | ||
|
|
||
| req1 = MetadataRequest[0](topics='foo') | ||
| header1 = RequestHeader(req1, client_id=conn.config['client_id']) | ||
| payload_bytes1 = len(header1.encode()) + len(req1.encode()) | ||
| req2 = MetadataRequest[0]([]) | ||
| header2 = RequestHeader(req2, client_id=conn.config['client_id']) | ||
| payload_bytes2 = len(header2.encode()) + len(req2.encode()) | ||
|
|
||
| # The first call to the socket will raise a transient SSL exception. This will make the first | ||
| # request to be kept in the internal buffer to be sent in the next call of | ||
| # send_pending_requests_v2. | ||
| _socket.send.side_effect = [SSLWantWriteError, 4 + payload_bytes1, 4 + payload_bytes2] | ||
|
|
||
| conn.send(req1, blocking=False) | ||
| # This won't send any bytes because of the SSL exception and the request bytes will be kept in | ||
| # the buffer. | ||
| assert conn.send_pending_requests_v2() is False | ||
| assert bytes_sent_sensor.record.call_args_list[0].args == (0,) | ||
|
|
||
| conn.send(req2, blocking=False) | ||
| # This will send the remaining bytes in the buffer from the first request, but should notice | ||
| # that the second request was queued, therefore it should return False. | ||
| bytes_sent_sensor.record.reset_mock() | ||
| assert conn.send_pending_requests_v2() is False | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where the test would fail before the fix. |
||
| bytes_sent_sensor.record.assert_called_once_with(4 + payload_bytes1) | ||
|
|
||
| bytes_sent_sensor.record.reset_mock() | ||
| assert conn.send_pending_requests_v2() is True | ||
| bytes_sent_sensor.record.assert_called_once_with(4 + payload_bytes2) | ||
|
|
||
| bytes_sent_sensor.record.reset_mock() | ||
| assert conn.send_pending_requests_v2() is True | ||
| bytes_sent_sensor.record.assert_called_once_with(0) | ||
|
|
||
|
|
||
| def test_send_error(_socket, conn): | ||
| conn.connect() | ||
| assert conn.state is ConnectionStates.CONNECTED | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.