Skip to content

Commit a607d93

Browse files
committed
Test server disconnect is detected
1 parent 18dd645 commit a607d93

File tree

4 files changed

+89
-17
lines changed

4 files changed

+89
-17
lines changed

neo4j/v1/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21+
from .connection import ProtocolError
2122
from .constants import *
2223
from .session import *
2324
from .types import *

runtests.sh

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@ DRIVER_HOME=$(dirname $0)
2121

2222
NEORUN_OPTIONS=""
2323
RUNNING=0
24+
QUICK=0
2425
KNOWN_HOSTS="${HOME}/.neo4j/known_hosts"
2526
KNOWN_HOSTS_BACKUP="${KNOWN_HOSTS}.backup"
2627

2728
FG_BRIGHT_RED='\033[1;31m'
2829
FG_DEFAULT='\033[0m'
2930

3031
# Parse options
31-
while getopts ":dr" OPTION
32+
while getopts ":dqr" OPTION
3233
do
3334
case ${OPTION} in
3435
d)
3536
NEORUN_OPTIONS="-f"
3637
;;
38+
q)
39+
QUICK=1
40+
;;
3741
r)
3842
RUNNING=1
3943
;;
@@ -86,27 +90,26 @@ then
8690
${TEST_RUNNER}
8791
check_exit_status $?
8892
else
89-
#echo "Updating password"
90-
#mv ${KNOWN_HOSTS} ${KNOWN_HOSTS_BACKUP}
91-
#neokit/neorun ${NEORUN_OPTIONS} "python -m test.auth password" ${VERSIONS}
92-
#EXIT_STATUS=$?
93-
#mv ${KNOWN_HOSTS_BACKUP} ${KNOWN_HOSTS}
94-
#check_exit_status ${EXIT_STATUS}
9593
export NEO4J_PASSWORD="password"
9694

9795
echo "Running unit tests"
9896
neokit/neorun ${NEORUN_OPTIONS} "${TEST_RUNNER}" ${VERSIONS}
9997
check_exit_status $?
10098

101-
echo "Testing example code"
102-
neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS}
103-
check_exit_status $?
99+
if [ ${QUICK} -eq 0 ]
100+
then
101+
echo "Testing example code"
102+
neokit/neorun ${NEORUN_OPTIONS} "${EXAMPLES_RUNNER}" ${VERSIONS}
103+
check_exit_status $?
104+
105+
echo "Testing TCK"
106+
coverage report --show-missing
107+
python -c 'from test.tck.configure_feature_files import *; set_up()'
108+
echo "Feature files downloaded"
109+
neokit/neorun ${NEORUN_OPTIONS} "${BEHAVE_RUNNER}" ${VERSIONS}
110+
python -c 'from test.tck.configure_feature_files import *; clean_up()'
111+
echo "Feature files removed"
104112

105-
coverage report --show-missing
106-
python -c 'from test.tck.configure_feature_files import *; set_up()'
107-
echo "Feature files downloaded"
108-
neokit/neorun ${NEORUN_OPTIONS} "${BEHAVE_RUNNER}" ${VERSIONS}
109-
python -c 'from test.tck.configure_feature_files import *; clean_up()'
110-
echo "Feature files removed"
113+
fi
111114

112115
fi

test/test_stability.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2016 "Neo Technology,"
5+
# Network Engine for Objects in Lund AB [http://neotechnology.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
22+
from neo4j.v1 import GraphDatabase, basic_auth, ProtocolError
23+
24+
from test.util import ServerTestCase, restart_server
25+
26+
27+
auth_token = basic_auth("neo4j", "password")
28+
29+
30+
class ServerRestartTestCase(ServerTestCase):
31+
32+
def test_server_shutdown_detection(self):
33+
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
34+
session = driver.session()
35+
session.run("RETURN 1").consume()
36+
assert restart_server()
37+
with self.assertRaises(ProtocolError):
38+
session.run("RETURN 1").consume()
39+
session.close()

test/util.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020

2121

2222
import functools
23-
from os import remove, rename
23+
from os import getenv, remove, rename
2424
from os.path import isfile
25+
from socket import create_connection
26+
from subprocess import check_call, CalledProcessError
27+
from time import sleep
2528
from unittest import TestCase
2629

2730
from neo4j.util import Watcher
@@ -48,6 +51,32 @@ def wrapper(*args, **kwargs):
4851
return wrapper
4952

5053

54+
def restart_server(http_port=7474):
55+
try:
56+
check_call("%s/bin/neo4j restart" % getenv("NEO4J_HOME"), shell=True)
57+
except CalledProcessError as error:
58+
if error.returncode == 2:
59+
raise OSError("Another process is listening on the server port")
60+
elif error.returncode == 512:
61+
raise OSError("Another server process is already running")
62+
else:
63+
raise OSError("An error occurred while trying to start "
64+
"the server [%s]" % error.returncode)
65+
else:
66+
running = False
67+
t = 0
68+
while not running and t < 30:
69+
try:
70+
s = create_connection(("localhost", http_port))
71+
except IOError:
72+
sleep(1)
73+
t += 1
74+
else:
75+
s.close()
76+
running = True
77+
return running
78+
79+
5180
class ServerTestCase(TestCase):
5281
""" Base class for test cases that use a remote server.
5382
"""

0 commit comments

Comments
 (0)