Skip to content

Commit 328036c

Browse files
carsongeehiranya911
authored andcommitted
Added support for timeouts in messaging module (#132)
1 parent bf783c3 commit 328036c

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ dist/
99
*~
1010
scripts/cert.json
1111
scripts/apikey.txt
12+
htmlcov/
13+
.pytest_cache/

firebase_admin/messaging.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ def __init__(self, app):
748748
'GCLOUD_PROJECT environment variable.')
749749
self._fcm_url = _MessagingService.FCM_URL.format(project_id)
750750
self._client = _http_client.JsonHttpClient(credential=app.credential.get_credential())
751+
self._timeout = app.options.get('httpTimeout')
751752

752753
@classmethod
753754
def encode_message(cls, message):
@@ -760,7 +761,7 @@ def send(self, message, dry_run=False):
760761
if dry_run:
761762
data['validate_only'] = True
762763
try:
763-
resp = self._client.body('post', url=self._fcm_url, json=data)
764+
resp = self._client.body('post', url=self._fcm_url, json=data, timeout=self._timeout)
764765
except requests.exceptions.RequestException as error:
765766
if error.response is not None:
766767
self._handle_fcm_error(error)
@@ -791,7 +792,12 @@ def make_topic_management_request(self, tokens, topic, operation):
791792
url = '{0}/{1}'.format(_MessagingService.IID_URL, operation)
792793
try:
793794
resp = self._client.body(
794-
'post', url=url, json=data, headers=_MessagingService.IID_HEADERS)
795+
'post',
796+
url=url,
797+
json=data,
798+
headers=_MessagingService.IID_HEADERS,
799+
timeout=self._timeout
800+
)
795801
except requests.exceptions.RequestException as error:
796802
if error.response is not None:
797803
self._handle_iid_error(error)

tests/test_messaging.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,44 @@ def test_aps_alert(self):
750750
check_encoding(msg, expected)
751751

752752

753+
class TestTimeout(object):
754+
755+
@classmethod
756+
def setup_class(cls):
757+
cred = testutils.MockCredential()
758+
firebase_admin.initialize_app(cred, {'httpTimeout': 4, 'projectId': 'explicit-project-id'})
759+
760+
@classmethod
761+
def teardown_class(cls):
762+
testutils.cleanup_apps()
763+
764+
def setup(self):
765+
app = firebase_admin.get_app()
766+
self.fcm_service = messaging._get_messaging_service(app)
767+
self.recorder = []
768+
769+
def test_send(self):
770+
self.fcm_service._client.session.mount(
771+
'https://fcm.googleapis.com',
772+
testutils.MockAdapter(json.dumps({'name': 'message-id'}), 200, self.recorder))
773+
msg = messaging.Message(topic='foo')
774+
messaging.send(msg)
775+
assert len(self.recorder) == 1
776+
assert self.recorder[0]._extra_kwargs['timeout'] == 4
777+
778+
def test_topic_management_timeout(self):
779+
self.fcm_service._client.session.mount(
780+
'https://iid.googleapis.com',
781+
testutils.MockAdapter(
782+
json.dumps({'results': [{}, {'error': 'error_reason'}]}),
783+
200,
784+
self.recorder)
785+
)
786+
messaging.subscribe_to_topic(['1'], 'a')
787+
assert len(self.recorder) == 1
788+
assert self.recorder[0]._extra_kwargs['timeout'] == 4
789+
790+
753791
class TestSend(object):
754792

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

0 commit comments

Comments
 (0)