def test_multipublish(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(11) == b'MPUB topic\n' size = nsq.unpack_size(socket.recv(4)) data = socket.recv(size) head, data = data[:4], data[4:] assert nsq.unpack_size(head) == 2 for _ in range(2): head, data = data[:4], data[4:] assert nsq.unpack_size(head) == 3 head, data = data[:3], data[3:] assert head == b'sup' assert data == b'' with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() conn.multipublish('topic', [b'sup', b'sup'])
def test_command(command, args, resp): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(len(resp)) == resp with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() getattr(conn, command)(*args)
def test_cls_error(): with NsqdIntegrationServer() as server: conn = NsqdTCPClient(server.address, server.tcp_port) conn.connect() assert conn.state == states.CONNECTED conn.close() frame, error = conn.read_response() assert frame == nsq.FRAME_TYPE_ERROR assert isinstance(error, errors.NSQInvalid)
def test_deferpublish(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(14) == b'DPUB topic 42\n' assert nsq.unpack_size(socket.recv(4)) == 3 assert socket.recv(3) == b'sup' with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() conn.publish('topic', b'sup', defer=42)
def test_read(body): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' socket.sendall(struct.pack('>l', len(body))) socket.sendall(body) assert socket.recv(1) == b'' with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() assert conn._read_response() == body conn.close_stream()
def test_error(error_msg, error, fatal): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' socket.sendall(mock_response(nsq.FRAME_TYPE_ERROR, error_msg)) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() frame, resp = conn.read_response() assert frame == nsq.FRAME_TYPE_ERROR assert isinstance(resp, getattr(errors, error)) assert conn.is_connected != fatal
def test_sync_heartbeat(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, b'_heartbeat_')) assert socket.recv(4) == b'NOP\n' with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() frame, data = conn.read_response() assert frame == nsq.FRAME_TYPE_RESPONSE assert data == b'_heartbeat_'
def test_disconnected(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(1) == b'' with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() conn.close_stream() assert conn.state == states.DISCONNECTED with pytest.raises(errors.NSQSocketError): conn.nop() with pytest.raises(errors.NSQSocketError): conn.read_response()
def test_identify(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(9) == b'IDENTIFY\n' size = nsq.unpack_size(socket.recv(4)) data = json.loads(socket.recv(size).decode('utf-8')) assert 'gnsq' in data['user_agent'] socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK')) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() assert conn.identify() is None
def test_sync_receive_messages(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(9) == b'IDENTIFY\n' size = nsq.unpack_size(socket.recv(4)) data = json.loads(socket.recv(size).decode('utf-8')) assert isinstance(data, dict) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK')) msg = b'SUB topic channel\n' assert socket.recv(len(msg)) == msg socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK')) for i in range(10): assert socket.recv(6) == b'RDY 1\n' body = six.b(json.dumps({'data': {'test_key': i}})) ts = i * 1000 * 1000 socket.sendall(mock_response_message(ts, i, i, body)) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() assert conn.identify() is None conn.subscribe('topic', 'channel') frame, data = conn.read_response() assert frame == nsq.FRAME_TYPE_RESPONSE assert data == b'OK' for i in range(10): conn.ready(1) frame, msg = conn.read_response() assert frame == nsq.FRAME_TYPE_MESSAGE assert isinstance(msg, Message) assert msg.timestamp == i * 1000 * 1000 assert msg.id == six.b('%016d' % i) assert msg.attempts == i assert json.loads( msg.body.decode('utf-8'))['data']['test_key'] == i
def test_sync_receive_messages(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(9) == b'IDENTIFY\n' size = nsq.unpack_size(socket.recv(4)) data = json.loads(socket.recv(size).decode('utf-8')) assert isinstance(data, dict) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK')) msg = b'SUB topic channel\n' assert socket.recv(len(msg)) == msg socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, b'OK')) for i in range(10): assert socket.recv(6) == b'RDY 1\n' body = six.b(json.dumps({'data': {'test_key': i}})) ts = i * 1000 * 1000 socket.sendall(mock_response_message(ts, i, i, body)) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() assert conn.identify() is None conn.subscribe('topic', 'channel') frame, data = conn.read_response() assert frame == nsq.FRAME_TYPE_RESPONSE assert data == b'OK' for i in range(10): conn.ready(1) frame, msg = conn.read_response() assert frame == nsq.FRAME_TYPE_MESSAGE assert isinstance(msg, Message) assert msg.timestamp == i * 1000 * 1000 assert msg.id == six.b('%016d' % i) assert msg.attempts == i assert json.loads(msg.body.decode('utf-8'))['data']['test_key'] == i
def test_connection(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(1) == b'' with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) assert conn.state == states.INIT conn.connect() assert conn.state == states.CONNECTED conn.connect() assert conn.state == states.CONNECTED conn.close_stream() assert conn.state == states.DISCONNECTED
def test_negotiation(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(9) == b'IDENTIFY\n' size = nsq.unpack_size(socket.recv(4)) data = json.loads(socket.recv(size).decode('utf-8')) assert 'gnsq' in data['user_agent'] resp = six.b(json.dumps({'test': 42})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port) conn.connect() assert conn.identify()['test'] == 42
def test_auth(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(5) == b'AUTH\n' assert nsq.unpack_size(socket.recv(4)) == 6 assert socket.recv(6) == b'secret' resp = six.b(json.dumps({'identity': 'awesome'})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) with handle as server: conn = NsqdTCPClient( '127.0.0.1', server.server_port, auth_secret=b'secret') conn.connect() resp = conn.auth() assert resp['identity'] == 'awesome'
def test_auth(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(5) == b'AUTH\n' assert nsq.unpack_size(socket.recv(4)) == 6 assert socket.recv(6) == b'secret' resp = six.b(json.dumps({'identity': 'awesome'})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port, auth_secret=b'secret') conn.connect() resp = conn.auth() assert resp['identity'] == 'awesome'
def test_identify_auth(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(9) == b'IDENTIFY\n' size = nsq.unpack_size(socket.recv(4)) data = json.loads(socket.recv(size).decode('utf-8')) assert 'gnsq' in data['user_agent'] resp = six.b(json.dumps({'auth_required': True})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) assert socket.recv(5) == b'AUTH\n' assert nsq.unpack_size(socket.recv(4)) == 6 assert socket.recv(6) == b'secret' resp = six.b(json.dumps({'identity': 'awesome'})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) with handle as server: conn = NsqdTCPClient('127.0.0.1', server.server_port, auth_secret=b'secret') @conn.on_auth.connect def assert_auth(conn, response): assert assert_auth.was_called is False assert_auth.was_called = True assert response['identity'] == 'awesome' assert_auth.was_called = False conn.connect() resp = conn.identify() assert resp['auth_required'] assert assert_auth.was_called
def test_identify_auth(): @mock_server def handle(socket, address): assert socket.recv(4) == b' V2' assert socket.recv(9) == b'IDENTIFY\n' size = nsq.unpack_size(socket.recv(4)) data = json.loads(socket.recv(size).decode('utf-8')) assert 'gnsq' in data['user_agent'] resp = six.b(json.dumps({'auth_required': True})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) assert socket.recv(5) == b'AUTH\n' assert nsq.unpack_size(socket.recv(4)) == 6 assert socket.recv(6) == b'secret' resp = six.b(json.dumps({'identity': 'awesome'})) socket.sendall(mock_response(nsq.FRAME_TYPE_RESPONSE, resp)) with handle as server: conn = NsqdTCPClient( '127.0.0.1', server.server_port, auth_secret=b'secret') @conn.on_auth.connect def assert_auth(conn, response): assert assert_auth.was_called is False assert_auth.was_called = True assert response['identity'] == 'awesome' assert_auth.was_called = False conn.connect() resp = conn.identify() assert resp['auth_required'] assert assert_auth.was_called
def test_socket_upgrades(tls, deflate, snappy): with NsqdIntegrationServer() as server: options = { 'address': server.address, 'port': server.tcp_port, 'deflate': deflate, 'snappy': snappy, } if tls: options.update({ 'tls_v1': True, 'tls_options': { 'keyfile': server.tls_key, 'certfile': server.tls_cert, } }) conn = NsqdTCPClient(**options) conn.connect() assert conn.state == states.CONNECTED if deflate and snappy: with pytest.raises(errors.NSQErrorCode): conn.identify() return if tls and BAD_GEVENT: with pytest.raises(AttributeError): conn.identify() return if tls and server.version < (0, 2, 28): with pytest.raises(ssl.SSLError): conn.identify() return resp = conn.identify() assert isinstance(resp, dict) assert resp['tls_v1'] is tls assert resp['deflate'] is deflate assert resp['snappy'] is snappy if tls and (deflate or snappy): assert isinstance(conn.stream.socket._socket, SSLSocket) elif tls: assert isinstance(conn.stream.socket, SSLSocket) if deflate: assert isinstance(conn.stream.socket, DefalteSocket) if snappy: assert isinstance(conn.stream.socket, SnappySocket) conn.publish('topic', b'sup') frame, data = conn.read_response() assert frame == nsq.FRAME_TYPE_RESPONSE assert data == b'OK' conn.subscribe('topic', 'channel') frame, data = conn.read_response() assert frame == nsq.FRAME_TYPE_RESPONSE assert data == b'OK' conn.ready(1) frame, data = conn.read_response() assert frame == nsq.FRAME_TYPE_MESSAGE assert data.body == b'sup' conn.close_stream()