Skip to content

Commit 363da25

Browse files
author
Zhen Li
committed
Merge pull request #50 from neo4j/1.0-stability-tests
Test server disconnect is detected
2 parents c559093 + 052dd65 commit 363da25

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
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 *

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)