Esempio n. 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))
Esempio n. 2
0
 def test_properties(self):
     frame = HeaderFrame(42, 'class_id', 'weight', 'size', 'props')
     assert_equals(42, frame.channel_id)
     assert_equals('class_id', frame.class_id)
     assert_equals('weight', frame.weight)
     assert_equals('size', frame.size)
     assert_equals('props', frame.properties)
Esempio n. 3
0
    def test_dispatch_method_frame_raises_invalidframetype(self):
        frame = mock()

        expect(frame.type).returns(HeaderFrame.type())

        with assert_raises(Frame.InvalidFrameType):
            self.ch.dispatch(frame)
Esempio n. 4
0
    def test_write_frame_slow_for_standard_properties(self):
        HeaderFrame.DEFAULT_PROPERTIES = False
        bit_field = 0
        properties = {}
        now = datetime.utcfromtimestamp(
            long(time.mktime(datetime.now().timetuple())))
        for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES:
            bit_field |= mask

            if ptype == 'shortstr':
                properties[pname] = pname
            elif ptype == 'octet':
                properties[pname] = 42
            elif ptype == 'timestamp':
                properties[pname] = now
            elif ptype == 'table':
                properties[pname] = {'foo': 'bar'}

        frame = HeaderFrame(42, 5, 6, 7, properties)
        buf = bytearray()
        frame.write_frame(buf)
        HeaderFrame.DEFAULT_PROPERTIES = True

        reader = Reader(buf)
        assert_equals(2, reader.read_octet())
        assert_equals(42, reader.read_short())
        size = reader.read_long()
        start_pos = reader.tell()
        assert_equals(5, reader.read_short())
        assert_equals(6, reader.read_short())
        assert_equals(7, reader.read_longlong())
        assert_equals(0b1111111111111100, reader.read_short())

        for pname, ptype, rfunc, wfunc, mask in HeaderFrame.PROPERTIES:
            if ptype == 'shortstr':
                assertEquals(pname, reader.read_shortstr())
            elif ptype == 'octet':
                assertEquals(42, reader.read_octet())
            elif ptype == 'timestamp':
                assertEquals(now, reader.read_timestamp())
            elif ptype == 'table':
                assertEquals({'foo': 'bar'}, reader.read_table())

        end_pos = reader.tell()
        assert_equals(size, end_pos - start_pos)
        assert_equals(0xce, reader.read_octet())
Esempio n. 5
0
    def test_write_frame_slow_for_standard_properties(self):
        HeaderFrame.DEFAULT_PROPERTIES = False
        bit_field = 0
        properties = {}
        now = datetime.utcfromtimestamp(
            long(time.mktime(datetime.now().timetuple())))
        for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES:
            bit_field |= mask

            if ptype == 'shortstr':
                properties[pname] = pname
            elif ptype == 'octet':
                properties[pname] = 42
            elif ptype == 'timestamp':
                properties[pname] = now
            elif ptype == 'table':
                properties[pname] = {'foo': 'bar'}

        frame = HeaderFrame(42, 5, 6, 7, properties)
        buf = bytearray()
        frame.write_frame(buf)
        HeaderFrame.DEFAULT_PROPERTIES = True

        reader = Reader(buf)
        assert_equals(2, reader.read_octet())
        assert_equals(42, reader.read_short())
        size = reader.read_long()
        start_pos = reader.tell()
        assert_equals(5, reader.read_short())
        assert_equals(6, reader.read_short())
        assert_equals(7, reader.read_longlong())
        assert_equals(0b1111111111111100, reader.read_short())

        for pname, ptype, rfunc, wfunc, mask in HeaderFrame.PROPERTIES:
            if ptype == 'shortstr':
                assertEquals(pname, reader.read_shortstr())
            elif ptype == 'octet':
                assertEquals(42, reader.read_octet())
            elif ptype == 'timestamp':
                assertEquals(now, reader.read_timestamp())
            elif ptype == 'table':
                assertEquals({'foo': 'bar'}, reader.read_table())

        end_pos = reader.tell()
        assert_equals(size, end_pos - start_pos)
        assert_equals(0xce, reader.read_octet())
