def test_registered_constructor_must_be_callable(): from moler.connection import ConnectionFactory with pytest.raises(ValueError) as err: ConnectionFactory.register_construction(io_type='memory', variant='superquick', constructor=[1, 2]) assert "constructor must be callable not" in str(err)
def test_missing_constructor_raises_KeyError(): from moler.connection import ConnectionFactory with pytest.raises(KeyError) as err: ConnectionFactory.get_connection(io_type='memory', variant='superquick') assert "No constructor registered for [('memory', 'superquick')] connection" in str( err)
def test_can_select_connection_variant_from_plugin_connections(builtin_connection_factories, connections_config): from moler.connection import ConnectionFactory, get_connection class DummyTcpConnection(object): def __init__(self, host, port): pass ConnectionFactory.register_construction(io_type='tcp', variant='dummy', constructor=DummyTcpConnection) connections_config.set_default_variant(io_type='tcp', variant='dummy') conn = get_connection(io_type='tcp', host='localhost', port=2345) assert conn.__class__.__name__ == 'DummyTcpConnection'
def test_can_plugin_alternative_connection_instead_of_builtin_one( builtin_connection_factories): from moler.connection import ConnectionFactory, get_connection from moler.connection import ObservableConnection class DummyTcpConnection(object): def __init__(self, host, port): self.moler_connection = ObservableConnection(how2send=self.send) def send(self, data): pass ConnectionFactory.register_construction(io_type='tcp', variant='threaded', constructor=DummyTcpConnection) conn = get_connection(io_type='tcp', variant='threaded', host='localhost', port=2345) assert conn.__class__.__name__ == 'DummyTcpConnection'
load_config(config=os.path.join(os.path.dirname(__file__), "connections_new_variant.yml")) # configure class used to realize tcp-threaded-connection # (default one tcp.ThreadedTcp has no logger) # This constitutes plugin system - you can exchange connection implementation def tcp_thd_conn(port, host='localhost', name=None): moler_conn = ObservableConnection(decoder=lambda data: data.decode("utf-8")) conn_logger_name = 'threaded.tcp-connection({}:{})'.format(host, port) conn_logger = logging.getLogger(conn_logger_name) io_conn = tcp.ThreadedTcp(moler_connection=moler_conn, port=port, host=host, logger=conn_logger) return io_conn ConnectionFactory.register_construction(io_type="tcp", variant="threaded-and-logged", constructor=tcp_thd_conn) # ------------------------------------------------------------------- logging.basicConfig( level=logging.DEBUG, format='%(asctime)s |%(name)-40s |%(message)s', datefmt='%H:%M:%S', stream=sys.stderr, ) connections2observe4ip = [(('localhost', 5671), '10.0.2.15'), (('localhost', 5672), '10.0.2.16')] main(connections2observe4ip) ''' LOG OUTPUT