Skip to content

Commit

Permalink
Fixed #85 - Add timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
ziima committed Oct 16, 2015
1 parent 1d2a9db commit 733b597
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 5 additions & 2 deletions gcm/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ class GCM(object):
BACKOFF_INITIAL_DELAY = 1000
MAX_BACKOFF_DELAY = 1024000

def __init__(self, api_key, url=GCM_URL, proxy=None):
def __init__(self, api_key, url=GCM_URL, proxy=None, timeout=None):
""" api_key : google api key
url: url of gcm service.
proxy: can be string "http://host:port" or dict {'https':'host:port'}
timeout: timeout for every HTTP request, see 'requests' documentation for possible values.
"""
self.api_key = api_key
self.url = url
Expand All @@ -149,6 +150,8 @@ def __init__(self, api_key, url=GCM_URL, proxy=None):
else:
self.proxy = proxy

self.timeout = timeout

def construct_payload(self, **kwargs):
"""
Construct the dictionary mapping of parameters.
Expand Down Expand Up @@ -189,7 +192,7 @@ def make_request(self, data, is_json=True):

response = requests.post(
self.url, data=data, headers=headers,
proxies=self.proxy
proxies=self.proxy, timeout=self.timeout,
)

# Successful response
Expand Down
18 changes: 17 additions & 1 deletion gcm/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from gcm import *
import json
from mock import MagicMock, patch
from mock import MagicMock, call, patch, sentinel
import time


Expand Down Expand Up @@ -251,6 +251,22 @@ def test_make_request_unicode(self, mock_request):
data['message']
)

@patch('requests.post')
def test_make_request_timeout(self, mock_request):
""" Test make_request uses timeout. """
mock_request.return_value.status_code = 200
mock_request.return_value.content = "OK"
gcm = GCM('123api', timeout=sentinel.timeout)
# Perform request
gcm.make_request(
{'message': 'test'}, is_json=True
)
mock_request.assert_called_once_with(
GCM_URL, data={'message': 'test'},
headers={'Authorization': 'key=123api', 'Content-Type': 'application/json'}, proxies=None,
timeout=sentinel.timeout,
)

def test_retry_plaintext_request_ok(self):
returns = [GCMUnavailableException(), GCMUnavailableException(), 'id=123456789']

Expand Down

0 comments on commit 733b597

Please sign in to comment.