Example #1
0
    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()
Example #2
0
    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))
Example #3
0
 def test_next_frame_with_a_frame(self):
     c = Channel(None, None, {})
     ch_id, c_id, m_id = 0, 1, 2
     f0 = MethodFrame(ch_id, c_id, m_id)
     f1 = MethodFrame(ch_id, c_id, m_id)
     c._frame_buffer = deque([f0, f1])
     assertEquals(c.next_frame(), f0)
Example #4
0
    def test_closed_cb_without_final_frame(self):
        c = Channel(
            'connection', None, {
                20: ChannelClass,
                40: ExchangeClass,
                50: QueueClass,
                60: BasicClass,
                90: TransactionClass,
            })
        c._pending_events = 'foo'
        c._frame_buffer = 'foo'

        for val in c._class_map.values():
            expect(val._cleanup)
        expect(c._notify_close_listeners)

        c._closed_cb()
        assert_equals(deque([]), c._pending_events)
        assert_equals(deque([]), c._frame_buffer)
        assert_equals(None, c._connection)
        assert_false(hasattr(c, 'channel'))
        assert_false(hasattr(c, 'exchange'))
        assert_false(hasattr(c, 'queue'))
        assert_false(hasattr(c, 'basic'))
        assert_false(hasattr(c, 'tx'))
        assert_equals(None, c._class_map)
        assert_equals(set(), c._close_listeners)
Example #5
0
 def test_next_frame_with_a_frame(self):
     c = Channel(None, None, {})
     ch_id, c_id, m_id = 0, 1, 2
     f0 = MethodFrame(ch_id, c_id, m_id)
     f1 = MethodFrame(ch_id, c_id, m_id)
     c._frame_buffer = deque([f0, f1])
     assert_equals(c.next_frame(), f0)
Example #6
0
    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()
Example #7
0
    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))
Example #8
0
    def test_closed_cb_without_final_frame(self):
        c = Channel('connection', None, {
            20: ChannelClass,
            40: ExchangeClass,
            50: QueueClass,
            60: BasicClass,
            90: TransactionClass,
        })
        c._pending_events = 'foo'
        c._frame_buffer = 'foo'

        for val in c._class_map.values():
            expect(val._cleanup)
        expect(c._notify_close_listeners)

        c._closed_cb()
        assert_equals(deque([]), c._pending_events)
        assert_equals(deque([]), c._frame_buffer)
        assert_equals(None, c._connection)
        assert_false(hasattr(c, 'channel'))
        assert_false(hasattr(c, 'exchange'))
        assert_false(hasattr(c, 'queue'))
        assert_false(hasattr(c, 'basic'))
        assert_false(hasattr(c, 'tx'))
        assert_equals(None, c._class_map)
        assert_equals(set(), c._close_listeners)
Example #9
0
    def test_requeue_frames(self):
        c = Channel(None, None, {})
        ch_id, c_id, m_id = 0, 1, 2
        f = [MethodFrame(ch_id, c_id, m_id) for i in xrange(4)]
        c._frame_buffer = deque(f[:2])

        c.requeue_frames(f[2:])
        assertEquals(c._frame_buffer, deque([f[i] for i in [3, 2, 0, 1]]))
Example #10
0
    def test_requeue_frames(self):
        c = Channel(None, None, {})
        ch_id, c_id, m_id = 0, 1, 2
        f = [MethodFrame(ch_id, c_id, m_id) for i in xrange(4)]
        c._frame_buffer = deque(f[:2])

        c.requeue_frames(f[2:])
        assert_equals(c._frame_buffer, deque([f[i] for i in [3, 2, 0, 1]]))
Example #11
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()
Example #12
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()
Example #13
0
    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])
Example #14
0
    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])
Example #15
0
    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()
Example #16
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()
        assert_equals(deque(), c._frame_buffer)
Example #17
0
  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()
