Example #1
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 #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_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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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])