Skip to content

Commit

Permalink
When a client has no connections, its read should not busy-wait
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Lecocq committed Jul 25, 2014
1 parent 98f41cf commit 6971f65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nsq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import random
from select import select
import socket
import time
import threading


Expand Down Expand Up @@ -173,6 +174,9 @@ def read(self):
connections = [c for c in self.connections() if c.alive()]

if not connections:
# If there are no connections, obviously we return no messages, but
# we should wait the duration of the timeout
time.sleep(self._timeout)
return []

# Not all connections need to be written to, so we'll only concern
Expand Down
8 changes: 8 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ def test_read_with_no_connections(self):
with mock.patch.object(self.client, 'connections', return_value=[]):
self.assertEqual(self.client.read(), [])

def test_read_sleep_no_connections(self):
'''Sleeps for timeout if no connections'''
with mock.patch.object(self.client, '_timeout', 5):
with mock.patch.object(self.client, 'connections', return_value=[]):
with mock.patch('nsq.client.time.sleep') as mock_sleep:
self.client.read()
mock_sleep.assert_called_with(self.client._timeout)

def test_random_connection(self):
'''Yields a random client'''
found = []
Expand Down

0 comments on commit 6971f65

Please sign in to comment.