Esempio n. 6
0
    def test_parse_slow_for_standard_properties(self):
        HeaderFrame.DEFAULT_PROPERTIES = False
        bit_writer = Writer()
        val_writer = Writer()

        # strip ms because amqp doesn't include it
        now = datetime.utcfromtimestamp(
            long(time.mktime(datetime.now().timetuple())))

        bit_field = 0
        for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES:
            bit_field = (bit_field << 1) | 1

            if ptype == 'shortstr':
                val_writer.write_shortstr(pname)
            elif ptype == 'octet':
                val_writer.write_octet(42)
            elif ptype == 'timestamp':
                val_writer.write_timestamp(now)
            elif ptype == 'table':
                val_writer.write_table({'foo': 'bar'})

        bit_field <<= (16 - len(HeaderFrame.PROPERTIES))
        bit_writer.write_short(bit_field)

        header_writer = Writer()
        header_writer.write_short(5)
        header_writer.write_short(6)
        header_writer.write_longlong(7)
        payload = header_writer.buffer()
        payload += bit_writer.buffer()
        payload += val_writer.buffer()

        reader = Reader(payload)
        frame = HeaderFrame.parse(4, reader)
        HeaderFrame.DEFAULT_PROPERTIES = True

        for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES:
            if ptype == 'shortstr':
                self.assertEquals(pname, frame.properties[pname])
            elif ptype == 'octet':
                self.assertEquals(42, frame.properties[pname])
            elif ptype == 'timestamp':
                self.assertEquals(now, frame.properties[pname])
            elif ptype == 'table':
                self.assertEquals({'foo': 'bar'}, frame.properties[pname])

        assert_equals(4, frame.channel_id)
        assert_equals(5, frame._class_id)
        assert_equals(6, frame._weight)
        assert_equals(7, frame._size)
Esempio n. 7
0
    def test_parse_slow_for_standard_properties(self):
        HeaderFrame.DEFAULT_PROPERTIES = False
        bit_writer = Writer()
        val_writer = Writer()

        # strip ms because amqp doesn't include it
        now = datetime.utcfromtimestamp(
            long(time.mktime(datetime.now().timetuple())))

        bit_field = 0
        for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES:
            bit_field = (bit_field << 1) | 1

            if ptype == 'shortstr':
                val_writer.write_shortstr(pname)
            elif ptype == 'octet':
                val_writer.write_octet(42)
            elif ptype == 'timestamp':
                val_writer.write_timestamp(now)
            elif ptype == 'table':
                val_writer.write_table({'foo': 'bar'})

        bit_field <<= (16 - len(HeaderFrame.PROPERTIES))
        bit_writer.write_short(bit_field)

        header_writer = Writer()
        header_writer.write_short(5)
        header_writer.write_short(6)
        header_writer.write_longlong(7)
        payload = header_writer.buffer()
        payload += bit_writer.buffer()
        payload += val_writer.buffer()

        reader = Reader(payload)
        frame = HeaderFrame.parse(4, reader)
        HeaderFrame.DEFAULT_PROPERTIES = True

        for pname, ptype, reader, writer, mask in HeaderFrame.PROPERTIES:
            if ptype == 'shortstr':
                self.assertEquals(pname, frame.properties[pname])
            elif ptype == 'octet':
                self.assertEquals(42, frame.properties[pname])
            elif ptype == 'timestamp':
                self.assertEquals(now, frame.properties[pname])
            elif ptype == 'table':
                self.assertEquals({'foo': 'bar'}, frame.properties[pname])

        assert_equals(4, frame.channel_id)
        assert_equals(5, frame._class_id)
        assert_equals(6, frame._weight)
        assert_equals(7, frame._size)
Esempio n. 8
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. 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_parse_for_standard_properties(self):
    bit_writer = Writer()
    val_writer = Writer()
    stream = StringIO()

    # strip ms because amqp doesn't include it
    now = datetime.fromtimestamp( long(time.mktime(datetime.now().timetuple())) )

    bit_field = 0
    for pname, ptype, reader, writer in HeaderFrame.PROPERTIES:
      bit_field = (bit_field << 1) | 1

      if ptype=='shortstr':
        val_writer.write_shortstr( pname )
      elif ptype=='octet':
        val_writer.write_octet( 42 )
      elif ptype=='timestamp':
        val_writer.write_timestamp( now )
      elif ptype=='table':
        val_writer.write_table( {'foo':'bar'} )

    bit_field <<= (16- len(HeaderFrame.PROPERTIES))
    bit_writer.write_short( bit_field )
    
    payload = struct.pack( '>HHQ', 5, 6, 7 )
    payload += bit_writer.buffer()
    payload += val_writer.buffer()

    reader = Reader(payload)
    frame = HeaderFrame.parse(4, reader)

    for pname, ptype, reader, writer in HeaderFrame.PROPERTIES:
      if ptype=='shortstr':
        self.assertEquals( pname, frame.properties[pname] )
      elif ptype=='octet':
        self.assertEquals( 42, frame.properties[pname] )
      elif ptype=='timestamp':
        self.assertEquals( now, frame.properties[pname] )
      elif ptype=='table':
        self.assertEquals( {'foo':'bar'}, frame.properties[pname] )
Esempio n. 11
0
 def test_type(self):
   assert_equals( 2, HeaderFrame.type() )
Esempio n. 12
0
 def test_str(self):
     # Don't bother checking the copy
     frame = HeaderFrame(42, 5, 6, 7, 'props')
     assert_equals(
         'HeaderFrame[channel: 42, class_id: 5, weight: 6, size: 7, properties: props]',
         str(frame))
Esempio n. 13
0
 def test_type(self):
     assert_equals(2, HeaderFrame.type())