Skip to content

Commit

Permalink
Merge pull request #8 from Overseas-Student-Living/connection_error_d…
Browse files Browse the repository at this point in the history
…iscards

ConnectionError discards client from pool
  • Loading branch information
iky authored Aug 29, 2018
2 parents 1cc0693 + cea2410 commit 533f885
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Release Notes
=============

Version 1.2.0
-------------

Released 2018-08-29

* Discards clients from the pool on ConnectionError

Version 1.1.0
-------------

Expand Down
7 changes: 6 additions & 1 deletion nameko_salesforce/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class MethodProxy(object):
Fetches and then invokes a method from a client that is checked out of
a pool. If the method raises a `SalesforceExpiredSession` the client is
discarded from the pool and the method is retried on a new client.
If `ConnectionError` is raised, the client will be discarded, but the
method not automatically retried.
`Salesforce` clients support querying directly with the client
and via a "resource" attribute, so the method may be on the client
Expand All @@ -53,7 +55,10 @@ def __call__(self, *args, **kwargs):
try:
method = self.get_method_ref(client)
return method(*args, **kwargs)
except simple_salesforce.SalesforceExpiredSession:
except (
simple_salesforce.SalesforceExpiredSession,
requests.exceptions.ConnectionError
):
self.pool.discard(client)
raise

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='nameko-salesforce',
version='1.1.0',
version='1.2.0',
description=(
'Nameko extension for easy communication with Salesforce '
'(Including Streaming API)'
Expand Down
34 changes: 34 additions & 0 deletions tests/api/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from eventlet.event import Event
from mock import patch
import pytest
import requests
import requests_mock
from simple_salesforce import SalesforceResourceNotFound

Expand Down Expand Up @@ -156,6 +157,39 @@ def test_bad_clients_are_discarded(client, mock_salesforce_server):
assert len(client.pool.free) == 1


@pytest.mark.usefixtures('fast_retry')
def test_bad_connections_are_discarded(client, mock_salesforce_server):

# first call is successful; second is ConnectionError
requests_data = {'LastName': 'Smith', 'Email': '[email protected]'}
response_data = {
'errors': [],
'id': '003e0000003GuNXAA0',
'success': True
}
mock_salesforce_server.post(
requests_mock.ANY,
[
{'json': response_data},
{'exc': requests.exceptions.ConnectionError}
]
)

assert client.Contact.create(requests_data) == response_data

assert len(client.pool.busy) == 0
assert len(client.pool.free) == 1
_first_client = list(client.pool.free)[0]

with pytest.raises(requests.exceptions.ConnectionError):
client.Contact.create(requests_data)

# first client is discarded from the pool
assert _first_client not in client.pool.free
assert len(client.pool.busy) == 0
assert len(client.pool.free) == 0


@pytest.mark.usefixtures('fast_retry')
def test_proxy_retries_on_session_expired(client, mock_salesforce_server):

Expand Down

0 comments on commit 533f885

Please sign in to comment.