def select(self, query, parameters): future = Future() request = (SELECT, (query, parameters), {}, future) self._in_queue.put(request) return future
def setUpClass(cls): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures. """ super(UplinkBridgeWithNonNATTest_IP_VLAN_GW, cls).setUpClass() warnings.simplefilter('ignore') cls.service_manager = create_service_manager([]) uplink_bridge_controller_reference = Future() testing_controller_reference = Future() test_setup = TestSetup( apps=[ PipelinedController.UplinkBridge, PipelinedController.Testing, PipelinedController.StartupFlows, ], references={ PipelinedController.UplinkBridge: uplink_bridge_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.StartupFlows: Future(), }, config={ 'bridge_name': cls.BRIDGE, 'bridge_ip_address': cls.BRIDGE_IP, 'ovs_gtp_port_number': 32768, 'clean_restart': True, 'enable_nat': False, 'uplink_bridge': cls.UPLINK_BRIDGE, 'uplink_eth_port_name': cls.UPLINK_ETH_PORT, 'virtual_mac': '02:bb:5e:36:06:4b', 'uplink_patch': cls.UPLINK_PATCH, 'uplink_dhcp_port': cls.UPLINK_DHCP, 'sgi_management_iface_vlan': cls.VLAN_TAG, 'sgi_management_iface_ip_addr': cls.SGi_IP, 'sgi_management_iface_gw': cls.SGi_GW, 'sgi_management_iface_ipv6_gw': cls.SGi_IPv6_GW, 'dev_vlan_in': "test_v_in", 'dev_vlan_out': "test_v_out", 'sgi_management_iface_ipv6_addr': 'fe80::48a3:2cff:aaaa:dd47/10', }, mconfig=None, loop=None, service_manager=cls.service_manager, integ_test=False, ) BridgeTools.create_bridge(cls.BRIDGE, cls.BRIDGE) # validate vlan id set vlan = "10" BridgeTools.create_bridge(cls.UPLINK_BRIDGE, cls.UPLINK_BRIDGE) subprocess.Popen([ "ovs-vsctl", "set", "port", cls.UPLINK_BRIDGE, "tag=" + vlan, ]).wait() assert get_ovsdb_port_tag(cls.UPLINK_BRIDGE) == vlan set_ip_cmd = [ "ip", "addr", "replace", "2.33.44.6", "dev", cls.UPLINK_BRIDGE, ] subprocess.check_call(set_ip_cmd) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_DHCP, None, ) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_PATCH, None, ) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_ETH_PORT, None, ) cls.thread = start_ryu_app_thread(test_setup) cls.uplink_br_controller = uplink_bridge_controller_reference.result() cls.testing_controller = testing_controller_reference.result()
def __call__(self, *args, **kwargs): # You can't call AsyncToSync from a thread with a running event loop try: event_loop = asyncio.get_event_loop() except RuntimeError: pass else: if event_loop.is_running(): raise RuntimeError( "You cannot use AsyncToSync in the same thread as an async event loop - " "just await the async function directly.") if contextvars is not None: # Wrapping context in list so it can be reassigned from within # `main_wrap`. context = [contextvars.copy_context()] else: context = None # Make a future for the return information call_result = Future() # Get the source thread source_thread = threading.current_thread() # Make a CurrentThreadExecutor we'll use to idle in this thread - we # need one for every sync frame, even if there's one above us in the # same thread. if hasattr(self.executors, "current"): old_current_executor = self.executors.current else: old_current_executor = None current_executor = CurrentThreadExecutor() self.executors.current = current_executor # Use call_soon_threadsafe to schedule a synchronous callback on the # main event loop's thread if it's there, otherwise make a new loop # in this thread. try: awaitable = self.main_wrap(args, kwargs, call_result, source_thread, sys.exc_info(), context) if not (self.main_event_loop and self.main_event_loop.is_running()): # Make our own event loop - in a new thread - and run inside that. loop = asyncio.new_event_loop() loop_executor = ThreadPoolExecutor(max_workers=1) loop_future = loop_executor.submit(self._run_event_loop, loop, awaitable) if current_executor: # Run the CurrentThreadExecutor until the future is done current_executor.run_until_future(loop_future) # Wait for future and/or allow for exception propagation loop_future.result() else: # Call it inside the existing loop self.main_event_loop.call_soon_threadsafe( self.main_event_loop.create_task, awaitable) if current_executor: # Run the CurrentThreadExecutor until the future is done current_executor.run_until_future(call_result) finally: # Clean up any executor we were running if hasattr(self.executors, "current"): del self.executors.current if old_current_executor: self.executors.current = old_current_executor if contextvars is not None: _restore_context(context[0]) # Wait for results from the future. return call_result.result()
def setUpNetworkAndController(self, vlan: str = "", non_nat_arp_egress_port: str = None, gw_mac_addr="ff:ff:ff:ff:ff:ff"): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures. """ global gw_info_map gw_info_map.clear() hub.sleep(2) cls = self.__class__ super(InOutNonNatTest, cls).setUpClass() inout.get_mobilityd_gw_info = mocked_get_mobilityd_gw_info inout.set_mobilityd_gw_info = mocked_set_mobilityd_gw_info warnings.simplefilter('ignore') cls.setup_uplink_br() if vlan != "": cls._setup_vlan_network(vlan) cls.service_manager = create_service_manager([]) inout_controller_reference = Future() testing_controller_reference = Future() if non_nat_arp_egress_port is None: non_nat_arp_egress_port = cls.DHCP_PORT patch_up_port_no = BridgeTools.get_ofport('patch-up') test_setup = TestSetup(apps=[ PipelinedController.InOut, PipelinedController.Testing, PipelinedController.StartupFlows ], references={ PipelinedController.InOut: inout_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.StartupFlows: Future(), }, config={ 'setup_type': 'LTE', 'bridge_name': cls.BRIDGE, 'bridge_ip_address': cls.BRIDGE_IP, 'ovs_gtp_port_number': 32768, 'clean_restart': True, 'enable_nat': False, 'non_nat_gw_probe_frequency': 0.5, 'non_nat_arp_egress_port': non_nat_arp_egress_port, 'uplink_port': patch_up_port_no, 'uplink_gw_mac': gw_mac_addr, }, mconfig=None, loop=None, service_manager=cls.service_manager, integ_test=False) BridgeTools.create_bridge(cls.BRIDGE, cls.IFACE) subprocess.Popen(["ifconfig", cls.UPLINK_BR, "192.168.128.41"]).wait() cls.thread = start_ryu_app_thread(test_setup) cls.inout_controller = inout_controller_reference.result() cls.testing_controller = testing_controller_reference.result()
def get(self, url): future = Future() url = QUrl(url) request = QNetworkRequest(url) request.setRawHeader(b"User-Agent", b"OWImageViewer/1.0") request.setAttribute( QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache ) if QT_VERSION >= 0x50600: request.setAttribute( QNetworkRequest.FollowRedirectsAttribute, True ) request.setMaximumRedirectsAllowed(5) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() n_redir = 0 def on_reply_ready(reply, future): # type: (QNetworkReply, Future) -> None nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if _log.level <= logging.DEBUG: s = io.StringIO() print("\n", reply.url(), file=s) if reply.attribute(QNetworkRequest.SourceIsFromCacheAttribute): print(" (served from cache)", file=s) for name, val in reply.rawHeaderPairs(): print(bytes(name).decode("latin-1"), ":", bytes(val).decode("latin-1"), file=s) _log.debug(s.getvalue()) if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future
def subscribe(self, topic, qos, callback=None): """Subscribe to a topic filter (async). The client sends a SUBSCRIBE packet and the server responds with a SUBACK. subscribe() may be called while the device is offline, though the async operation cannot complete successfully until the connection resumes. Once subscribed, `callback` is invoked each time a message matching the `topic` is received. It is possible for such messages to arrive before the SUBACK is received. Args: topic (str): Subscribe to this topic filter, which may include wildcards. qos (QoS): Maximum requested QoS that server may use when sending messages to the client. The server may grant a lower QoS in the SUBACK (see returned Future) callback: Optional callback invoked when message received. Function should take the following arguments and return nothing: * `topic` (str): Topic receiving message. * `payload` (bytes): Payload of message. * `**kwargs` (dict): Forward-compatibility kwargs. Returns: Tuple[concurrent.futures.Future, int]: Tuple containing a Future and the ID of the SUBSCRIBE packet. The Future completes when a SUBACK is received from the server. If successful, the Future will contain a dict with the following members: * ['packet_id'] (int): ID of the SUBSCRIBE packet being acknowledged. * ['topic'] (str): Topic filter of the SUBSCRIBE packet being acknowledged. * ['qos'] (:class:`QoS`): Maximum QoS that was granted by the server. This may be lower than the requested QoS. If unsuccessful, the Future contains an exception. The exception will be a :class:`SubscribeError` if a SUBACK was received in which the server rejected the subscription. Other exception types indicate other errors with the operation. """ future = Future() packet_id = 0 if callback: def callback_wrapper(topic, payload): callback(topic=topic, payload=payload) else: callback_wrapper = None def suback(packet_id, topic, qos, error_code): if error_code: future.set_exception(awscrt.exceptions.from_code(error_code)) else: qos = _try_qos(qos) if qos is None: future.set_exception(SubscribeError(topic)) else: future.set_result( dict( packet_id=packet_id, topic=topic, qos=qos, )) try: assert callable(callback) or callback is None assert isinstance(qos, QoS) packet_id = _awscrt.mqtt_client_connection_subscribe( self._binding, topic, qos.value, callback_wrapper, suback) except Exception as e: future.set_exception(e) return future, packet_id
def __init__(self): self.future = Future()
def get(self, url): future = Future() url = QUrl(url) request = QNetworkRequest(url) request.setRawHeader(b"User-Agent", b"OWImageViewer/1.0") request.setAttribute( QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache ) # Future yielding a QNetworkReply when finished. reply = self._netmanager.get(request) future._reply = reply @future.add_done_callback def abort_on_cancel(f): # abort the network request on future.cancel() if f.cancelled() and f._reply is not None: f._reply.abort() n_redir = 0 def on_reply_ready(reply, future): nonlocal n_redir # schedule deferred delete to ensure the reply is closed # otherwise we will leak file/socket descriptors reply.deleteLater() future._reply = None if reply.error() == QNetworkReply.OperationCanceledError: # The network request was cancelled reply.close() future.cancel() return if reply.error() != QNetworkReply.NoError: # XXX Maybe convert the error into standard # http and urllib exceptions. future.set_exception(Exception(reply.errorString())) reply.close() return # Handle a possible redirection location = reply.attribute( QNetworkRequest.RedirectionTargetAttribute) if location is not None and n_redir < 1: n_redir += 1 location = reply.url().resolved(location) # Retry the original request with a new url. request = QNetworkRequest(reply.request()) request.setUrl(location) newreply = self._netmanager.get(request) future._reply = newreply newreply.finished.connect( partial(on_reply_ready, newreply, future)) reply.close() return reader = QImageReader(reply) image = reader.read() reply.close() if image.isNull(): future.set_exception(Exception(reader.errorString())) else: future.set_result(image) reply.finished.connect(partial(on_reply_ready, reply, future)) return future
def setUpClass(cls): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures. """ super(UplinkBridgeWithNonNatUplinkConnect_Static_IP_Test, cls).setUpClass() warnings.simplefilter('ignore') cls.service_manager = create_service_manager([]) cls._setup_vlan_network("0") BridgeTools.create_bridge(cls.UPLINK_BRIDGE, cls.UPLINK_BRIDGE) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_DHCP, None, ) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_PATCH, None, ) check_connectivity(cls.ROUTER_IP, cls.UPLINK_ETH_PORT) BridgeTools.add_ovs_port(cls.UPLINK_BRIDGE, cls.UPLINK_ETH_PORT, "200") print("create uplink br: done") # this is setup after AGW boot up in NATed mode. uplink_bridge_controller_reference = Future() testing_controller_reference = Future() test_setup = TestSetup( apps=[ PipelinedController.UplinkBridge, PipelinedController.Testing, PipelinedController.StartupFlows, ], references={ PipelinedController.UplinkBridge: uplink_bridge_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.StartupFlows: Future(), }, config={ 'bridge_name': cls.BRIDGE, 'bridge_ip_address': cls.BRIDGE_IP, 'ovs_gtp_port_number': 32768, 'clean_restart': True, 'enable_nat': False, 'uplink_bridge': cls.UPLINK_BRIDGE, 'uplink_eth_port_name': cls.UPLINK_ETH_PORT, 'virtual_mac': '02:bb:5e:36:06:4b', 'uplink_patch': cls.UPLINK_PATCH, 'uplink_dhcp_port': cls.UPLINK_DHCP, 'sgi_management_iface_vlan': "", 'ovs_vlan_workaround': True, 'dev_vlan_in': "testv1_in", 'dev_vlan_out': "testv1_out", 'sgi_management_iface_ip_addr': '10.55.0.41/24', 'sgi_management_iface_ipv6_addr': 'fc00::55:0:111/96', }, mconfig=None, loop=None, service_manager=cls.service_manager, integ_test=False, ) BridgeTools.create_bridge(cls.BRIDGE, cls.BRIDGE) cls.thread = start_ryu_app_thread(test_setup) cls.uplink_br_controller = uplink_bridge_controller_reference.result() cls.testing_controller = testing_controller_reference.result()
def __call__(self, spans): # print('ManualSender called', request) self.requests.append(spans) fut = Future() self.futures.append(fut) return fut
def launch_if_ready(self, task_id): """ launch_if_ready will launch the specified task, if it is ready to run (for example, without dependencies, and in pending state). This should be called by any piece of the DataFlowKernel that thinks a task may have become ready to run. It is not an error to call launch_if_ready on a task that is not ready to run - launch_if_ready will not incorrectly launch that task. launch_if_ready is thread safe, so may be called from any thread or callback. """ # after launching the task, self.tasks[task_id] is no longer # guaranteed to exist (because it can complete fast as part of the # submission - eg memoization) task_record = self.tasks.get(task_id) if task_record is None: # assume this task has already been processed to completion logger.debug( "Task {} has no task record. Assuming it has already been processed to completion." .format(task_id)) return if self._count_deps(task_record['depends']) == 0: # We can now launch *task* new_args, kwargs, exceptions = self.sanitize_and_wrap( task_id, task_record['args'], task_record['kwargs']) task_record['args'] = new_args task_record['kwargs'] = kwargs if not exceptions: # There are no dependency errors exec_fu = None # Acquire a lock, retest the state, launch with task_record['task_launch_lock']: if task_record['status'] == States.pending: try: exec_fu = self.launch_task(task_id, task_record['func'], *new_args, **kwargs) except Exception as e: # task launched failed somehow. the execution might # have been launched and an exception raised after # that, though. that's hard to detect from here. # we don't attempt retries here. This is an error with submission # even though it might come from user code such as a plugged-in # executor or memoization hash function. logger.debug("Got an exception launching task", exc_info=True) exec_fu = Future() exec_fu.set_exception(e) else: logger.info( "Task {} failed due to dependency failure".format(task_id)) # Raise a dependency exception task_record['status'] = States.dep_fail self.tasks_dep_fail_count += 1 self._send_task_log_info(task_record) exec_fu = Future() exec_fu.set_exception(DependencyError(exceptions, task_id)) if exec_fu: try: exec_fu.add_done_callback( partial(self.handle_exec_update, task_id)) except Exception as e: # this exception is ignored here because it is assumed that exception # comes from directly executing handle_exec_update (because exec_fu is # done already). If the callback executes later, then any exception # coming out of the callback will be ignored and not propate anywhere, # so this block attempts to keep the same behaviour here. logger.error( "add_done_callback got an exception {} which will be ignored" .format(e)) task_record['exec_fu'] = exec_fu
def setUpClass(cls): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures, mocks the redis policy_dictionary of dpi_controller """ super(InternalPktIpfixExportTest, cls).setUpClass() warnings.simplefilter('ignore') cls._static_rule_dict = {} cls.service_manager = create_service_manager( [PipelineD.DPI], ['ue_mac', 'ipfix'], ) cls._tbl_num = cls.service_manager.get_table_num( DPIController.APP_NAME, ) ue_mac_controller_reference = Future() dpi_controller_reference = Future() ipfix_controller_reference = Future() testing_controller_reference = Future() test_setup = TestSetup( apps=[ PipelinedController.UEMac, PipelinedController.DPI, PipelinedController.IPFIX, PipelinedController.Testing, PipelinedController.StartupFlows, ], references={ PipelinedController.UEMac: ue_mac_controller_reference, PipelinedController.DPI: dpi_controller_reference, PipelinedController.Arp: Future(), PipelinedController.IPFIX: ipfix_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.StartupFlows: Future(), }, config={ 'bridge_name': cls.BRIDGE, 'bridge_ip_address': '192.168.128.1', 'internal_ip_subnet': '192.168.0.0/16', 'nat_iface': 'eth2', 'enodeb_iface': 'eth1', 'enable_queue_pgm': False, 'clean_restart': True, 'setup_type': 'CWF', 'dpi': { 'enabled': True, 'mon_port': 'mon1', 'mon_port_number': 32769, 'idle_timeout': 42, }, 'ipfix': { 'enabled': True, 'probability': 65, 'collector_set_id': 1, 'collector_ip': '1.1.1.1', 'collector_port': 65010, 'cache_timeout': 60, 'obs_domain_id': 1, 'obs_point_id': 1, }, 'conntrackd': { 'enabled': True, }, 'ovs_gtp_port_number': 32768, }, mconfig=PipelineD(), loop=None, service_manager=cls.service_manager, integ_test=False, ) BridgeTools.create_bridge(cls.BRIDGE, cls.IFACE) BridgeTools.create_internal_iface( cls.BRIDGE, cls.DPI_PORT, cls.DPI_IP, ) cls.thread = start_ryu_app_thread(test_setup) cls.ue_mac_controller = ue_mac_controller_reference.result() cls.dpi_controller = dpi_controller_reference.result() cls.ipfix_controller = ipfix_controller_reference.result() cls.testing_controller = testing_controller_reference.result() cls.dpi_controller._policy_dict = cls._static_rule_dict
def stop(self): future = Future() request = (POISON, None, None, future) self._in_queue.put(request) return future
def commit(self): future = Future() request = (COMMIT, None, None, future) self._in_queue.put(request) return future
def __init__(self, response): self._fut = Future() self._response = response response.add_done_callback(self._on_response_done) self._asyncio_task = None self._canceled = False
def __init__(self, connection, on_body=None): super(HttpStreamBase, self).__init__() self._connection = connection self._completion_future = Future() self._on_body_cb = on_body
def submit(self, coro): response = Future() fut = AsyncioExecutorFuture(response) self._request.put_nowait((coro, response)) self._notify.sendall(b'1') return fut
def __init__(self): super(HttpConnectionBase, self).__init__() self.shutdown_future = Future()
def Enrichment(self): assert self.input_data is not None assert self.__state == State.Ready if not self.annotations.ontology: self.annotations.ontology = self.ontology self.error(1) self.warning([0, 1]) self.__get_input_genes() self.input_genes = set(self.input_genes) self.known_input_genes = self.annotations.get_genes_with_known_annotation(self.input_genes) # self.clusterGenes = clusterGenes = self.annotations.map_to_ncbi_id(self.input_genes).values() self.infoLabel.setText("%i unique genes on input\n%i (%.1f%%) genes with known annotations" % (len(self.input_genes), len(self.known_input_genes), 100.0*len(self.known_input_genes)/len(self.input_genes) if len(self.input_genes) else 0.0)) if not self.useReferenceDataset or self.ref_data is None: self.information(2) self.information(1) self.ref_genes = self.annotations.genes() self.ref_genes = set(self.ref_genes) elif self.ref_data is not None: self.__get_ref_genes() self.ref_genes = set(self.ref_genes) ref_count = len(self.ref_genes) if ref_count == 0: self.ref_genes = self.annotations.genes() self.referenceRadioBox.buttons[1].setText("Reference set") self.referenceRadioBox.buttons[1].setDisabled(True) self.information(2, "Unable to extract gene names from reference dataset. " "Using entire genome for reference") self.useReferenceDataset = 0 else: self.referenceRadioBox.buttons[1].setText("Reference set ({} genes)".format(ref_count)) self.referenceRadioBox.buttons[1].setDisabled(False) self.information(2) else: self.useReferenceDataset = 0 self.ref_genes = [] if not self.ref_genes: self.error(1, "No valid reference set") return {} evidences = [] for etype in go.evidenceTypesOrdered: if self.useEvidenceType[etype]: evidences.append(etype) aspect = ['Process', 'Component', 'Function'][self.aspectIndex] self.progressBarInit(processEvents=False) self.setBlocking(True) self.__state = State.Running if self.input_genes: f = self._executor.submit( self.annotations.get_enriched_terms, self.input_genes, self.ref_genes, evidences, aspect=aspect, prob=self.probFunctions[self.probFunc], use_fdr=False, progress_callback=methodinvoke( self, "_progressBarSet", (float,)) ) fw = FutureWatcher(f, parent=self) fw.done.connect(self.__on_enrichment_done) fw.done.connect(fw.deleteLater) return else: f = Future() f.set_result({}) self.__on_enrichment_done(f)
def new(cls, host_name, port, bootstrap, socket_options=None, tls_connection_options=None, proxy_options=None): """ Initiates a new connection to host_name and port using socket_options and tls_connection_options if supplied. if tls_connection_options is None, then the connection will be attempted over plain-text. Returns a future where the result is a new instance to HttpClientConnection, once the connection has completed and is ready for use. """ assert isinstance(bootstrap, ClientBootstrap) or bootstrap is None assert isinstance_str(host_name) assert isinstance(port, int) assert isinstance( tls_connection_options, TlsConnectionOptions) or tls_connection_options is None assert isinstance(socket_options, SocketOptions) or socket_options is None assert isinstance(proxy_options, HttpProxyOptions) or proxy_options is None future = Future() try: if not socket_options: socket_options = SocketOptions() if not bootstrap: event_loop_group = EventLoopGroup(1) host_resolver = DefaultHostResolver(event_loop_group) bootstrap = ClientBootstrap(event_loop_group, host_resolver) connection = cls() connection._host_name = host_name connection._port = port def on_connection_setup(binding, error_code): if error_code == 0: connection._binding = binding future.set_result(connection) else: future.set_exception( awscrt.exceptions.from_code(error_code)) # on_shutdown MUST NOT reference the connection itself, just the shutdown_future within it. # Otherwise we create a circular reference that prevents the connection from getting GC'd. shutdown_future = connection.shutdown_future def on_shutdown(error_code): if error_code: shutdown_future.set_exception( awscrt.exceptions.from_code(error_code)) else: shutdown_future.set_result(None) _awscrt.http_client_connection_new(bootstrap, on_connection_setup, on_shutdown, host_name, port, socket_options, tls_connection_options, proxy_options) except Exception as e: future.set_exception(e) return future
def reset(self): self.future = Future()
def genereate_request(url): future = Future() t = threading.Thread(target=(lambda: future.set_result(requests.get(url)))) t.start() return future
def setupScene(self): self.error() if self.data: attr = self.stringAttrs[self.imageAttr] titleAttr = self.allAttrs[self.titleAttr] assert self.thumbnailView.count() == 0 size = QSizeF(self.imageSize, self.imageSize) for i, inst in enumerate(self.data): if not numpy.isfinite(inst[attr]): # skip missing continue url = self.urlFromValue(inst[attr]) title = str(inst[titleAttr]) thumbnail = GraphicsThumbnailWidget(QPixmap(), title=title) thumbnail.setThumbnailSize(size) thumbnail.setToolTip(url.toString()) thumbnail.instance = inst self.thumbnailView.addThumbnail(thumbnail) if url.isValid() and url.isLocalFile(): reader = QImageReader(url.toLocalFile()) image = reader.read() if image.isNull(): error = reader.errorString() thumbnail.setToolTip( thumbnail.toolTip() + "\n" + error) self._errcount += 1 else: pixmap = QPixmap.fromImage(image) thumbnail.setPixmap(pixmap) self._successcount += 1 future = Future() future.set_result(image) future._reply = None elif url.isValid(): future = self.loader.get(url) @future.add_done_callback def set_pixmap(future, thumb=thumbnail): if future.cancelled(): return assert future.done() if future.exception(): # Should be some generic error image. pixmap = QPixmap() thumb.setToolTip(thumb.toolTip() + "\n" + str(future.exception())) else: pixmap = QPixmap.fromImage(future.result()) thumb.setPixmap(pixmap) self._noteCompleted(future) else: future = None self.items.append(_ImageItem(i, thumbnail, url, future)) if any(it.future is not None and not it.future.done() for it in self.items): self.info.setText("Retrieving...\n") else: self._updateStatus()
def test_is_thenable_future(): promise = Future() assert is_thenable(promise)
def setUpClass(cls): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures. """ super(UplinkBridgeWithNonNATTestVlan, cls).setUpClass() warnings.simplefilter('ignore') cls.service_manager = create_service_manager([]) uplink_bridge_controller_reference = Future() testing_controller_reference = Future() test_setup = TestSetup( apps=[ PipelinedController.UplinkBridge, PipelinedController.Testing, PipelinedController.StartupFlows, ], references={ PipelinedController.UplinkBridge: uplink_bridge_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.StartupFlows: Future(), }, config={ 'bridge_name': cls.BRIDGE, 'bridge_ip_address': cls.BRIDGE_IP, 'ovs_gtp_port_number': 32768, 'clean_restart': True, 'enable_nat': False, 'uplink_bridge': cls.UPLINK_BRIDGE, 'uplink_eth_port_name': cls.UPLINK_ETH_PORT, 'virtual_mac': '02:bb:5e:36:06:4b', 'uplink_patch': cls.UPLINK_PATCH, 'uplink_dhcp_port': cls.UPLINK_DHCP, 'sgi_management_iface_vlan': cls.VLAN_TAG, 'dev_vlan_in': cls.VLAN_DEV_IN, 'dev_vlan_out': cls.VLAN_DEV_OUT, 'sgi_management_iface_ip_addr': '1.1.11.1', 'sgi_management_iface_ipv6_addr': 'fe80::48a3:2cff:fe1a:dd47/10', }, mconfig=None, loop=None, service_manager=cls.service_manager, integ_test=False, ) BridgeTools.create_bridge(cls.BRIDGE, cls.BRIDGE) BridgeTools.create_bridge(cls.UPLINK_BRIDGE, cls.UPLINK_BRIDGE) BridgeTools.create_veth_pair( cls.VLAN_DEV_IN, cls.VLAN_DEV_OUT, ) # Add to OVS, BridgeTools.add_ovs_port( cls.UPLINK_BRIDGE, cls.VLAN_DEV_IN, "70", ) BridgeTools.add_ovs_port( cls.UPLINK_BRIDGE, cls.VLAN_DEV_OUT, "71", ) BridgeTools.create_bridge(cls.UPLINK_BRIDGE, cls.UPLINK_BRIDGE) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_DHCP, None, ) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_PATCH, None, ) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_ETH_PORT, None, ) cls.thread = start_ryu_app_thread(test_setup) cls.uplink_br_controller = uplink_bridge_controller_reference.result() cls.testing_controller = testing_controller_reference.result()
def wrapper(*args, **kwargs): future = Future() Thread(target=call_with_future, args=(fn, future, args, kwargs)).start() return future
def setUpClass(cls): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures. """ super(UplinkBridgeWithNonNatUplinkConnect_Test, cls).setUpClass() warnings.simplefilter('ignore') cls.service_manager = create_service_manager([]) BridgeTools.create_bridge(cls.UPLINK_BRIDGE, cls.UPLINK_BRIDGE) br_mac = BridgeTools.get_mac_address(cls.UPLINK_BRIDGE) cls._setup_vlan_network("0", br_mac) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_DHCP, None, ) BridgeTools.create_internal_iface( cls.UPLINK_BRIDGE, cls.UPLINK_PATCH, None, ) flush_ip_cmd = [ "ip", "addr", "flush", "dev", cls.UPLINK_BRIDGE, ] subprocess.check_call(flush_ip_cmd) set_ip_cmd = [ "ip", "addr", "replace", "fe80::b0a6:34ff:fee0:b640", "dev", cls.UPLINK_BRIDGE, ] subprocess.check_call(set_ip_cmd) check_connectivity(cls.ROUTER_IP, cls.UPLINK_ETH_PORT) BridgeTools.add_ovs_port(cls.UPLINK_BRIDGE, cls.UPLINK_ETH_PORT, "200") # this is setup after AGW boot up in NATed mode. uplink_bridge_controller_reference = Future() testing_controller_reference = Future() test_setup = TestSetup( apps=[ PipelinedController.UplinkBridge, PipelinedController.Testing, PipelinedController.StartupFlows, ], references={ PipelinedController.UplinkBridge: uplink_bridge_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.StartupFlows: Future(), }, config={ 'bridge_name': cls.BRIDGE, 'bridge_ip_address': cls.BRIDGE_IP, 'ovs_gtp_port_number': 32768, 'clean_restart': True, 'enable_nat': False, 'uplink_bridge': cls.UPLINK_BRIDGE, 'uplink_eth_port_name': cls.UPLINK_ETH_PORT, 'virtual_mac': '02:bb:5e:36:06:4b', 'uplink_patch': cls.UPLINK_PATCH, 'uplink_dhcp_port': cls.UPLINK_DHCP, 'sgi_management_iface_vlan': "", 'ovs_vlan_workaround': True, 'dev_vlan_in': "testv1_in", 'dev_vlan_out': "testv1_out", 'sgi_ip_monitoring': False, }, mconfig=None, loop=None, service_manager=cls.service_manager, integ_test=False, ) BridgeTools.create_bridge(cls.BRIDGE, cls.BRIDGE) cls.thread = start_ryu_app_thread(test_setup) cls.uplink_br_controller = uplink_bridge_controller_reference.result() cls.testing_controller = testing_controller_reference.result()
def setUp(self): """ Starts the thread which launches ryu apps Create a testing bridge, add a port, setup the port interfaces. Then launch the ryu apps for testing pipelined. Gets the references to apps launched by using futures. Mocks the redis policy_dictionary of enforcement_controller. Mocks the loop for testing EnforcementStatsController """ super(EnforcementStatsTest, self).setUpClass() warnings.simplefilter('ignore') self._static_rule_dict = {} self.service_manager = create_service_manager([PipelineD.ENFORCEMENT]) self._main_tbl_num = self.service_manager.get_table_num( EnforcementController.APP_NAME) enforcement_controller_reference = Future() testing_controller_reference = Future() enf_stat_ref = Future() """ Enforcement_stats reports data by using loop.call_soon_threadsafe, but as we don't have an eventloop in testing, just directly call the stats handling function Here is how the mocked function is used in EnforcementStatsController: self.loop.call_soon_threadsafe(self._handle_flow_stats, ev.msg.body) """ def mock_thread_safe(cmd, body): cmd(body) loop_mock = MagicMock() loop_mock.call_soon_threadsafe = mock_thread_safe test_setup = TestSetup( apps=[PipelinedController.Enforcement, PipelinedController.Enforcement_stats, PipelinedController.Testing, PipelinedController.StartupFlows], references={ PipelinedController.Enforcement: enforcement_controller_reference, PipelinedController.Testing: testing_controller_reference, PipelinedController.Enforcement_stats: enf_stat_ref, PipelinedController.StartupFlows: Future(), }, config={ 'bridge_name': self.BRIDGE, 'bridge_ip_address': '192.168.128.1', 'enforcement': {'poll_interval': 5}, 'nat_iface': 'eth2', 'enodeb_iface': 'eth1', 'enable_queue_pgm': False, 'clean_restart': True, }, mconfig=PipelineD( relay_enabled=True, ), loop=loop_mock, service_manager=self.service_manager, integ_test=False, rpc_stubs={'sessiond': MagicMock()} ) BridgeTools.create_bridge(self.BRIDGE, self.IFACE) self.thread = start_ryu_app_thread(test_setup) self.enforcement_stats_controller = enf_stat_ref.result() self._scratch_tbl_num = self.enforcement_stats_controller.tbl_num self.enforcement_controller = enforcement_controller_reference.result() self.testing_controller = testing_controller_reference.result() self.enforcement_stats_controller._policy_dict = self._static_rule_dict self.enforcement_stats_controller._report_usage = MagicMock() self.enforcement_controller._policy_dict = self._static_rule_dict self.enforcement_controller._redirect_manager._save_redirect_entry = \ MagicMock()
def setLeverage(self, symbol, leverage): fut = Future() rep = fut.result() return rep
def query(self, query, parameters): future = Future() request = (QUERY, (query, parameters), {}, future) self._in_queue.put(request) return future