def test_inbound_header__empty_body(self): m = Message() m.properties = {} buf = pack('>HxxQ', m.CLASS_ID, 0) buf += m._serialize_properties() assert m.inbound_header(buf, offset=0) == 12 assert m.ready
def ret_factory(method, channel=0, args=b'', arg_format=None): if len(args) > 0: args = dumps(arg_format, args) else: args = b'' frame = (b''.join([pack('>HH', *method), args])) return 1, channel, frame
def test_read_frame__windowstimeout(self, monkeypatch): """Make sure BlockingIOError on Windows properly saves off partial reads. See https://github.com/celery/py-amqp/issues/320 """ self.t._quick_recv = Mock() self.t._quick_recv.side_effect = [ pack('>BHI', 1, 1, 16), socket.error( 10035, "A non-blocking socket operation could " "not be completed immediately"), b'thequickbrownfox', b'\xce' ] monkeypatch.setattr(os, 'name', 'nt') monkeypatch.setattr(errno, 'EWOULDBLOCK', 10035) assert len(self.t._read_buffer) == 0 with pytest.raises(socket.timeout): self.t.read_frame() assert len(self.t._read_buffer) == 7 frame_type, channel, payload = self.t.read_frame() assert len(self.t._read_buffer) == 0 assert frame_type == 1 assert channel == 1 assert payload == b'thequickbrownfox'
def test_read_frame__long(self): self.t._read = Mock() self.t._read.side_effect = [pack('>BHI', 1, 1, SIGNED_INT_MAX + 16), b'read1', b'read2', b'\xce'] frame_type, channel, payload = self.t.read_frame() assert frame_type == 1 assert channel == 1 assert payload == b'read1read2'
def test_header_message_empty_body(self): assert not self.g((1, 1, pack('>HH', *spec.Basic.Deliver))) self.callback.assert_not_called() with pytest.raises(UnexpectedFrame): self.g((1, 1, pack('>HH', *spec.Basic.Deliver))) m = Message() m.properties = {} buf = pack('>HxxQ', m.CLASS_ID, 0) buf += m._serialize_properties() assert self.g((2, 1, buf)) self.callback.assert_called() msg = self.callback.call_args[0][3] self.callback.assert_called_with( 1, msg.frame_method, msg.frame_args, msg, )
def test_read_frame__simple(self): self.t._read = Mock() checksum = [b'\xce'] def on_read2(size, *args): return checksum[0] def on_read1(size, *args): ret = self.t._read.return_value self.t._read.return_value = b'thequickbrownfox' self.t._read.side_effect = on_read2 return ret self.t._read.return_value = pack('>BHI', 1, 1, 16) self.t._read.side_effect = on_read1 self.t.read_frame() self.t._read.return_value = pack('>BHI', 1, 1, 16) self.t._read.side_effect = on_read1 checksum[0] = b'\x13' with pytest.raises(UnexpectedFrame): self.t.read_frame()
def test_header_message_content(self): assert not self.g((1, 1, pack('>HH', *spec.Basic.Deliver))) self.callback.assert_not_called() m = Message() m.properties = {} buf = pack('>HxxQ', m.CLASS_ID, 16) buf += m._serialize_properties() assert not self.g((2, 1, buf)) self.callback.assert_not_called() assert not self.g((3, 1, b'thequick')) self.callback.assert_not_called() assert self.g((3, 1, b'brownfox')) self.callback.assert_called() msg = self.callback.call_args[0][3] self.callback.assert_called_with( 1, msg.frame_method, msg.frame_args, msg, ) assert msg.body == b'thequickbrownfox'
def test_inbound_header(self): m = Message() m.properties = { 'content_type': 'application/json', 'content_encoding': 'utf-8', } body = 'the quick brown fox' buf = b'\0' * 30 + pack('>HxxQ', m.CLASS_ID, len(body)) buf += m._serialize_properties() assert m.inbound_header(buf, offset=30) == 42 assert m.body_size == len(body) assert m.properties['content_type'] == 'application/json' assert not m.ready
def test_basic_publish_confirm_nack(self): # test checking whether library is handling correctly Nack confirms # sent from RabbitMQ. Library must raise MessageNacked when server # sent Nack message. # Nack frame construction args = dumps('Lb', (1, False)) frame = (b''.join([pack('>HH', *spec.Basic.Nack), args])) def wait(method, *args, **kwargs): # Simple mock simulating registering callbacks of real wait method for m in method: self.c._pending[m] = kwargs['callback'] self.c._basic_publish = Mock(name='_basic_publish') self.c.wait = Mock(name='wait', side_effect=wait) self.c.basic_publish_confirm(1, 2, arg=1) with pytest.raises(MessageNacked): # Inject Nack to message handler self.c.dispatch_method(spec.Basic.Nack, frame, None)
def test_basic_publish_confirm_nack(self): # test checking whether library is handling correctly Nack confirms # sent from RabbitMQ. Library must raise MessageNacked when server # sent Nack message. # Nack frame construction args = dumps('Lb', (1, False)) frame = (b''.join([pack('>HH', *spec.Basic.Nack), args])) def wait(method, *args, **kwargs): # Simple mock simulating registering callbacks of real wait method for m in method: self.c._pending[m] = kwargs['callback'] self.c._basic_publish = Mock(name='_basic_publish') self.c.wait = Mock(name='wait', side_effect=wait) self.c.basic_publish_confirm(1, 2, arg=1) with pytest.raises(MessageNacked): # Inject Nack to message handler self.c.dispatch_method( spec.Basic.Nack, frame, None )
def test_header(self): buf = pack('>HH', 60, 51) assert self.g((1, 1, buf)) self.callback.assert_called_with(1, (60, 51), buf, None) assert self.conn.bytes_recv
class test_serialization: @pytest.mark.parametrize('descr,frame,expected,cast', [ ('S', b's8thequick', 'thequick', None), ('x', b'x\x00\x00\x00\x09thequick\xffIGNORED', b'thequick\xff', None), ('b', b'b' + pack('>B', True), True, None), ('B', b'B' + pack('>b', 123), 123, None), ('U', b'U' + pack('>h', -321), -321, None), ('u', b'u' + pack('>H', 321), 321, None), ('i', b'i' + pack('>I', 1234), 1234, None), ('L', b'L' + pack('>q', -32451), -32451, None), ('l', b'l' + pack('>Q', 32451), 32451, None), ('f', b'f' + pack('>f', 33.3), 34.0, ceil), ]) def test_read_item(self, descr, frame, expected, cast): actual = _read_item(frame)[0] actual = cast(actual) if cast else actual assert actual == expected def test_read_item_V(self): assert _read_item(b'V')[0] is None def test_roundtrip(self): format = b'bobBlLbsbSTx' x = dumps(format, [ True, 32, False, 3415, 4513134, 13241923419, True, b'thequickbrownfox', False, 'jumpsoverthelazydog', datetime(2015, 3, 13, 10, 23), b'thequick\xff' ]) y = loads(format, x) assert [ True, 32, False, 3415, 4513134, 13241923419, True, 'thequickbrownfox', False, 'jumpsoverthelazydog', datetime(2015, 3, 13, 10, 23), b'thequick\xff' ] == y[0] def test_int_boundaries(self): format = b'F' x = dumps( format, [ { 'a': -2147483649, 'b': 2147483648 }, # celery/celery#3121 ]) y = loads(format, x) assert y[0] == [{ 'a': -2147483649, 'b': 2147483648, # celery/celery#3121 }] def test_loads_unknown_type(self): with pytest.raises(FrameSyntaxError): loads('y', 'asdsad') def test_float(self): assert (int(loads(b'fb', dumps(b'fb', [32.31, False]))[0][0] * 100) == 3231) def test_table(self): table = {'foo': 32, 'bar': 'baz', 'nil': None} assert loads(b'F', dumps(b'F', [table]))[0][0] == table def test_array(self): array = [ 'A', 1, True, 33.3, Decimal('55.5'), Decimal('-3.4'), datetime(2015, 3, 13, 10, 23), { 'quick': 'fox', 'amount': 1 }, [3, 'hens'], None, ] expected = list(array) expected[6] = _ANY() assert expected == loads('A', dumps('A', [array]))[0][0] def test_array_unknown_type(self): with pytest.raises(FrameSyntaxError): dumps('A', [[object()]])
def build_frame_type_2(body_len, channel, properties): frame = (b''.join( [pack('>HxxQ', spec.Basic.CLASS_ID, body_len), properties])) return 2, channel, frame