Skip to content

Commit

Permalink
Fix regression from aiohttp upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
jbn committed Jun 5, 2018
1 parent 75500f1 commit af9adf0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -10,3 +10,5 @@ build/
brittle_wit.egg-info
setup_me.sh
.DS_Store
.tox/
dist
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -8,5 +8,5 @@ install:
- pip install -r dev_requirements.txt
- pip install .
script:
- coverage run --source=brittle_wit setup.py nosetests
- pytest --cov=brittle_wit
after_success: coveralls
4 changes: 3 additions & 1 deletion appveyor.yml
Expand Up @@ -3,13 +3,15 @@ environment:
matrix:
- PYTHON: "C:\\Python35"
- PYTHON: "C:\\Python35-x64"
- PYTHON: "C:\\Python36"
- PYTHON: "C:\\Python36-x64"

install:
- "%PYTHON%\\python.exe -m pip install -r dev_requirements.txt"
- "%PYTHON%\\python.exe -m pip install -r requirements.txt"
- "%PYTHON%\\python.exe -m pip install ."

test_script:
- "%PYTHON%\\python.exe setup.py nosetests"
- "py.test --cov=brittle_wit"

build: off
42 changes: 25 additions & 17 deletions brittle_wit/streaming/__init__.py
Expand Up @@ -5,6 +5,7 @@
import time

from aiohttp import web
from itertools import compress
from brittle_wit.rate_limit import RateLimit
from brittle_wit.helpers import LOGGER
from brittle_wit_core import TwitterError, BrittleWitError
Expand Down Expand Up @@ -64,24 +65,31 @@ async def handle(self, req):

return resp

def send(self, message):
removal_set = []

for caller_id, (resp, finished_flag) in self._clients.items():
try:
resp.write(message)
except Exception as e:
# This is a sloppy catch all for now. I'm not sure if
# asyncio guarantees a cancel to handle prior to a
# possible write. But, I do know it would be quite
# wrong for a write to one client to cause failures
# in other clients.
finished_flag.set()
removal_set.append(caller_id)

for k in removal_set:
del self._clients[k]
async def _send_to(self, message, caller_id, resp, finished_flag):
try:
await resp.write(message)
except Exception as e:
# This is a sloppy catch all for now. I'm not sure if
# asyncio guarantees a cancel to handle prior to a
# possible write. But, I do know it would be quite
# wrong for a write to one client to cause failures
# in other clients.
finished_flag.set()
return True
return False

async def send_to_all(self, message):
tasks = [self._send_to(message, caller_id, resp, finished_flag)
for caller_id, (resp, finished_flag) in self._clients.items()]

finished = await asyncio.gather(*tasks)

for client in compress(self._clients.keys(), finished):
del self._clients[client]

def send(self, message):
# XXX: Sloppy refactoring
asyncio.ensure_future(self.send_to_all(message))

class StreamProcessor:
"""
Expand Down
5 changes: 4 additions & 1 deletion dev_requirements.txt
@@ -1,3 +1,6 @@
coveralls
nose
requests
pytest
pytest-cov
pytest-mock
pytest-asyncio
4 changes: 2 additions & 2 deletions tests/unit_tests/executors/test_twitter_req_to_http_req.py
Expand Up @@ -2,7 +2,7 @@
import pytest
from brittle_wit_core import TwitterRequest, AppCredentials, ClientCredentials
from brittle_wit.executors import twitter_req_to_http_req
from test.helpers import *
from tests.helpers import *


app_cred = AppCredentials("app", "secret")
Expand All @@ -15,7 +15,7 @@


@pytest.mark.asyncio
async def test_twitter_req_to_http_req_get(capsys):
async def test_twitter_req_to_http_req_get():
tr = TwitterRequest("GET", "http://url.com/", "service", "family")
async with aiohttp.ClientSession() as session:
req = twitter_req_to_http_req(session, app_cred, client_cred, tr)
Expand Down
12 changes: 12 additions & 0 deletions tox.ini
@@ -0,0 +1,12 @@
[tox]
envlist = py35,py36

[testenv]
deps=
aiohttp
pytest
pytest-asyncio
pytest-mock
requests

commands=pytest

0 comments on commit af9adf0

Please sign in to comment.