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())
def test_write_frame(self): args = mock() expect(args.buffer).returns('hello') frame = MethodFrame(42, 5, 6, args) buf = bytearray() frame.write_frame(buf) reader = Reader(buf) assert_equals(1, 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()) args_pos = reader.tell() assert_equals('hello', reader.read(size - (args_pos - start_pos))) assert_equals(0xce, reader.read_octet())
def test_read_long(self): b = Reader('\xff\x00\xff\x00') assert_equals(4278255360, b.read_long()) assert_raises(Reader.BufferUnderflow, b.read_long)