Example #1
0
def test_sync_receive_messages():
    c = nsq.SyncConn()
    c.connect("127.0.0.1", 4150)

    c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'}))
    c.send(nsq.subscribe('test', 'ch'))

    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')
    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == 'OK'

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == 'OK'

    for i in range(10):
        c.send(nsq.ready(1))
        body = '{"data": {"test_key": %d}}' % i
        ts = int(time.time() * 1000 * 1000)
        mock_response_write_message(c, ts, 0, i, body)
        resp = c.read_response()
        unpacked = nsq.unpack_response(resp)
        assert unpacked[0] == nsq.FRAME_TYPE_MESSAGE
        msg = nsq.decode_message(unpacked[1])
        assert msg.timestamp == ts
        assert msg.id == "%016d" % i
        assert msg.attempts == 0
        assert msg.body == body
Example #2
0
def test_sync_receive_messages():
    c = nsq.SyncConn()
    c.connect("127.0.0.1", 4150)

    c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'}))
    c.send(nsq.subscribe('test', 'ch'))

    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')
    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == six.b('OK')

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == six.b('OK')

    for i in range(10):
        c.send(nsq.ready(1))
        body = '{"data": {"test_key": %d}}' % i
        body = body.encode('utf-8')
        ts = int(time.time() * 1000 * 1000)
        mock_response_write_message(c, ts, 0, i, body)
        resp = c.read_response()
        unpacked = nsq.unpack_response(resp)
        assert unpacked[0] == nsq.FRAME_TYPE_MESSAGE
        msg = nsq.decode_message(unpacked[1])
        assert msg.timestamp == ts
        assert msg.id == ("%016d" % i).encode('utf-8')
        assert msg.attempts == 0
        assert msg.body == body
Example #3
0
 def data_callback(c, data):
     unpacked = nsq.unpack_response(data)
     if unpacked[0] == nsq.FRAME_TYPE_MESSAGE:
         c.send(nsq.ready(1))
         msg = nsq.decode_message(unpacked[1])
         print msg.id, msg.body
         c.send(nsq.finish(msg.id))
Example #4
0
 def data_callback(c, data):
     unpacked = nsq.unpack_response(data)
     if unpacked[0] == nsq.FRAME_TYPE_MESSAGE:
         c.send(nsq.ready(1))
         msg = nsq.decode_message(unpacked[1])
         print msg.id, msg.body
         c.send(nsq.finish(msg.id))
Example #5
0
def test_sync_authenticate_subscribe():
    c = nsq.SyncConn()
    c.connect("127.0.0.1", 4150)

    c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'}))
    c.send(nsq.subscribe('test', 'ch'))

    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')
    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == six.b('OK')

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == six.b('OK')
Example #6
0
def test_sync_authenticate_subscribe():
    c = nsq.SyncConn()
    c.connect("127.0.0.1", 4150)

    c.send(nsq.identify({'short_id': 'test', 'long_id': 'test.example'}))
    c.send(nsq.subscribe('test', 'ch'))

    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')
    mock_response_write(c, nsq.FRAME_TYPE_RESPONSE, 'OK')

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == 'OK'

    resp = c.read_response()
    unpacked = nsq.unpack_response(resp)
    assert unpacked[0] == nsq.FRAME_TYPE_RESPONSE
    assert unpacked[1] == 'OK'
Example #7
0
    def _on_data(self, data, **kwargs):
        self.last_recv_timestamp = time.time()
        frame, data = nsq.unpack_response(data)
        if frame == nsq.FRAME_TYPE_MESSAGE:
            self.last_msg_timestamp = time.time()
            self.rdy = max(self.rdy - 1, 0)
            self.in_flight += 1

            message = nsq.decode_message(data)
            message.on('finish', self._on_message_finish)
            message.on('requeue', self._on_message_requeue)
            message.on('touch', self._on_message_touch)

            self.trigger('message', conn=self, message=message)
        elif frame == nsq.FRAME_TYPE_RESPONSE and data == '_heartbeat_':
            self.send(nsq.nop())
            self.trigger('heartbeat', conn=self)
        elif frame == nsq.FRAME_TYPE_RESPONSE:
            self.trigger('response', conn=self, data=data)
        elif frame == nsq.FRAME_TYPE_ERROR:
            self.trigger('error', conn=self, error=nsq.Error(data))
Example #8
0
        while True:
            if len(self.buffer) >= size:
                break
            packet = self.s.recv(4096)
            if not packet:
                raise Exception("failed to read %d" % size)
            self.buffer += packet
        data = self.buffer[:size]
        self.buffer = self.buffer[size:]
        return data

    def read_response(self):
        size = struct.unpack('>l', self._readn(4))[0]
        return self._readn(size)

    def send(self, data):
        self.s.send(data)


if __name__ == '__main__':
    c = SyncConn()
    c.connect("127.0.0.1", 4150)
    c.send(nsq.subscribe('test', 'ch', 'a', 'b'))
    for i in xrange(10):
        c.send(nsq.ready(1))
        resp = c.read_response()
        unpacked = nsq.unpack_response(resp)
        msg = nsq.decode_message(unpacked[1])
        print msg.id, msg.body
        c.send(nsq.finish(msg.id))
Example #9
0
        while True:
            if len(self.buffer) >= size:
                break
            packet = self.s.recv(4096)
            if not packet:
                raise Exception("failed to read %d" % size)
            self.buffer += packet
        data = self.buffer[:size]
        self.buffer = self.buffer[size:]
        return data
    
    def read_response(self):
        size = struct.unpack('>l', self._readn(4))[0]
        return self._readn(size)
    
    def send(self, data):
        self.s.send(data)


if __name__ == '__main__':
    c = SyncConn()
    c.connect("127.0.0.1", 4150)
    c.send(nsq.subscribe('test', 'ch', 'a', 'b'))
    for i in xrange(10):
        c.send(nsq.ready(1))
        resp = c.read_response()
        unpacked = nsq.unpack_response(resp)
        msg = nsq.decode_message(unpacked[1])
        print msg.id, msg.body
        c.send(nsq.finish(msg.id))