Skip to content

Commit

Permalink
Check keyword arguments passed to Reader constructor. Throw an Assert…
Browse files Browse the repository at this point in the history
…ionError

if an invalid argument is passed.

Before this change, specifying an invalid keyword argument would cause the AsyncConn
to fail quietly in the Tornado default exception handler during run-time.

Remove trailing whitespace

Check keyword arguments in Writer constructor and add unit test
  • Loading branch information
Stan Hu committed Jul 12, 2014
1 parent ffeeac7 commit c2cd613
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
7 changes: 7 additions & 0 deletions nsq/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from client import Client
import nsq
import async
import inspect

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -185,6 +186,12 @@ def __init__(self, topic, channel, message_handler=None, name=None,
self.lookupd_connect_timeout = lookupd_connect_timeout
self.lookupd_request_timeout = lookupd_request_timeout
self.random_rdy_ts = time.time()

# Verify keyword arguments
valid_args = inspect.getargspec(async.AsyncConn.__init__).args
diff = set(kwargs) - set(valid_args)
assert len(diff) == 0, 'Invalid keyword argument(s): %s' % list(diff)

self.conn_kwargs = kwargs

self.backoff_timer = BackoffTimer(0, max_backoff_duration)
Expand Down
9 changes: 8 additions & 1 deletion nsq/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from client import Client
import nsq
import async
import inspect

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,6 +90,12 @@ def __init__(self, nsqd_tcp_addresses, reconnect_interval=15.0, name=None, **kwa
self.name = name or nsqd_tcp_addresses[0]
self.nsqd_tcp_addresses = nsqd_tcp_addresses
self.conns = {}

# Verify keyword arguments
valid_args = inspect.getargspec(async.AsyncConn.__init__).args
diff = set(kwargs) - set(valid_args)
assert len(diff) == 0, 'Invalid keyword argument(s): %s' % list(diff)

self.conn_kwargs = kwargs
assert isinstance(reconnect_interval, (int, float))
self.reconnect_interval = reconnect_interval
Expand Down Expand Up @@ -132,7 +139,7 @@ def _on_connection_error(self, conn, error, **kwargs):
while conn.callback_queue:
callback = conn.callback_queue.pop(0)
callback(conn, error)

def _on_connection_response(self, conn, data=None, **kwargs):
if conn.callback_queue:
callback = conn.callback_queue.pop(0)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ def tearDown(self):
os.kill(proc.pid, signal.SIGKILL)
proc.wait()

def test_bad_reader_arguments(self):
topic = 'test_reader_msgs_%s' % time.time()
bad_options = dict(self.identify_options)
bad_options.update(dict(foo=10))
handler = lambda x: None

self.assertRaises(
AssertionError,
nsq.Reader,
nsqd_tcp_addresses=['127.0.0.1:4150'], topic=topic,
channel='ch', io_loop=self.io_loop,
message_handler=handler, max_in_flight=100,
**bad_options)

def test_conn_identify(self):
c = nsq.async.AsyncConn('127.0.0.1', 4150, io_loop=self.io_loop)
c.on('identify_response', self.stop)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import nsq
import unittest


class WriterUnitTest(unittest.TestCase):

def setUp(self):
super(WriterUnitTest, self).setUp()

def test_constructor(self):
name = 'test'
reconnect_interval = 10.0
writer = nsq.Writer(nsqd_tcp_addresses=['127.0.0.1:4150'],
reconnect_interval=reconnect_interval,
name=name)
self.assertEqual(writer.name, name)
self.assertEqual(0, len(writer.conn_kwargs))
self.assertEqual(writer.reconnect_interval, reconnect_interval)

def test_bad_writer_arguments(self):
bad_options = dict(foo=10)

self.assertRaises(
AssertionError,
nsq.Writer,
nsqd_tcp_addresses=['127.0.0.1:4150'],
reconnect_interval=15.0,
name='test', **bad_options)

0 comments on commit c2cd613

Please sign in to comment.