Skip to content
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

Added support for timeouts in messaging module #132

Merged
merged 1 commit into from
Mar 9, 2018
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ dist/
*~
scripts/cert.json
scripts/apikey.txt
htmlcov/
.pytest_cache/
10 changes: 8 additions & 2 deletions firebase_admin/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ def __init__(self, app):
'GCLOUD_PROJECT environment variable.')
self._fcm_url = _MessagingService.FCM_URL.format(project_id)
self._client = _http_client.JsonHttpClient(credential=app.credential.get_credential())
self._timeout = app.options.get('httpTimeout')

@classmethod
def encode_message(cls, message):
Expand All @@ -760,7 +761,7 @@ def send(self, message, dry_run=False):
if dry_run:
data['validate_only'] = True
try:
resp = self._client.body('post', url=self._fcm_url, json=data)
resp = self._client.body('post', url=self._fcm_url, json=data, timeout=self._timeout)
except requests.exceptions.RequestException as error:
if error.response is not None:
self._handle_fcm_error(error)
Expand Down Expand Up @@ -791,7 +792,12 @@ def make_topic_management_request(self, tokens, topic, operation):
url = '{0}/{1}'.format(_MessagingService.IID_URL, operation)
try:
resp = self._client.body(
'post', url=url, json=data, headers=_MessagingService.IID_HEADERS)
'post',
url=url,
json=data,
headers=_MessagingService.IID_HEADERS,
timeout=self._timeout
)
except requests.exceptions.RequestException as error:
if error.response is not None:
self._handle_iid_error(error)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,44 @@ def test_aps_alert(self):
check_encoding(msg, expected)


class TestTimeout(object):

@classmethod
def setup_class(cls):
cred = testutils.MockCredential()
firebase_admin.initialize_app(cred, {'httpTimeout': 4, 'projectId': 'explicit-project-id'})

@classmethod
def teardown_class(cls):
testutils.cleanup_apps()

def setup(self):
app = firebase_admin.get_app()
self.fcm_service = messaging._get_messaging_service(app)
self.recorder = []

def test_send(self):
self.fcm_service._client.session.mount(
'https://fcm.googleapis.com',
testutils.MockAdapter(json.dumps({'name': 'message-id'}), 200, self.recorder))
msg = messaging.Message(topic='foo')
messaging.send(msg)
assert len(self.recorder) == 1
assert self.recorder[0]._extra_kwargs['timeout'] == 4

def test_topic_management_timeout(self):
self.fcm_service._client.session.mount(
'https://iid.googleapis.com',
testutils.MockAdapter(
json.dumps({'results': [{}, {'error': 'error_reason'}]}),
200,
self.recorder)
)
messaging.subscribe_to_topic(['1'], 'a')
assert len(self.recorder) == 1
assert self.recorder[0]._extra_kwargs['timeout'] == 4


class TestSend(object):

_DEFAULT_RESPONSE = json.dumps({'name': 'message-id'})
Expand Down Expand Up @@ -817,6 +855,7 @@ def test_send(self):
assert len(recorder) == 1
assert recorder[0].method == 'POST'
assert recorder[0].url == self._get_url('explicit-project-id')
assert recorder[0]._extra_kwargs['timeout'] is None
body = {'message': messaging._MessagingService.encode_message(msg)}
assert json.loads(recorder[0].body.decode()) == body

Expand Down