diff --git a/neo4j/v1/__init__.py b/neo4j/v1/__init__.py index 5c275455..ab5f7664 100644 --- a/neo4j/v1/__init__.py +++ b/neo4j/v1/__init__.py @@ -18,6 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from .connection import ProtocolError from .constants import * from .session import * from .types import * diff --git a/test/test_stability.py b/test/test_stability.py new file mode 100644 index 00000000..12c729a2 --- /dev/null +++ b/test/test_stability.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +# Copyright (c) 2002-2016 "Neo Technology," +# Network Engine for Objects in Lund AB [http://neotechnology.com] +# +# This file is part of Neo4j. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from neo4j.v1 import GraphDatabase, basic_auth, ProtocolError + +from test.util import ServerTestCase, restart_server + + +auth_token = basic_auth("neo4j", "password") + + +class ServerRestartTestCase(ServerTestCase): + + def test_server_shutdown_detection(self): + driver = GraphDatabase.driver("bolt://localhost", auth=auth_token) + session = driver.session() + session.run("RETURN 1").consume() + assert restart_server() + with self.assertRaises(ProtocolError): + session.run("RETURN 1").consume() + session.close() diff --git a/test/util.py b/test/util.py index 987a7e9d..8ce45806 100644 --- a/test/util.py +++ b/test/util.py @@ -20,8 +20,11 @@ import functools -from os import remove, rename +from os import getenv, remove, rename from os.path import isfile +from socket import create_connection +from subprocess import check_call, CalledProcessError +from time import sleep from unittest import TestCase from neo4j.util import Watcher @@ -48,6 +51,32 @@ def wrapper(*args, **kwargs): return wrapper +def restart_server(http_port=7474): + try: + check_call("%s/bin/neo4j restart" % getenv("NEO4J_HOME"), shell=True) + except CalledProcessError as error: + if error.returncode == 2: + raise OSError("Another process is listening on the server port") + elif error.returncode == 512: + raise OSError("Another server process is already running") + else: + raise OSError("An error occurred while trying to start " + "the server [%s]" % error.returncode) + else: + running = False + t = 0 + while not running and t < 30: + try: + s = create_connection(("localhost", http_port)) + except IOError: + sleep(1) + t += 1 + else: + s.close() + running = True + return running + + class ServerTestCase(TestCase): """ Base class for test cases that use a remote server. """