Skip to content

Commit

Permalink
[#2912] Monkey-patch HTTPretty to resolve slow tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
TkTech committed Mar 14, 2016
1 parent c6e24f0 commit aadf6e1
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions ckanext/datapusher/tests/test.py
@@ -1,5 +1,6 @@
import json
import httpretty
import httpretty.core
import nose
import sys
import datetime
Expand All @@ -19,6 +20,40 @@
from ckanext.datastore.tests.helpers import rebuild_all_dbs, set_url_type


class HTTPrettyFix(httpretty.core.fakesock.socket):
"""
Monkey-patches HTTPretty with a fix originally suggested in PR #161
from 2014 (still open).
Versions of httpretty < 0.8.10 use a bufsize of 16 *bytes*, and
an infinite timeout. This makes httpretty unbelievably slow, and because
the httpretty decorator monkey patches *all* requests (like solr),
the performance impact is massive.
While this is fixed in versions >= 0.8.10, newer versions of HTTPretty
break SOLR and other database wrappers (See #265).
"""
def __init__(self, *args, **kwargs):
super(HTTPrettyFix, self).__init__(*args, **kwargs)
self._bufsize = 4096

original_socket = self.truesock
self.truesock.settimeout(3)

# We also patch the "real" socket itself to prevent HTTPretty
# from changing it to infinite which it tries to do in real_sendall.
class SetTimeoutPatch(object):
def settimeout(self, *args, **kwargs):
pass

def __getattr__(self, attr):
return getattr(original_socket, attr)

self.truesock = SetTimeoutPatch()

httpretty.core.fakesock.socket = HTTPrettyFix


# avoid hanging tests https://github.com/gabrielfalcao/HTTPretty/issues/34
if sys.version_info < (2, 7, 0):
import socket
Expand Down Expand Up @@ -92,8 +127,11 @@ def test_providing_res_with_url_calls_datapusher_correctly(self):
package = model.Package.get('annakarenina')

tests.call_action_api(
self.app, 'datastore_create', apikey=self.sysadmin_user.apikey,
resource=dict(package_id=package.id, url='demo.ckan.org'))
self.app,
'datastore_create',
apikey=self.sysadmin_user.apikey,
resource=dict(package_id=package.id, url='demo.ckan.org')
)

assert len(package.resources) == 4, len(package.resources)
resource = package.resources[3]
Expand Down

0 comments on commit aadf6e1

Please sign in to comment.