From a607d930c4fdf663c5cf840db52e973ce1b5a0db Mon Sep 17 00:00:00 2001 From: Nigel Small Date: Wed, 6 Apr 2016 16:50:32 +0100 Subject: [PATCH 1/2] Test server disconnect is detected --- neo4j/v1/__init__.py | 1 + runtests.sh | 35 +++++++++++++++++++---------------- test/test_stability.py | 39 +++++++++++++++++++++++++++++++++++++++ test/util.py | 31 ++++++++++++++++++++++++++++++- 4 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 test/test_stability.py 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/runtests.sh b/runtests.sh index ccd63d82..b4f13342 100755 --- a/runtests.sh +++ b/runtests.sh @@ -21,6 +21,7 @@ DRIVER_HOME=$(dirname $0) NEORUN_OPTIONS="" RUNNING=0 +QUICK=0 KNOWN_HOSTS="${HOME}/.neo4j/known_hosts" KNOWN_HOSTS_BACKUP="${KNOWN_HOSTS}.backup" @@ -28,12 +29,15 @@ FG_BRIGHT_RED='\033[1;31m' FG_DEFAULT='\033[0m' # Parse options -while getopts ":dr" OPTION +while getopts ":dqr" OPTION do case ${OPTION} in d) NEORUN_OPTIONS="-f" ;; + q) + QUICK=1 + ;; r) RUNNING=1 ;; @@ -86,27 +90,26 @@ then ${TEST_RUNNER} check_exit_status $? else - #echo "Updating password" - #mv ${KNOWN_HOSTS} ${KNOWN_HOSTS_BACKUP} - #neokit/neorun ${NEORUN_OPTIONS} "python -m test.auth password" ${VERSIONS} - #EXIT_STATUS=$? - #mv ${KNOWN_HOSTS_BACKUP} ${KNOWN_HOSTS} - #check_exit_status ${EXIT_STATUS} export NEO4J_PASSWORD="password" echo "Running unit tests" neokit/neorun ${NEORUN_OPTIONS} "${TEST_RUNNER}" ${VERSIONS} check_exit_status $? - echo "Testing example code" - neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS} - check_exit_status $? + if [ ${QUICK} -eq 0 ] + then + echo "Testing example code" + neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS} + check_exit_status $? + + echo "Testing TCK" + coverage report --show-missing + python -c 'from test.tck.configure_feature_files import *; set_up()' + echo "Feature files downloaded" + neokit/neorun ${NEORUN_OPTIONS} "${BEHAVE_RUNNER}" ${VERSIONS} + python -c 'from test.tck.configure_feature_files import *; clean_up()' + echo "Feature files removed" - coverage report --show-missing - python -c 'from test.tck.configure_feature_files import *; set_up()' - echo "Feature files downloaded" - neokit/neorun ${NEORUN_OPTIONS} "${BEHAVE_RUNNER}" ${VERSIONS} - python -c 'from test.tck.configure_feature_files import *; clean_up()' - echo "Feature files removed" + fi fi 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. """ From 052dd65e0f6e11de3cefb2870c88ce8a5a5e4dca Mon Sep 17 00:00:00 2001 From: Nigel Small Date: Fri, 8 Apr 2016 21:22:44 +0100 Subject: [PATCH 2/2] Reverted runtests -q --- runtests.sh | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/runtests.sh b/runtests.sh index b4f13342..ccd63d82 100755 --- a/runtests.sh +++ b/runtests.sh @@ -21,7 +21,6 @@ DRIVER_HOME=$(dirname $0) NEORUN_OPTIONS="" RUNNING=0 -QUICK=0 KNOWN_HOSTS="${HOME}/.neo4j/known_hosts" KNOWN_HOSTS_BACKUP="${KNOWN_HOSTS}.backup" @@ -29,15 +28,12 @@ FG_BRIGHT_RED='\033[1;31m' FG_DEFAULT='\033[0m' # Parse options -while getopts ":dqr" OPTION +while getopts ":dr" OPTION do case ${OPTION} in d) NEORUN_OPTIONS="-f" ;; - q) - QUICK=1 - ;; r) RUNNING=1 ;; @@ -90,26 +86,27 @@ then ${TEST_RUNNER} check_exit_status $? else + #echo "Updating password" + #mv ${KNOWN_HOSTS} ${KNOWN_HOSTS_BACKUP} + #neokit/neorun ${NEORUN_OPTIONS} "python -m test.auth password" ${VERSIONS} + #EXIT_STATUS=$? + #mv ${KNOWN_HOSTS_BACKUP} ${KNOWN_HOSTS} + #check_exit_status ${EXIT_STATUS} export NEO4J_PASSWORD="password" echo "Running unit tests" neokit/neorun ${NEORUN_OPTIONS} "${TEST_RUNNER}" ${VERSIONS} check_exit_status $? - if [ ${QUICK} -eq 0 ] - then - echo "Testing example code" - neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS} - check_exit_status $? - - echo "Testing TCK" - coverage report --show-missing - python -c 'from test.tck.configure_feature_files import *; set_up()' - echo "Feature files downloaded" - neokit/neorun ${NEORUN_OPTIONS} "${BEHAVE_RUNNER}" ${VERSIONS} - python -c 'from test.tck.configure_feature_files import *; clean_up()' - echo "Feature files removed" + echo "Testing example code" + neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS} + check_exit_status $? - fi + coverage report --show-missing + python -c 'from test.tck.configure_feature_files import *; set_up()' + echo "Feature files downloaded" + neokit/neorun ${NEORUN_OPTIONS} "${BEHAVE_RUNNER}" ${VERSIONS} + python -c 'from test.tck.configure_feature_files import *; clean_up()' + echo "Feature files removed" fi