def test_table_multi(self): val = { 'foo': 7, 'bar': Decimal('123345.1234'), 'baz': 'this is some random string I typed', 'ubaz': u'And something in unicode', 'dday_aniv': datetime(1994, 6, 6), 'more': { 'abc': -123, 'def': 'hello world', 'now': datetime(2007, 11, 11, 21, 14, 31), 'qty': Decimal('-123.45'), 'blank': {}, 'extra': { 'deeper': 'more strings', 'nums': -12345678, }, } } w = AMQPWriter() w.write_table(val) s = w.getvalue() r = AMQPReader(s) self.assertEqual(r.read_table(), val)
def test_shortstr(self): w = AMQPWriter() w.write_shortstr('hello') s = w.getvalue() self.assertEqualBinary(s, '\x05hello') r = AMQPReader(s) self.assertEqual(r.read_shortstr(), 'hello')
def test_shortstr_unicode(self): w = AMQPWriter() w.write_shortstr(u'hello') s = w.getvalue() self.assertEqual(s, '\x05hello') r = AMQPReader(s) self.assertEqual(r.read_shortstr(), u'hello')
def test_longlong(self): for i in range(256): val = randint(0, (2**64) - 1) w = AMQPWriter() w.write_longlong(val) s = w.getvalue() r = AMQPReader(s) self.assertEqual(r.read_longlong(), val)
def test_octet(self): for val in range(256): w = AMQPWriter() w.write_octet(val) s = w.getvalue() self.assertEqual(s, chr(val)) r = AMQPReader(s) self.assertEqual(r.read_octet(), val)
def test_short(self): for i in range(256): val = randint(0, 65535) w = AMQPWriter() w.write_short(val) s = w.getvalue() r = AMQPReader(s) self.assertEqual(r.read_short(), val)
def start(self, version_major, version_minor, server_properties, mechanisms, locales): args = AMQPWriter() args.write_octet(version_major) args.write_octet(version_minor) args.write_table(server_properties) args.write_longstr(' '.join(mechanisms)) args.write_longstr(' '.join(locales)) self._send_channel_method_frame(0, (10, 10), args)
def test_empty_longstr(self): w = AMQPWriter() w.write_longstr('') s = w.getvalue() self.assertEqual(s, '\x00\x00\x00\x00') r = AMQPReader(s) self.assertEqual(r.read_longstr(), '')
def test_table_empty(self): val = {} w = AMQPWriter() w.write_table(val) s = w.getvalue() self.assertEqual(s, '\x00\x00\x00\x00') r = AMQPReader(s) self.assertEqual(r.read_table(), val)
def test_longstr_unicode(self): val = u'a' * 512 w = AMQPWriter() w.write_longstr(val) s = w.getvalue() self.assertEqual(s, '\x00\x00\x02\x00' + ('a' * 512)) r = AMQPReader(s) self.assertEqual(r.read_longstr(), val)
def test_table(self): val = {'foo': 7} w = AMQPWriter() w.write_table(val) s = w.getvalue() self.assertEqual(s, '\x00\x00\x00\x09\x03fooI\x00\x00\x00\x07') r = AMQPReader(s) self.assertEqual(r.read_table(), val)
def test_single_bit(self): for val, check in [(True, '\x01'), (False, '\x00')]: w = AMQPWriter() w.write_bit(val) s = w.getvalue() self.assertEqual(s, check) r = AMQPReader(s) self.assertEqual(r.read_bit(), val)
def __init__(self, sock): self.channels = {} super(amqp.Connection, self).__init__(self, 0) self.out = AMQPWriter(sock.makefile('w')) self.input = AMQPReader(sock.makefile('r')) self.method_reader = _MethodReader(self.input)
def test_table_invalid(self): """ Check that an un-serializable table entry raises a ValueError """ val = {'test': None} w = AMQPWriter() self.assertRaises(ValueError, w.write_table, val)
def test_multiple_bits(self): w = AMQPWriter() w.write_bit(True) w.write_bit(True) w.write_bit(False) w.write_bit(True) s = w.getvalue() self.assertEqual(s, '\x0b') r = AMQPReader(s) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), False) self.assertEqual(r.read_bit(), True)
def test_multiple_bits2(self): """ Check bits mixed with non-bits """ w = AMQPWriter() w.write_bit(True) w.write_bit(True) w.write_bit(False) w.write_octet(10) w.write_bit(True) s = w.getvalue() self.assertEqual(s, '\x03\x0a\x01') r = AMQPReader(s) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), False) self.assertEqual(r.read_octet(), 10) self.assertEqual(r.read_bit(), True)
def test_multiple_bits3(self): """ Check bit groups that span multiple bytes """ w = AMQPWriter() # Spit out 20 bits for i in range(10): w.write_bit(True) w.write_bit(False) s = w.getvalue() self.assertEquals(s, '\x55\x55\x05') r = AMQPReader(s) for i in range(10): self.assertEqual(r.read_bit(), True) self.assertEqual(r.read_bit(), False)
def redirect(self, host, known_hosts): args = AMQPWriter() args.write_shortstr(host) args.write_shortstr(known_hosts) self._send_channel_method_frame(0, (10, 50), args)
def test_octet_invalid2(self): w = AMQPWriter() self.assertRaises(ValueError, w.write_octet, 256)
def tune(self, channel_max, frame_max, heartbeat): args = AMQPWriter() args.write_short(channel_max) args.write_long(frame_max) args.write_short(heartbeat) self._send_channel_method_frame(0, (10, 30), args)
def test_short_invalid2(self): w = AMQPWriter() self.assertRaises(ValueError, w.write_short, 65536)
def test_longlong_invalid2(self): w = AMQPWriter() self.assertRaises(ValueError, w.write_longlong, 2**64)
def test_long_shortstr_unicode(self): w = AMQPWriter() self.assertRaises(ValueError, w.write_shortstr, u'\u0100' * 128)
def test_long_shortstr(self): w = AMQPWriter() self.assertRaises(ValueError, w.write_shortstr, 'x' * 256)
def test_empty_writer(self): w = AMQPWriter() self.assertEqual(w.getvalue(), '')