Skip to content

Commit

Permalink
use a queue as buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
gawel committed Jan 9, 2014
1 parent 6fffa62 commit f9a424b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions panoramisk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ class Connection(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
self.closed = False
self.buf = ''
self.queue = Queue()

def data_received(self, data):
encoding = getattr(self, 'encoding', 'ascii')
data = self.buf + data.decode(encoding, 'ignore')
data = data.decode(encoding, 'ignore')
if not self.queue.empty():
data = self.queue.get_nowait() + data
lines = data.split(EOL+EOL)
self.buf = lines.pop(-1)
self.queue.put_nowait(lines.pop(-1))
for line in lines:
obj = Message.from_line(line, self.factory.callbacks)
if obj is None:
Expand Down Expand Up @@ -352,7 +354,7 @@ def send_action_via_http(self, action, **kwargs):
self.http.auth = auth
try:
resp = self.http.get(self.config['url'], params=action)
except Exception as e:
except Exception as e: # pragma: no cover
self.log.exception(e)
self.http = None
if not retries:
Expand Down
4 changes: 2 additions & 2 deletions panoramisk/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ def callback(event, manager):
manager = self.callFTU(use_http=True, url='http://host')
manager.register_event('Peer*', callback)
event = panoramisk.Message.from_line('Event: PeerStatus',
manager.callbacks)
manager.callbacks)
self.assertTrue(event.success)
self.assertEqual(event['Event'], 'PeerStatus')
self.assertIn('Event', event)
manager.dispatch(event, event.matches)
self.assertTrue(event is future.result())

event = panoramisk.Message.from_line('Event: NoPeerStatus',
manager.callbacks)
manager.callbacks)
self.assertTrue(event is None)


Expand Down

0 comments on commit f9a424b

Please sign in to comment.