def channel(self, channel_id=None): """ Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. If channel_id is not None but no channel exists for that id, will raise InvalidChannel. If there are already too many channels open, will raise TooManyChannels. """ if channel_id is None: # adjust for channel 0 if len(self._channels) - 1 >= self._channel_max: raise Connection.TooManyChannels( "%d channels already open, max %d", len(self._channels) - 1, self._channel_max) channel_id = self._next_channel_id() while channel_id in self._channels: channel_id = self._next_channel_id() elif channel_id in self._channels: return self._channels[channel_id] else: raise Connection.InvalidChannel("%s is not a valid channel id", channel_id) # Call open() here so that ConnectionChannel doesn't have it called. Could # also solve this other ways, but it's a HACK regardless. rval = Channel(self, channel_id, self._class_map) self._channels[channel_id] = rval rval.add_close_listener(self._channel_closed) rval.open() return rval
def channel(self, channel_id=None, synchronous=False): """ Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. If channel_id is not None but no channel exists for that id, will raise InvalidChannel. If there are already too many channels open, will raise TooManyChannels. If synchronous=True, then the channel will act synchronous in all cases where a protocol method supports `nowait=False`, or where there is an implied callback in the protocol. """ if channel_id is None: # adjust for channel 0 if len(self._channels) - 1 >= self._channel_max: raise Connection.TooManyChannels( "%d channels already open, max %d", len(self._channels) - 1, self._channel_max) channel_id = self._next_channel_id() while channel_id in self._channels: channel_id = self._next_channel_id() elif channel_id in self._channels: return self._channels[channel_id] else: raise Connection.InvalidChannel("%s is not a valid channel id", channel_id) # Call open() here so that ConnectionChannel doesn't have it called. Could # also solve this other ways, but it's a HACK regardless. ch = Channel(self, channel_id, self._class_map, synchronous=synchronous) self._channels[channel_id] = ch ch.add_close_listener(self._channel_closed) ch.open() return ch
def channel(self, channel_id=None): """ Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. If channel_id is not None but no channel exists for that id, will raise InvalidChannel. If there are already too many channels open, will raise TooManyChannels. """ if channel_id is None: # adjust for channel 0 if len(self._channels)-1 >= self._channel_max: raise Connection.TooManyChannels( "%d channels already open, max %d", len(self._channels)-1, self._channel_max ) channel_id = self._next_channel_id() while channel_id in self._channels: channel_id = self._next_channel_id() elif channel_id in self._channels: return self._channels[channel_id] else: raise Connection.InvalidChannel("%s is not a valid channel id", channel_id ) # Call open() here so that ConnectionChannel doesn't have it called. Could # also solve this other ways, but it's a HACK regardless. rval = Channel(self, channel_id, self._class_map) self._channels[ channel_id ] = rval rval.add_close_listener( self._channel_closed ) rval.open() return rval
def channel(self, channel_id=None, synchronous=False): """ Fetch a Channel object identified by the numeric channel_id, or create that object if it doesn't already exist. If channel_id is not None but no channel exists for that id, will raise InvalidChannel. If there are already too many channels open, will raise TooManyChannels. If synchronous=True, then the channel will act synchronous in all cases where a protocol method supports `nowait=False`, or where there is an implied callback in the protocol. """ if channel_id is None: # adjust for channel 0 if len(self._channels) - 1 >= self._channel_max: raise Connection.TooManyChannels( "%d channels already open, max %d", len(self._channels) - 1, self._channel_max) channel_id = self._next_channel_id() while channel_id in self._channels: channel_id = self._next_channel_id() elif channel_id in self._channels: return self._channels[channel_id] else: raise Connection.InvalidChannel( "%s is not a valid channel id", channel_id) # Call open() here so that ConnectionChannel doesn't have it called. # Could also solve this other ways, but it's a HACK regardless. rval = Channel( self, channel_id, self._class_map, synchronous=synchronous) self._channels[channel_id] = rval rval.add_close_listener(self._channel_closed) rval.open() return rval
def test_process_frames_passes_through_exception_from_close_listener(self): class MyError(Exception): pass connection = mock() ch = Channel(connection, channel_id=1, class_map={20: ChannelClass}) rframe = mock(channel_id=ch.channel_id, class_id=20, method_id=40) ch._frame_buffer = deque([rframe]) on_channel_closed = mock() ch.add_close_listener(on_channel_closed) expect(rframe.args.read_short).returns('rcode') expect(rframe.args.read_shortstr).returns('reason') expect(rframe.args.read_short).returns('cid') expect(rframe.args.read_short).returns('mid') expect(connection.send_frame).once() expect(on_channel_closed).args(ch).raises(MyError) expect(ch.logger.exception).args( 'Closing on failed dispatch of frame %.255s', rframe) with assert_raises(MyError): ch.process_frames()
def test_remove_close_listener(self): c = Channel(None, None, {}) c.add_close_listener('foo') c.remove_close_listener('foo') c.remove_close_listener('bar') assert_equals(set([]), c._close_listeners)
def test_add_close_listener(self): c = Channel(None, None, {}) c.add_close_listener('foo') assert_equals(set(['foo']), c._close_listeners)