Example #18
0
    def test_process_frames_when_connectionclosed_on_dispatch(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(ConnectionClosed('something darkside'))
        stub(c.close)  # assert not called

        assert_raises(ConnectionClosed, c.process_frames)
Example #19
0
    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(RuntimeError("zomg it broked"))
        expect(c.close).args(500, 'Failed to dispatch %s' % (str(f0)))

        assert_raises(RuntimeError, c.process_frames)
        assertEquals(f1, c._frame_buffer[0])
Example #20
0
    def test_process_frames_when_connectionclosed_on_dispatch(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(
            ConnectionClosed('something darkside'))
        stub(c.close)  # assert not called

        assert_raises(ConnectionClosed, c.process_frames)
Example #21
0
    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(RuntimeError("zomg it broked"))
        expect(c.close).args(500, "Failed to dispatch %s" % (str(f0)))

        assert_raises(RuntimeError, c.process_frames)
        assertEquals(f1, c._frame_buffer[0])
Example #22
0
  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()
Example #23
0
    def test_process_frames_logs_and_closes_when_systemexit_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(SystemExit())
        stub(c.close)

        assert_raises(SystemExit, c.process_frames)
        assert_equals(f1, c._frame_buffer[0])
Example #24
0
    def test_process_frames_logs_and_closes_when_dispatch_error_raised_even_when_exception_on_close(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(RuntimeError("zomg it broked"))
        expect(c.close).raises(ValueError())

        assert_raises(RuntimeError, c.process_frames)
        assert_equals(f1, c._frame_buffer[0])
Example #25
0
    def test_process_frames_logs_and_closes_when_systemexit_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(SystemExit())
        stub(c.close)

        assert_raises(SystemExit, c.process_frames)
        assertEquals(f1, c._frame_buffer[0])
Example #26
0
    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()
Example #27
0
    def test_process_frames_raises_systemexit_when_close_raises_systemexit(self):
        c = Channel(mock(), 20, {})
        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(RuntimeError("zomg it broked"))
        expect(c.logger.exception).args(
            'Closing on failed dispatch of frame %.255s', f0)
        expect(c.close).raises(SystemExit())

        assert_raises(SystemExit, c.process_frames)
        assert_equals(f1, c._frame_buffer[0])
Example #28
0
    def test_process_frames_logs_and_closes_when_dispatch_error_raised(self):
        c = Channel(mock(), 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(RuntimeError("zomg it broked"))
        expect(c.logger.exception).args(
            'Closing on failed dispatch of frame %.255s', f0)
        expect(c.close).args(500, 'Failed to dispatch %s' % (str(f0)))

        assert_raises(RuntimeError, c.process_frames)
        assert_equals(f1, c._frame_buffer[0])
Example #29
0
    def test_process_frames_logs_and_preserves_original_exception_when_dispatch_and_close_fail(self):
        # fix it, too.
        c = Channel(mock(), 20, {})
        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(RuntimeError("zomg it broked"))
        expect(c.logger.exception).args(
            'Closing on failed dispatch of frame %.255s', f0)
        expect(c.close).raises(ValueError())
        expect(c.logger.exception).args('Channel close failed')

        assert_raises(RuntimeError, c.process_frames)
        assert_equals(f1, c._frame_buffer[0])
Example #30
0
    def test_closed_cb_without_final_frame(self):
        c = Channel(mock(), None, self._CLASS_MAP)
        c._pending_events = 'foo'
        c._frame_buffer = 'foo'

        for val in c._class_map.values():
            expect(val._cleanup)
        expect(c._notify_close_listeners)

        c._closed_cb()
        assert_equals(deque([]), c._pending_events)
        assert_equals(deque([]), c._frame_buffer)
        assert_equals(None, c._connection)
        assert_false(hasattr(c, 'channel'))
        assert_false(hasattr(c, 'exchange'))
        assert_false(hasattr(c, 'queue'))
        assert_false(hasattr(c, 'basic'))
        assert_false(hasattr(c, 'tx'))
        assert_equals(None, c._class_map)
        assert_equals(set(), c._close_listeners)
Example #31
0
    def test_closed_cb_without_final_frame(self):
        c = Channel(mock(), None, self._CLASS_MAP)
        c._pending_events = 'foo'
        c._frame_buffer = 'foo'

        for val in c._class_map.values():
            expect(val._cleanup)
        expect(c._notify_close_listeners)

        c._closed_cb()
        assert_equals(deque([]), c._pending_events)
        assert_equals(deque([]), c._frame_buffer)
        assert_equals(None, c._connection)
        assert_false(hasattr(c, 'channel'))
        assert_false(hasattr(c, 'exchange'))
        assert_false(hasattr(c, 'queue'))
        assert_false(hasattr(c, 'basic'))
        assert_false(hasattr(c, 'tx'))
        assert_equals(None, c._class_map)
        assert_equals(set(), c._close_listeners)
Example #32
0
    def test_closed_cb_without_final_frame(self):
        c = Channel(
            "connection",
            None,
            {20: ChannelClass, 40: ExchangeClass, 50: QueueClass, 60: BasicClass, 90: TransactionClass},
        )
        c._pending_events = "foo"
        c._frame_buffer = "foo"

        for val in c._class_map.values():
            expect(val._cleanup)
        expect(c._notify_close_listeners)

        c._closed_cb()
        assert_equals(deque([]), c._pending_events)
        assert_equals(deque([]), c._frame_buffer)
        assert_equals(None, c._connection)
        assert_false(hasattr(c, "channel"))
        assert_false(hasattr(c, "exchange"))
        assert_false(hasattr(c, "queue"))
        assert_false(hasattr(c, "basic"))
        assert_false(hasattr(c, "tx"))
        assert_equals(None, c._class_map)
        assert_equals(set(), c._close_listeners)
Example #33
0
 def test_next_frame_with_no_frames(self):
     c = Channel(None, None, {})
     c._frame_buffer = deque()
     assertEquals(c.next_frame(), None)
Example #34
0
 def test_next_frame_with_no_frames(self):
     c = Channel(None, None, {})
     c._frame_buffer = deque()
     assert_equals(c.next_frame(), None)