def test_pdus(): for pdu_cls in PDU._registered_pdus.values(): assert (issubclass(pdu_cls, RequestPDU) and pdu_cls.command_id.value & 0x80000000 == 0) or \ (issubclass(pdu_cls, ResponsePDU) and pdu_cls.command_id.value & 0x80000000 != 0) or \ (pdu_cls in (OutbindPDU, AlertNotificationPDU)) assert tuple_of(str)(pdu_cls.mandatory_parameters) assert tuple_of(str)(pdu_cls.optional_parameters) if issubclass(pdu_cls, RequestPDU): assert pdu_cls.command_id.value | 0x80000000 in PDU._registered_pdus
def _start_interfaces(interfaces: tuple_of(str)): # prepare the global request factory shared across all interfaces # see begin_request method below global _request_factory _request_factory = InterlockedFactory(Request) pmnc.log.message("as of this moment requests can be created") # the configured interfaces are started one by one and kept on file, # technically they can be restarted (presumably with modified configuration) # at runtime by stopping and recreating, but I don't see it big an issue # to restart a cage once its interface settings have changed for interface_name in interfaces: pmnc.log.message("starting interface {0:s}".format(interface_name)) try: config_module_name = "config_interface_{0:s}".format(interface_name) config = pmnc.__getattr__(config_module_name).copy() interface = pmnc.interface.create(interface_name, **config) interface.start() except: pmnc.log.message("interface {0:s} failed to start: {1:s}".\ format(interface_name, exc_string())) # log and ignore else: pmnc.log.message("interface {0:s} has started".format(interface_name)) _interfaces[interface_name] = interface
def run(*, required_dirs: optional(tuple_of(str)) = ()): if getenv("PYTHONHASHSEED") != "0": raise SystemExit( "Running self-tests requires turning off hash randomization, " "set environment variable PYTHONHASHSEED to 0.") current_thread().name = "self_test" _log("***** STARTING SELF-TEST FOR MODULE {0:s} *****".format( main_module_name.upper())) test_cages_dir = _create_temp_cage_copy(required_dirs=required_dirs) try: pmnc = _start_pmnc(test_cages_dir) try: try: current_thread()._pmnc = pmnc # to be used in active_interface assert pmnc.request.self_test == main_module_name pmnc.__getattr__(main_module_name).self_test() except: _log("***** FAILURE: {1:s}".format(main_module_name, exc_string())) else: _log( "***** SUCCESS, BUT EXAMINE THE LOG FOR UNEXPECTED ERRORS *****" ) finally: _stop_pmnc(pmnc) finally: _remove_temp_cage_copy(test_cages_dir)
def __init__(self, name: str, *, java: os_path.isfile, arguments: tuple_of(str), classpath: str, jndi: dict_of(str, str), factory: str, queue: str, username: str, password: str, request_timeout: optional(float) = None, **kwargs): # this kwargs allows for extra application-specific # settings in config_interface_jms_X.py self._name = name AdapterHost.__init__(self, "org.pythomnic.jms.Receiver", java = java, arguments = arguments, classpath = classpath, jndi = jndi, factory = factory, queue = queue, username = username, password = password) self._request_timeout = request_timeout or \ pmnc.config_interfaces.get("request_timeout") # this is now static if pmnc.request.self_test == __name__: # self-test self._process_request = kwargs["process_request"]
def __init__(self, name: str, *, java: os_path.isfile, arguments: tuple_of(str), classpath: str, jndi: dict_of(str, str), factory: str, queue: str, username: str, password: str): TransactionalResource.__init__(self, name) AdapterHost.__init__(self, "org.pythomnic.jms.Sender", java = java, arguments = arguments, classpath = classpath, jndi = jndi, factory = factory, queue = queue, username = username, password = password) self._request_count = 0
def __init__(self, name: str, *, source_cages: tuple_of(valid_cage_name), request_timeout: optional(float) = None, **kwargs): self._name = name self._source_cages = source_cages self._request_timeout = request_timeout or \ pmnc.config_interfaces.get("request_timeout") # this is now static if pmnc.request.self_test != __name__: self._poll = lambda cage: pmnc(cage).reverse_call.poll() self._post = lambda cage, request_id, response: pmnc(cage).reverse_call.post(request_id, response) else: # self-test self._process_revrpc_request = kwargs["process_revrpc_request"] self._poll = kwargs["poll"] self._post = kwargs["post"]
def __init__(self, collection: str, query: dict, fields: optional(tuple_of(str)) = None, *, docs_to_skip: optional(int) = None, docs_to_return: optional(int) = None, tailable_cursor: optional(bool) = False, slave_ok: optional(bool) = False, no_cursor_timeout: optional(bool) = False, await_data: optional(bool) = False, exhaust: optional(bool) = False): MongoDB_Request.__init__(self) self._collection = collection self._query = query self._fields = fields self._docs_to_skip = docs_to_skip self._docs_to_return = docs_to_return self._tailable_cursor = tailable_cursor self._slave_ok = slave_ok self._no_cursor_timeout = no_cursor_timeout self._await_data = await_data self._exhaust = exhaust
def _update_interfaces(interface_names: tuple_of(str)): started_interfaces = set( _interfaces.keys()) # currently running interfaces for interface_name in interface_names: if interface_name not in started_interfaces: # start a newly mentioned interface interface = LoadedInterface(interface_name) interface.start() _interfaces[interface_name] = interface else: started_interfaces.remove( interface_name) # ignore the already running interface # stop the interfaces that are no longer mentioned for interface_name in started_interfaces: interface = _interfaces.pop(interface_name) interface.cease() interface.stop()
def __init__(self, name: str, *, source_cages: tuple_of(valid_cage_name), request_timeout: optional(float) = None, **kwargs): self._name = name self._source_cages = source_cages self._request_timeout = request_timeout or \ pmnc.config_interfaces.get("request_timeout") # this is now static if pmnc.request.self_test != __name__: self._poll = lambda cage: pmnc(cage).reverse_call.poll() self._post = lambda cage, request_id, response: pmnc( cage).reverse_call.post(request_id, response) else: # self-test self._process_revrpc_request = kwargs["process_revrpc_request"] self._poll = kwargs["poll"] self._post = kwargs["post"]
def run(*, required_dirs: optional(tuple_of(str)) = ()): current_thread().name = "self_test" _log("***** STARTING SELF-TEST FOR MODULE {0:s} *****".format(main_module_name.upper())) test_cages_dir = _create_temp_cage_copy(required_dirs = required_dirs) try: pmnc = _start_pmnc(test_cages_dir) try: try: current_thread()._pmnc = pmnc # to be used in active_interface assert pmnc.request.self_test == main_module_name pmnc.__getattr__(main_module_name).self_test() except: _log("***** FAILURE: {1:s}".format(main_module_name, exc_string())) else: _log("***** SUCCESS, BUT EXAMINE THE LOG FOR UNEXPECTED ERRORS *****") finally: _stop_pmnc(pmnc) finally: _remove_temp_cage_copy(test_cages_dir)
def __init__(self, class_name: str, *, java: os_path.isfile, arguments: tuple_of(str), classpath: str, jndi: dict_of(str, str), factory: str, queue: str, username: str, password: str): # random line prefix and suffix for packet serialization bol = b2a_hex(urandom(8)).decode("ascii").upper() eol = b2a_hex(urandom(8)).decode("ascii").upper() # compose the executable command line self._args = [ java ] + list(arguments) + \ [ "-classpath", classpath, class_name, "connection.factory={0:s}".format(factory), "connection.queue={0:s}".format(queue), "stdout.bol={0:s}".format(bol), "stdout.eol={0:s}".format(eol) ] if username or password: self._args.append("connection.username={0:s}".format(username)) self._args.append("connection.password={0:s}".format(password)) self._args.extend("jndi.{0:s}={1:s}".format(*t) for t in jndi.items()) self._bol_b = bol.encode("ascii") self._eol_b = eol.encode("ascii") # this set tracks messages that have been processed but # not committed on the server due to a failure self._processed_messages = set()
def foo(x: tc.tuple_of([tc.has("^[01]+$"), int])) -> bool: return functools.reduce(lambda r, e: r and int(e[0], 2) == e[1], x, True)
def foo(x: tc.tuple_of(int)) -> tc.tuple_of(float): return tuple(map(float, x))
with expected(InputParameterError("foo() has got an incompatible value for x: []")): foo([]) ################### @typecheck def foo(x: tc.tuple_of([tc.has("^[01]+$"), int])) -> bool: return functools.reduce(lambda r, e: r and int(e[0], 2) == e[1], x, True) assert foo((["1010", 10], ["0101", 5])) assert not foo((["1010", 10], ["0111", 77])) ################### assert tc.tuple_of(tc.optional(tc.has("^foo$")))(("foo", None, "foo")) and \ tc.tuple_of(tc.optional(tc.has("^foo$"))).check(("foo", None, "foo")) assert not tc.tuple_of(tc.optional(tc.has("^foo$")))(("123", None, "foo")) and \ not tc.tuple_of(tc.optional(tc.has("^foo$"))).check(("123", None, "foo")) print("ok") ############################################################################ print("list_of: ", end="") @typecheck def foo(x: tc.list_of(int)) -> tc.list_of(float): return list(map(float, x))