diff --git a/mopidy/utils/network.py b/mopidy/utils/network.py index 5267afd5..84fe49bd 100644 --- a/mopidy/utils/network.py +++ b/mopidy/utils/network.py @@ -164,9 +164,10 @@ class Connection(object): def queue_send(self, data): """Send data to client exactly as is.""" self.send_lock.acquire(True) - self.send_buffer += data + self.send_buffer = self.send(self.send_buffer + data) self.send_lock.release() - self.enable_send() + if self.send_buffer: + self.enable_send() def enable_timeout(self): """Reactivate timeout mechanism.""" diff --git a/tests/utils/network/connection_test.py b/tests/utils/network/connection_test.py index e96d1852..1bda0af5 100644 --- a/tests/utils/network/connection_test.py +++ b/tests/utils/network/connection_test.py @@ -322,25 +322,35 @@ class ConnectionTest(unittest.TestCase): self.mock.send_lock.acquire.assert_called_once_with(True) self.mock.send_lock.release.assert_called_once_with() - def test_queue_send_appends_to_send_buffer(self): - self.mock.send_lock = Mock() + def test_queue_send_calls_send(self): self.mock.send_buffer = '' - - network.Connection.queue_send(self.mock, 'abc') - self.assertEqual('abc', self.mock.send_buffer) - - network.Connection.queue_send(self.mock, 'def') - self.assertEqual('abcdef', self.mock.send_buffer) - - network.Connection.queue_send(self.mock, '') - self.assertEqual('abcdef', self.mock.send_buffer) - - def test_queue_send_calls_enable_send(self): self.mock.send_lock = Mock() - self.mock.send_buffer = '' + self.mock.send.return_value = '' network.Connection.queue_send(self.mock, 'data') + self.mock.send.assert_called_once_with('data') + self.assertEqual(0, self.mock.enable_send.call_count) + self.assertEqual('', self.mock.send_buffer) + + def test_queue_send_calls_enable_send_for_partial_send(self): + self.mock.send_buffer = '' + self.mock.send_lock = Mock() + self.mock.send.return_value = 'ta' + + network.Connection.queue_send(self.mock, 'data') + self.mock.send.assert_called_once_with('data') self.mock.enable_send.assert_called_once_with() + self.assertEqual('ta', self.mock.send_buffer) + + def test_queue_send_calls_send_with_existing_buffer(self): + self.mock.send_buffer = 'foo' + self.mock.send_lock = Mock() + self.mock.send.return_value = '' + + network.Connection.queue_send(self.mock, 'bar') + self.mock.send.assert_called_once_with('foobar') + self.assertEqual(0, self.mock.enable_send.call_count) + self.assertEqual('', self.mock.send_buffer) def test_recv_callback_respects_io_err(self): self.mock.sock = Mock(spec=socket.SocketType)