Skip to content

Test server disconnect is detected #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions neo4j/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
39 changes: 39 additions & 0 deletions test/test_stability.py
Original file line number Diff line number Diff line change
@@ -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()
31 changes: 30 additions & 1 deletion test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
"""
Expand Down