Esempio n. 1
0
    def test_str(self):
        # Test both branches but don't assert the actual content because its
        # not worth it
        frame = ContentFrame(42, 'payload')
        str(frame)

        frame = ContentFrame(42, 8675309)
        str(frame)
Esempio n. 2
0
    def test_write_frame(self):
        w = mock()
        expect(mock(content_frame, 'Writer')).args('buffer').returns(w)
        expect(w.write_octet).args(3).returns(w)
        expect(w.write_short).args(42).returns(w)
        expect(w.write_long).args(5).returns(w)
        expect(w.write).args('hello').returns(w)
        expect(w.write_octet).args(0xce)

        frame = ContentFrame(42, 'hello')
        frame.write_frame('buffer')
Esempio n. 3
0
    def test_write_frame(self):
        w = mock()
        expect(mock(content_frame, 'Writer')).args('buffer').returns(w)
        expect(w.write_octet).args(3).returns(w)
        expect(w.write_short).args(42).returns(w)
        expect(w.write_long).args(5).returns(w)
        expect(w.write).args('hello').returns(w)
        expect(w.write_octet).args(0xce)

        frame = ContentFrame(42, 'hello')
        frame.write_frame('buffer')
Esempio n. 4
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))
Esempio n. 5
0
    def test_create_frames(self):
        itr = ContentFrame.create_frames(42, 'helloworld', 13)

        frame = itr.next()
        assert_true(isinstance(frame, ContentFrame))
        assert_equals(42, frame.channel_id)
        assert_equals('hello', frame.payload)

        frame = itr.next()
        assert_true(isinstance(frame, ContentFrame))
        assert_equals(42, frame.channel_id)
        assert_equals('world', frame.payload)

        assert_raises(StopIteration, itr.next)
Esempio n. 6
0
    def test_create_frames(self):
        itr = ContentFrame.create_frames(42, 'helloworld', 13)

        frame = itr.next()
        assert_true(isinstance(frame, ContentFrame))
        assert_equals(42, frame.channel_id)
        assert_equals('hello', frame.payload)

        frame = itr.next()
        assert_true(isinstance(frame, ContentFrame))
        assert_equals(42, frame.channel_id)
        assert_equals('world', frame.payload)

        assert_raises(StopIteration, itr.next)
Esempio n. 7
0
    def test_send_frame_when_not_closed_and_flow_control(self):
        conn = mock()
        c = Channel(conn, 32, {})
        c._active = False

        method = MethodFrame(1, 2, 3)
        heartbeat = HeartbeatFrame()
        header = HeaderFrame(1, 2, 3, 4)
        content = ContentFrame(1, 'foo')

        expect(conn.send_frame).args(method)
        expect(conn.send_frame).args(heartbeat)

        c.send_frame(method)
        c.send_frame(heartbeat)
        assert_raises(Channel.Inactive, c.send_frame, header)
        assert_raises(Channel.Inactive, c.send_frame, content)
Esempio n. 8
0
    def publish(self, msg, exchange, routing_key, mandatory=False,
                immediate=False, ticket=None):
        '''
        publish a message.
        '''
        args = Writer()
        args.write_short(ticket or self.default_ticket).\
            write_shortstr(exchange).\
            write_shortstr(routing_key).\
            write_bits(mandatory, immediate)

        self.send_frame(MethodFrame(self.channel_id, 60, 40, args))
        self.send_frame(
            HeaderFrame(self.channel_id, 60, 0, len(msg), msg.properties))

        f_max = self.channel.connection.frame_max
        for f in ContentFrame.create_frames(self.channel_id, msg.body, f_max):
            self.send_frame(f)
Esempio n. 9
0
    def publish(self, msg, exchange, routing_key, mandatory=False,
                immediate=False, ticket=None):
        '''
        publish a message.
        '''
        args = Writer()
        args.write_short(ticket or self.default_ticket).\
            write_shortstr(exchange).\
            write_shortstr(routing_key).\
            write_bits(mandatory, immediate)

        self.send_frame(MethodFrame(self.channel_id, 60, 40, args))
        self.send_frame(
            HeaderFrame(self.channel_id, 60, 0, len(msg), msg.properties))

        f_max = self.channel.connection.frame_max
        for f in ContentFrame.create_frames(self.channel_id, msg.body, f_max):
            self.send_frame(f)
Esempio n. 10
0
 def test_init(self):
     expect(Frame.__init__).args(is_a(ContentFrame), 42)
     frame = ContentFrame(42, 'payload')
     assert_equals('payload', frame._payload)
Esempio n. 11
0
 def test_parse(self):
     frame = ContentFrame.parse(42, 'payload')
     assert_true(isinstance(frame, ContentFrame))
     assert_equals(42, frame.channel_id)
     assert_equals('payload', frame.payload)
Esempio n. 12
0
 def test_payload(self):
     klass = ContentFrame(42, 'payload')
     assert_equals('payload', klass.payload)
Esempio n. 13
0
 def test_type(self):
     assert_equals(3, ContentFrame.type())
Esempio n. 14
0
 def test_parse(self):
     frame = ContentFrame.parse(42, 'payload')
     assert_true(isinstance(frame, ContentFrame))
     assert_equals(42, frame.channel_id)
     assert_equals('payload', frame.payload)
Esempio n. 15
0
 def test_type(self):
     assert_equals(3, ContentFrame.type())