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_process_frames_drops_non_close_methods_when_emergency_closing(self): c = Channel(mock(), None, self._CLASS_MAP) c._emergency_close_pending = True c._connection = mock() c._connection.logger = mock() f0 = MethodFrame(1, 30, 40) f1 = HeaderFrame(1, 30, 0, 0) f2 = ContentFrame(1, "payload") f3_basic_close = MethodFrame(1, 20, 40) f4_basic_close_ok = MethodFrame(1, 20, 41) f5 = MethodFrame(1, 90, 11) c._frame_buffer = deque( [ f0, f1, f2, f3_basic_close, f4_basic_close_ok, f5 ]) expect(c.dispatch).args(f0).times(0) expect(c.dispatch).args(f1).times(0) expect(c.dispatch).args(f2).times(0) expect(c.dispatch).args(f3_basic_close).times(1) expect(c.dispatch).args(f4_basic_close_ok).times(1) expect(c.dispatch).args(f5).times(0) expect(c.logger.warn).times(4) c.process_frames() assert_equals(0, len(c._frame_buffer))
def test_process_frames_drops_non_close_methods_when_emergency_closing( self): c = Channel(mock(), None, self._CLASS_MAP) c._emergency_close_pending = True c._connection = mock() c._connection.logger = mock() f0 = MethodFrame(1, 30, 40) f1 = HeaderFrame(1, 30, 0, 0) f2 = ContentFrame(1, "payload") f3_basic_close = MethodFrame(1, 20, 40) f4_basic_close_ok = MethodFrame(1, 20, 41) f5 = MethodFrame(1, 90, 11) c._frame_buffer = deque( [f0, f1, f2, f3_basic_close, f4_basic_close_ok, f5]) expect(c.dispatch).args(f0).times(0) expect(c.dispatch).args(f1).times(0) expect(c.dispatch).args(f2).times(0) expect(c.dispatch).args(f3_basic_close).times(1) expect(c.dispatch).args(f4_basic_close_ok).times(1) expect(c.dispatch).args(f5).times(0) expect(c.logger.warn).times(4) c.process_frames() assert_equals(0, len(c._frame_buffer))
def test_process_frames_stops_when_frameunderflow_raised(self): c = Channel(None, None, {}) f0 = MethodFrame('ch_id', 'c_id', 'm_id') f1 = MethodFrame('ch_id', 'c_id', 'm_id') c._frame_buffer = deque([f0, f1]) expect(c.dispatch).args(f0).raises(ProtocolClass.FrameUnderflow) c.process_frames() assert_equals(f1, c._frame_buffer[0])
def test_process_frames_stops_when_buffer_is_empty(self): c = Channel(None, None) f0 = MethodFrame('ch_id', 'c_id', 'm_id') f1 = MethodFrame('ch_id', 'c_id', 'm_id') c._frame_buffer = deque([f0, f1]) expect(c.dispatch).args(f0) expect(c.dispatch).args(f1) c.process_frames()
def test_process_frames_stops_when_frameunderflow_raised(self): c = Channel(None, None, {}) f0 = MethodFrame('ch_id', 'c_id', 'm_id') f1 = MethodFrame('ch_id', 'c_id', 'm_id') c._frame_buffer = deque([f0, f1]) expect(c.dispatch).args(f0).raises(ProtocolClass.FrameUnderflow) c.process_frames() assertEquals(f1, c._frame_buffer[0])
def test_process_frames_stops_when_buffer_is_empty(self): c = Channel(None, None) f0 = MethodFrame('ch_id', 'c_id', 'm_id') f1 = MethodFrame('ch_id', 'c_id', 'm_id') c._frame_buffer = deque([ f0, f1 ]) expect( c.dispatch ).args( f0 ) expect( c.dispatch ).args( f1 ) c.process_frames()
def test_process_frames_stops_when_buffer_is_empty(self): c = Channel(None, None, {}) f0 = MethodFrame("ch_id", "c_id", "m_id") f1 = MethodFrame("ch_id", "c_id", "m_id") c._frame_buffer = deque([f0, f1]) expect(c.dispatch).args(f0) expect(c.dispatch).args(f1) c.process_frames() assert_equals(deque(), c._frame_buffer)
def test_process_frames_with_one_frame(self): conn = mock() conn.logger = mock() c = Channel(conn, None) ch_id, c_id, m_id = 0, 20, 2 f = MethodFrame(ch_id, c_id, m_id) c._frame_buffer = deque([ f ]) expect(c.logger.error).args(ignore(), ignore(), exc_info=ignore()) expect(c.close).args(ignore(), ignore()) c.process_frames()
def test_process_frames_with_one_frame(self): conn = mock() conn.logger = mock() c = Channel(conn, None) ch_id, c_id, m_id = 0, 20, 2 f = MethodFrame(ch_id, c_id, m_id) c._frame_buffer = deque([f]) expect(c.logger.error).args(ignore(), ignore(), exc_info=ignore()) expect(c.close).args(ignore(), ignore()) c.process_frames()
def test_process_frames_logs_and_closes_when_dispatch_error_raised(self): c = Channel(None, None) c._connection = mock() c._connection.logger = mock() f0 = MethodFrame(20, 30, 40) f1 = MethodFrame('ch_id', 'c_id', 'm_id') c._frame_buffer = deque([ f0, f1 ]) expect( c.dispatch ).args( f0 ).raises( Exception("zomg it broked") ) expect( c._connection.logger.error ).args( "Failed to dispatch %s", f0, exc_info=True ) expect( c.close ).args( 500, str ) c.process_frames()
def test_process_frames_logs_and_closes_when_dispatch_error_raised(self): c = Channel(None, None) c._connection = mock() c._connection.logger = mock() f0 = MethodFrame(20, 30, 40) f1 = MethodFrame('ch_id', 'c_id', 'm_id') c._frame_buffer = deque([f0, f1]) expect(c.dispatch).args(f0).raises(Exception("zomg it broked")) expect(c._connection.logger.error).args("Failed to dispatch %s", f0, exc_info=True) expect(c.close).args(500, str) c.process_frames()
def test_process_frames_when_no_frames(self): # Not that this should ever happen, but to be sure c = Channel(None, None, {}) stub(c.dispatch) c.process_frames()
def test_process_frames_when_no_frames(self): # Not that this should ever happen, but to be sure c = Channel(mock(), None, {}) expect(c.dispatch).times(0) c.process_frames()