Skip to content

Commit

Permalink
[app][fix] dont sys exit on connection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
M3ssman committed Aug 29, 2024
1 parent ff75621 commit 4ae4395
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "digiflow"
version = "5.1.3"
version = "5.1.4"
description = "Father's Little Digitization Workflow Helper"
readme = "README.md"
requires-python = ">=3.8"
Expand Down
8 changes: 4 additions & 4 deletions src/digiflow/record/record_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import http.server
import json
import logging
import sys

from pathlib import Path

Expand Down Expand Up @@ -219,6 +218,7 @@ def __init__(self, oai_record_list_label, host, port,
self.oai_record_list_label = oai_record_list_label
self.record: df_r.Record = None
self.oai_server_url = f'http://{host}:{port}/{oai_record_list_label}'
self.timeout_secs = 30
super().__init__(some_logger=logger)

def get_record(self, get_record_state, set_record_state):
Expand All @@ -229,11 +229,11 @@ def get_record(self, get_record_state, set_record_state):
the_headers = {X_HEADER_GET_STATE: get_record_state,
X_HEADER_SET_STATE: set_record_state}
response = requests.get(f'{self.oai_server_url}/next',
timeout=300, headers=the_headers)
timeout=self.timeout_secs, headers=the_headers)
except requests.exceptions.RequestException as err:
if self.logger is not None:
self.logger.error("connection fails: %s", err)
sys.exit(1)
self.logger.error("Connection failure: %s", err)
raise RecordsServiceException(f"Connection failure: {err}") from err
status = response.status_code
result = response.content
if status == 404:
Expand Down
31 changes: 25 additions & 6 deletions tests/test_record_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import typing
import unittest.mock

import requests.exceptions as req_ex
import pytest

import digiflow.record as df_r
Expand Down Expand Up @@ -150,18 +151,36 @@ def test_exit_on_data_exhausted(mock_request):
"""

# arrange
_the_list_label = 'oai-record-test'
_rsp = f'{df_rs.DATA_EXHAUSTED_MARK.format(_the_list_label)}'.encode()
client = df_r.Client(_the_list_label, '1.2.3.4', '9999')
list_label = 'oai-record-test'
srv_rsp = f'{df_rs.DATA_EXHAUSTED_MARK.format(list_label)}'.encode()
client = df_r.Client(list_label, '1.2.3.4', '9999')
mock_resp = unittest.mock.Mock()
mock_resp.status_code = 404
mock_resp.headers = {'Content-Type': 'text/xml'}
mock_resp.content = _rsp
mock_resp.content = srv_rsp
mock_request.return_value = mock_resp

# act
with pytest.raises(SystemExit) as proc_exit:
with pytest.raises(df_r.RecordsExhaustedException) as recs_ex:
client.get_record(get_record_state=df_r.UNSET_LABEL, set_record_state='busy')

# assert
assert proc_exit.value.args[0] == 1
assert recs_ex.value.args[0] == f'no open records in {list_label}'


@unittest.mock.patch('digiflow.requests.get')
def test_client_connection_timeout(mock_request):
"""Behavior when server down"""

# arrange
a_label = 'oai-record-test'
client = df_r.Client(a_label, '1.2.3.4', '9999')
exc_msg = f'connection timeout after {client.timeout_secs} seconds'
mock_request.side_effect = req_ex.ConnectTimeout(exc_msg)

# act
with pytest.raises(df_r.RecordsServiceException) as rse:
client.get_record(get_record_state=df_r.UNSET_LABEL, set_record_state='busy')

# assert
assert exc_msg in rse.value.args[0]

0 comments on commit 4ae4395

Please sign in to comment.