-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Liam Childs
committed
Sep 26, 2016
1 parent
dd96cdf
commit 39fad83
Showing
15 changed files
with
171 additions
and
227 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,73 @@ | ||
from .connection import Connection | ||
import asyncio | ||
|
||
|
||
class Input(Connection): | ||
def peek(self): | ||
return self.entities[0] | ||
class ConnectionClosed(Exception): | ||
pass | ||
|
||
def pop(self): | ||
return self.entities.popleft() | ||
|
||
def extend(self, entities): | ||
self.entities.extend(entities) | ||
return len(self.entities) < self.threshold | ||
class Input(asyncio.Queue): | ||
def __init__(self, name, maxsize=0, loop=None): | ||
super().__init__(maxsize, loop=loop) | ||
self.name = name | ||
self._closed = False | ||
|
||
async def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
try: | ||
return await self.pop() | ||
except ConnectionClosed: | ||
raise StopAsyncIteration | ||
|
||
async def push(self, item): | ||
if self._closed: | ||
raise ConnectionClosed | ||
return await self.put(item) | ||
|
||
async def peek(self): | ||
while self.empty(): | ||
if self._closed and len(self._putters) == 0: | ||
raise ConnectionClosed | ||
|
||
getter = self._loop.create_future() | ||
self._getters.append(getter) | ||
try: | ||
await getter | ||
except: | ||
getter.cancel() | ||
if not self.empty() and not getter.cancelled(): | ||
self._wakeup_next(self._getters) | ||
raise | ||
return self._queue[0] | ||
|
||
async def pop(self): | ||
while self.empty(): | ||
if self._closed and len(self._putters) == 0: | ||
raise ConnectionClosed | ||
|
||
getter = self._loop.create_future() | ||
self._getters.append(getter) | ||
try: | ||
await getter | ||
except: | ||
getter.cancel() | ||
if not self.empty() and not getter.cancelled(): | ||
self._wakeup_next(self._getters) | ||
raise | ||
return self.get_nowait() | ||
|
||
def close(self): | ||
self._closed = True | ||
self._maxsize = 0 | ||
while self._putters: | ||
putter = self._putters.popleft() | ||
if not putter.done(): | ||
putter.set_result(None) | ||
while self._getters: | ||
getter = self._getters.popleft() | ||
if not getter.done(): | ||
if self.empty(): | ||
getter.set_exception(ConnectionClosed) | ||
else: | ||
getter.set_result(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from .connection import Connection | ||
class Output: | ||
def __init__(self, name): | ||
self.name = name | ||
self._connections = [] | ||
|
||
async def push(self, entity): | ||
for connection in self._connections: | ||
await connection.push(entity) | ||
|
||
class Output(Connection): | ||
def push(self, entity): | ||
""" | ||
Add an entity to the end of the connection. Return if connection can still be pushed to. | ||
:param entity: | ||
:return: | ||
""" | ||
self.entities.append(entity) | ||
return len(self.entities) < self.threshold | ||
def pipe(self, connection): | ||
self._connections.append(connection) | ||
|
||
def close(self): | ||
for connection in self._connections: | ||
connection.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
import asyncio | ||
|
||
from .connection.input import Input | ||
from .connection.output import Output | ||
from .connection.connection_set import ConnectionSet | ||
|
||
|
||
class TestHelper: | ||
def __init__(self, stream): | ||
def __init__(self, stream, timeout=5): | ||
ins = [Input(in_) for in_ in stream.ins] | ||
outs = [Output(out) for out in stream.outs] | ||
self.sinks = [Input(out) for out in stream.outs] | ||
for out, sink in zip(outs, self.sinks): | ||
out.pipe(sink) | ||
self.ins = ConnectionSet(ins) | ||
self.outs = ConnectionSet(outs) | ||
self.stream = stream | ||
self._timeout = timeout | ||
self._loop = asyncio.get_event_loop() | ||
|
||
def run(self, ins): | ||
def run(self, ins={}): | ||
for key, value in ins.items(): | ||
self.ins[key].clear() | ||
self.ins[key].extend(value) | ||
outs = {out.name: [] for out in self.outs} | ||
while self.stream.run(self.ins, self.outs): | ||
for out in self.outs: | ||
outs[out.name].extend(out.entities) | ||
out.clear() | ||
return outs | ||
self.ins[key]._queue.extend(value) | ||
self.ins[key].close() | ||
task = self._loop.create_task(self.stream.run(self.ins, self.outs)) | ||
self._loop.run_until_complete(asyncio.wait_for(task, self._timeout, loop=self._loop)) | ||
return {sink.name: list(sink._queue) for sink in self.sinks} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
setup( | ||
name='mimo', | ||
version='1.0.6', | ||
version='1.0.7', | ||
author='Liam H. Childs', | ||
author_email='[email protected]', | ||
packages=find_packages(exclude=['tests']), | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.