Skip to content

Commit

Permalink
Merge pull request #33 from dlecocq/dan/no-connection-sleep-on-read
Browse files Browse the repository at this point in the history
When a client has no connections, its read should not busy-wait
  • Loading branch information
dlecocq committed Jul 25, 2014
2 parents 98f41cf + 6971f65 commit 855fbe1
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 855fbe1

Please sign in to comment.