def test_sync_receive_messages(): c = sync.SyncConn() c.connect("127.0.0.1", 4150) c.send(protocol.identify({'short_id': 'test', 'long_id': 'test.example'})) c.send(protocol.subscribe('test', 'ch')) mock_response_write(c, protocol.FRAME_TYPE_RESPONSE, b'OK') mock_response_write(c, protocol.FRAME_TYPE_RESPONSE, b'OK') resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_RESPONSE assert unpacked[1] == b'OK' resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_RESPONSE assert unpacked[1] == b'OK' for i in range(10): c.send(protocol.ready(1)) body = compat.b('{"data": {"test_key": %d}}' % i, "utf8") ts = int(time.time() * 1000 * 1000) mock_response_write_message(c, ts, 0, i, body) resp = c.read_response() unpacked = protocol.unpack_response(resp) assert unpacked[0] == protocol.FRAME_TYPE_MESSAGE msg = protocol.decode_message(unpacked[1]) assert msg.timestamp == ts assert msg.id == compat.b("%016d" % i, "ascii") assert msg.attempts == 0 assert msg.body == body
def mock_response_write_message(c, timestamp, attempts, id, body): timestamp_packed = struct.pack('>q', timestamp) attempts_packed = struct.pack('>h', attempts) id = compat.b("%016d" % id, "ascii") mock_response_write( c, protocol.FRAME_TYPE_MESSAGE, (timestamp_packed + attempts_packed + id + body) )
def _command(cmd, body, *params): body_prefix = b'' params_data = b'' if body: assert isinstance(body, compat.string_like), 'body must be a string' if isinstance(body, compat.unicode): body = body.encode('utf-8') body_prefix = compat.b(struct.pack(b'>l', len(body))) else: body = b'' # None or u"" if len(params): params = [ p.encode('utf-8') if isinstance(p, compat.unicode) else p for p in params ] params_data = b' ' + b' '.join(params) return b''.join((cmd, params_data, NL, body_prefix, body))
def test_backoff_easy(): mock_ioloop = _make_ioloop() r = _get_reader(mock_ioloop) conn = _get_conn(r) msg = _send_message(conn) msg.trigger(event.FINISH, message=msg) assert r.backoff_block is False assert r.backoff_timer.get_interval() == 0 msg = _send_message(conn) msg.trigger(event.REQUEUE, message=msg) assert r.backoff_block is True assert r.backoff_timer.get_interval() > 0 assert mock_ioloop.add_timeout.called timeout_args, timeout_kwargs = mock_ioloop.add_timeout.call_args timeout_args[1]() assert r.backoff_block is False send_args, send_kwargs = conn.stream.write.call_args assert send_args[0] == compat.b('RDY 1\n') msg = _send_message(conn) msg.trigger(event.FINISH, message=msg) assert r.backoff_block is False assert r.backoff_timer.get_interval() == 0 expected_args = [ 'SUB test test\n', 'RDY 1\n', 'RDY 5\n', 'FIN 1234\n', 'RDY 0\n', 'REQ 1234 0\n', 'RDY 1\n', 'RDY 5\n', 'FIN 1234\n' ] _assert_wrote_expected(conn, expected_args)
def _assert_wrote_expected(conn, expected_args): assert conn.stream.write.call_args_list == [((compat.b(arg),),) for arg in expected_args]
from __future__ import absolute_import import struct import re try: import simplejson as json except ImportError: import json # pyflakes.ignore from .message import Message from nsq import compat MAGIC_V2 = compat.b(' V2') NL = compat.b('\n') FRAME_TYPE_RESPONSE = 0 FRAME_TYPE_ERROR = 1 FRAME_TYPE_MESSAGE = 2 # commmands AUTH = compat.b('AUTH') FIN = compat.b('FIN') # success IDENTIFY = compat.b('IDENTIFY') MPUB = compat.b('MPUB') NOP = compat.b('NOP') PUB = compat.b('PUB') # publish RDY = compat.b('RDY')