This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from Connexions/database_restart
Creating a restart function for storage classes to fix broken database connections
- Loading branch information
Showing
7 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[run] | ||
source = cnxauthoring | ||
branch = True | ||
|
||
[report] | ||
# Regexes for lines to exclude from consideration | ||
exclude_lines = | ||
# Have to re-enable the standard pragma | ||
pragma: no cover | ||
raise NotImplementedError | ||
omit = | ||
cnxauthoring/scripts/* | ||
cnxauthoring/tests/* | ||
ignore_errors = True | ||
|
||
[html] | ||
directory = coverage_html_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2773,6 +2773,33 @@ def test_user_contents_hide_documents_inside_binders(self): | |
}, | ||
}) | ||
|
||
def test_db_restart(self): | ||
''' | ||
Test to see if the database resets itself after a broken | ||
connection | ||
''' | ||
import psycopg2 | ||
from ..storage import storage | ||
|
||
self.addCleanup(setattr, storage, 'conn', | ||
psycopg2.connect(storage.conn.dsn)) | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.post_json( | ||
'/users/contents', | ||
{'title': u'My document タイトル'}, | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
response = self.testapp.post_json( | ||
'/users/contents', | ||
{'title': u'My document タイトル'}, | ||
status=201, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '201 Created') | ||
|
||
def test_service_unavailable_response(self): | ||
''' | ||
Test service unavailable response when a request is made during a | ||
|
@@ -2793,31 +2820,64 @@ def test_service_unavailable_response(self): | |
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.get( | ||
'/resources/1234abcde', | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.put_json( | ||
'/contents/[email protected]', | ||
{}, | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.get( | ||
'/search', | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
storage.conn.close() | ||
|
||
response = self.testapp.delete( | ||
'/contents/{}@draft'.format(id), | ||
status=503, | ||
expect_errors=True) | ||
self.assertEqual(response.status, '503 Service Unavailable') | ||
|
||
@mock.patch('cnxauthoring.views.logger') | ||
def test_database_restart_failed(self, logger): | ||
import psycopg2 | ||
from ..storage import storage | ||
|
||
self.addCleanup(setattr, storage, 'conn', | ||
psycopg2.connect(storage.conn.dsn)) | ||
|
||
storage.conn.close() | ||
|
||
with mock.patch.object(storage, 'restart') as mock_restart: | ||
mock_restart.side_effect = storage.Error | ||
|
||
response = self.testapp.post_json( | ||
'/users/contents', | ||
{'title': 'Test Document'}, | ||
status=503) | ||
self.assertEqual(mock_restart.call_count, 1) | ||
|
||
self.assertEqual(logger.exception.call_count, 3) | ||
args1, args2, args3 = logger.exception.call_args_list | ||
self.assertEqual(args1[0], ('Storage failure',)) | ||
self.assertEqual(args2[0], ('Storage failed to abort',)) | ||
self.assertEqual(args3[0], ('Storage failed to restart',)) | ||
|
||
|
||
class PublicationTests(BaseFunctionalTestCase): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters