Skip to content

Commit

Permalink
Use the first unused channel number addressing #404, #460
Browse files Browse the repository at this point in the history
  • Loading branch information
gmr committed Apr 12, 2014
1 parent 3ad0fbe commit bd388a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 2 additions & 3 deletions pika/connection.py
Expand Up @@ -1128,9 +1128,8 @@ def _next_channel_number(self):
limit = self.params.channel_max or channel.MAX_CHANNELS
if len(self._channels) == limit:
raise exceptions.NoFreeChannels()
if not self._channels:
return 1
return max(self._channels.keys()) + 1
return [x + 1 for x in sorted(self._channels.keys() or [0])
if x + 1 not in self._channels.keys()][0]

def _on_channel_closeok(self, method_frame):
"""Remove the channel from the dict of channels when Channel.CloseOk is
Expand Down
16 changes: 16 additions & 0 deletions tests/connection_tests.py
Expand Up @@ -3,6 +3,7 @@
"""
import mock
import random
try:
import unittest2 as unittest
except ImportError:
Expand Down Expand Up @@ -81,3 +82,18 @@ def test_on_disconnect(self):
method_frame = self.channel._on_close.call_args[0][0]
self.assertEqual(method_frame.method.reply_code, 0)
self.assertEqual(method_frame.method.reply_text, 'Undefined')

@mock.patch('pika.connection.Connection.connect')
def test_new_conn_should_use_first_channel(self, connect):
"""_next_channel_number in new conn should always be 1"""
conn = connection.Connection()
self.assertEqual(1, conn._next_channel_number())

def test_next_channel_number_returns_lowest_unused(self):
"""_next_channel_number must return lowest available channel number"""
self.connection._channels = {channel_num: 'channel'
for channel_num in xrange(1, 50)}
expectation = random.randint(5, 50)
del self.connection._channels[expectation]
self.assertEqual(self.connection._next_channel_number(),
expectation)

0 comments on commit bd388a3

Please sign in to comment.