def __init__(self, device_finder_loops): self.found = hp.ResettableFuture() self.device_finder_loops = device_finder_loops self.futures = {e: hp.ResettableFuture() for e in InfoPoints} self.last_touch = {} self.device_spec = Device.FieldSpec() self.by_target = ByTarget(self.device_spec) self.tasks_by_target = {} self.collections = Collections()
def setup(self, *args, **kwargs): super().setup(*args, **kwargs) self.point_futures = { e: hp.ResettableFuture(name=f"Device({self.serial})::setup[point_futures.{e.name}]") for e in InfoPoints } self.point_futures[None] = hp.ResettableFuture( name=f"Device::setup({self.serial})[point_futures.None]" ) self.refreshing = hp.ResettableFuture(name=f"Device({self.serial})::[refreshing]")
def Points(self, device, RF): expect = {e: RF() for e in InfoPoints} expect[None] = RF() assert device.point_futures == expect class IP(enum.Enum): ONE = Point("msg1", ["attr1", "attr2", "attr3"], 10,) TWO = Point("msg2", ["attr4"], None) THREE = Point("msg3", ["attr5", "attr6"], 300) device.point_futures = {e: hp.ResettableFuture() for e in IP} device.point_futures[None] = hp.ResettableFuture() with mock.patch("photons_control.device_finder.InfoPoints", IP): yield IP
def make_socket_protocol(self): fut = hp.ResettableFuture() fut.add_done_callback(hp.reporter) self.socket_futs.append(fut) self.socket_futs = [t for t in self.socket_futs if not t.done()] class SocketProtocol: def error_received(sp, exc): log.error( self.lc("Socket got an error", address=self.address, error=exc)) # errno 51 is network unreachable # Once the network is back, the socket will start working again if isinstance(exc, OSError) and exc.errno == 51: return onerror(fut, exc) def connection_made(sp, transport): log.debug(self.lc("Connected socket", address=self.address)) close_existing(fut) fut.set_result(transport) def connection_lost(sp, exc): log.debug( self.lc("Connected socket lost connection", address=self.address)) onerror(fut, exc) def eof_received(sp): log.error(self.lc("Socket got an eof", address=self.address)) onerror(fut, EOFError()) return fut, SocketProtocol
def __init__( self, serial, responders, protocol_register=None, port=None, use_sockets=False, delay_discovery=False, ): self.port = port self.serial = serial self.use_sockets = use_sockets if protocol_register is None: from photons_messages import protocol_register self.protocol_register = protocol_register self.responders = responders self.echo_responder = EchoResponder() self.service_responder = ServicesResponder( delay_discovery=delay_discovery) self.attrs = Attrs(self) self.attrs.online = False self.wait_for_reboot_fut = hp.ResettableFuture() self.wait_for_reboot_fut.set_result(True) self.reboots = [] self.services = [] self.pre_reboot = None self.write_tasks = [] self.time_rebooting = 1 self.setup()
def __init__(self, t): self.t = t self.loop = asyncio.get_event_loop() self.task = None self.patch = None self.trigger = hp.ResettableFuture() self.whens = [] self.called = [] self.called_times = []
async def _get_response(self, packet, timeout, waiter, limit=None): errf = hp.ResettableFuture() errf.add_done_callback(hp.silent_reporter) response = [] async def wait_for_responses(): async with (limit or NoLimit()): if hasattr(asyncio, "current_task"): current_task = asyncio.current_task() else: current_task = asyncio.Task.current_task() asyncio.get_event_loop().call_later(timeout, timeout_task, current_task, errf, packet.serial) try: for info in await waiter: response.append(info) finally: if hasattr(waiter, "finish"): await waiter.finish() f = hp.async_as_background(wait_for_responses(), silent=True) def process(res): if errf.done() and not errf.cancelled(): # Errf has an exception return elif res.cancelled(): errf.reset() errf.set_exception( TimedOut("Message was cancelled", serial=packet.serial)) return if not res.cancelled(): exc = res.exception() if exc: errf.set_exception(exc) return errf.set_result(True) f.add_done_callback(process) try: await errf finally: f.cancel() return response
def __init__(self, sender, final_future=None, *, forget_after=30, limit=30): self.sender = sender self.forget_after = forget_after self.limit = limit if isinstance(self.limit, int): self.limit = asyncio.Semaphore(self.limit) self.devices = {} self.last_seen = {} self.searched = hp.ResettableFuture(name="Finder::__init__[searched]") self.collections = Collections() self.final_future = hp.ChildOfFuture( final_future or self.sender.stop_fut, name="Finder::__init__[final_future]" )
def __init__(self, writer, original, packet, conn, serial, addr, target, expect_zero): self.writer = writer self.conn = conn self.addr = addr self.packet = packet self.serial = serial self.target = target self.original = original self.expect_zero = expect_zero self.clone = packet.clone() self.requests = [] self.conn_fut = hp.ResettableFuture() self.conn_fut.set_result(self.conn)
async def search_loop(self): refreshing = hp.ResettableFuture(name="DeviceFinderDaemon::search_loop[refreshing]") refresh_discovery_fltr = Filter.empty(refresh_discovery=True) async def add(streamer): if refreshing.done(): return refreshing.set_result(True) async for device in self.finder.find(refresh_discovery_fltr): await streamer.add_coroutine( device.refresh_information_loop( self.sender, self.time_between_queries, self.finder.collections ), context=device, ) async def ticks(): async with self.hp_tick(self.search_interval, final_future=self.final_future) as ticks: async for info in ticks: yield info catcher = partial(log_errors, "Something went wrong in a search") async with hp.ResultStreamer( self.final_future, name="DeviceFinderDaemon::search_loop[streamer]", error_catcher=catcher, ) as streamer: await streamer.add_generator(ticks(), context="finder_tick") streamer.no_more_work() async for result in streamer: if result.successful and result.context == "finder_tick": refreshing.reset() await streamer.add_coroutine(add(streamer))
def __init__(self, sender): self.sender = sender self.search_fut = hp.ResettableFuture() self.search_fut.set_result(None)
def add_listener(self): u = str(uuid.uuid4()) self.listeners[u] = hp.ResettableFuture() self.listeners[u].cancel() return u, self.listeners[u]
def setup(self, *args, **kwargs): super().setup(*args, **kwargs) self.point_futures = {e: hp.ResettableFuture() for e in InfoPoints} self.point_futures[None] = hp.ResettableFuture()
def sent(self): return hp.ResettableFuture()
def got_event(self): return hp.ResettableFuture()
describe "IO": it "has a packet filter", device: class IIO(IO): io_source = "iio" io = IIO(device) assert isinstance(io.packet_filter, Filter) assert io.final_future is None assert io.last_final_future is None describe "session": async it "manages a queue and consumer task for incoming messages", device, wrap_io, final_future, parent_ts: process = [] got = hp.ResettableFuture() class TheIO(IO): io_source = "theio" async def process_incoming(s, bts, give_reply, addr): process.append((bts, give_reply, addr)) got.reset() got.set_result(len(process)) bts1 = mock.Mock(name="bts1") give_reply1 = mock.Mock(name="give_reply1") addr1 = mock.Mock(name="addr1") bts2 = mock.Mock(name="bts2") give_reply2 = mock.Mock(name="give_reply2")
from delfick_project.errors_pytest import assertRaises from unittest import mock import asyncio import pytest import uuid import time @pytest.fixture() def loop(): return asyncio.get_event_loop() describe "ResettableFuture": async it "ensure_future returns the ResettableFuture as is": fut = hp.ResettableFuture() assert asyncio.ensure_future(fut) is fut describe "Reset": async it "can be reset": res = mock.Mock(name="res") res2 = mock.Mock(name="res2") fut = hp.ResettableFuture() fut.set_result(res) assert await fut is res assert fut.done() fut.reset() assert not fut.done()
def __init__(self): self.found = hp.ResettableFuture( name=f"SpecialReference({self.__class__.__name__}.found)") self.finding = hp.ResettableFuture( name=f"SpecialReference({self.__class__.__name__}.finding")
assert await waiter is True assert called == [(0.3, "0.3"), (1, "1"), (2, "2"), (5, "5")] async it "can cancel handles", FakeTime, MockedCallLater: with FakeTime() as t: async with MockedCallLater(t) as m: info = {"handle": None} def nxt(*args): if info["handle"]: info["handle"].cancel() info["handle"] = call_later(*args) waiter = hp.ResettableFuture() nxt(1, waiter.set_result, True) nxt(0.3, waiter.set_result, True) assert await waiter is True waiter.reset() assert time.time() == 0.3 await m.add(1) assert time.time() == 1.3 assert waiter.done() waiter.reset() nxt(2, waiter.set_result, True) await m.add(1.5) assert time.time() == 2.8
# coding: spec from photons_app import helpers as hp from delfick_project.errors_pytest import assertRaises import asyncio import pytest describe "ResettableFuture": async it "ensure_future returns the ResettableFuture as is": fut = hp.ResettableFuture() assert asyncio.ensure_future(fut) is fut async it "creates a future": fut = hp.ResettableFuture() assert isinstance(fut.fut, asyncio.Future) assert fut.name is None assert fut.fut.name == "ResettableFuture(None)::__init__[fut]" fut = hp.ResettableFuture(name="blah") assert fut.name == "blah" assert isinstance(fut.fut, asyncio.Future) assert fut.fut.name == "ResettableFuture(blah)::__init__[fut]" async it "gets callbacks from the current future": fut = hp.ResettableFuture() assert len(fut._callbacks) == 1 pytest.helpers.assertFutCallbacks(fut, hp.silent_reporter) assert fut._callbacks == fut.fut._callbacks
def __init__(self): self.found = hp.ResettableFuture() self.finding = hp.ResettableFuture()
result = mock.Mock(name="result") V.waiter.final_future.set_result(result) assert V.waiter.result() is result assert V.waiter.done() async it "can get exception from the final future", V: error = PhotonsAppError("error") V.waiter.final_future.set_exception(error) assert V.waiter.exception() is error assert V.waiter.done() describe "await": async it "starts a writings", V: called = [] called_fut = hp.ResettableFuture() def w(): called.append(1) called_fut.set_result(True) writings = pytest.helpers.AsyncMock(name="writings", side_effect=w) V.waiter.writings = writings async def doit(): return await V.waiter t = V.loop.create_task(doit()) await called_fut called_fut.reset() assert called == [1]