示例#1
0
    def test_context_options(self):

        protocol_options = {
            'resend_time': nng.Protocols.REQ0,
            'survey_time': nng.Protocols.SURVEYOR0,
        }

        with nng.Socket(nng.Protocols.REP0) as socket:
            with nng.Context(socket) as context:
                for name, prop in self.get_properties(context):
                    with self.subTest(name):
                        if name in protocol_options:
                            self.assert_no_prop(context, name, prop)
                        else:
                            self.assert_prop(context, name, prop)

        for name, protocol in protocol_options.items():
            with self.subTest((name, protocol)):
                with nng.Socket(protocol) as socket:
                    with nng.Context(socket) as context:
                        self.assert_prop(context, name)

        with nng.Socket(nng.Protocols.SUB0) as socket:
            with nng.Context(socket) as context:
                context.subscribe(b'topic-1')
                context.unsubscribe(b'topic-1')
                with self.assertRaises(nng.Errors.ENOENT):
                    context.unsubscribe(b'topic-2')
示例#2
0
def main(argv):

    if len(argv) < 3:
        print('usage: %s url num_ctxs' % argv[0], file=sys.stderr)
        return 1

    url = argv[1]
    num_ctxs = int(argv[2])

    with contextlib.ExitStack() as stack:

        socket = stack.enter_context(nng.Socket(nng.Protocols.REP0))
        socket.listen(url)

        servers = []
        for _ in range(num_ctxs):
            ctx = stack.enter_context(nng.Context(socket))
            server = threading.Thread(target=serve, args=(ctx, ))
            server.start()
            servers.append(server)

        while any(server.is_alive() for server in servers):
            try:
                for server in servers:
                    server.join()
            except KeyboardInterrupt:
                print('Keyboard interrupted!')
                nng.close_all()

    return 0
示例#3
0
文件: echo.py 项目: clchiou/garage
 def request():
     try:
         with nng.Socket(nng.Protocols.REQ0) as socket:
             socket.dial(argv[2])
             socket.send(argv[3].encode('utf-8'))
             print(socket.recv().decode('utf-8'))
     except nng.NngError as exc:
         print('request: nng error: %s' % exc)
示例#4
0
文件: pub.py 项目: clchiou/garage
 def publish(url):
     try:
         with nng.Socket(nng.Protocols.PUB0) as socket:
             socket.listen(url)
             for i in itertools.count():
                 socket.send(b'%d' % i)
                 time.sleep(0.1)
     except nng.NngError as exc:
         print('serve: nng error: %s' % exc)
示例#5
0
 def subscribe(url, topic):
     topic = topic.encode('utf-8')
     try:
         with nng.Socket(nng.Protocols.SUB0) as socket:
             socket.subscribe(topic)
             socket.dial(url)
             while True:
                 print(socket.recv().decode('utf-8'))
                 time.sleep(0.1)
     except nng.NngError as exc:
         print('serve: nng error: %s' % exc)
示例#6
0
 def test_inproc_options(self):
     with nng.Socket(nng.Protocols.REP0) as socket:
         for endpoint in (
                 socket.dial('inproc://%s' % uuid.uuid4(),
                             create_only=True),
                 socket.listen('inproc://%s' % uuid.uuid4(),
                               create_only=True),
         ):
             for name, prop in self.get_properties(endpoint):
                 with self.subTest((endpoint, name)):
                     if (name.startswith('ipc_') or name.startswith('tls_')
                             or name.startswith('ws_')
                             or name == 'tcp_bound_port'):
                         self.assert_no_prop(endpoint, name, prop)
                     else:
                         self.assert_prop(endpoint, name, prop)
示例#7
0
    def test_invalid_response(self):
        with clients.Client(Request, Response, WIRE_DATA) as client:
            with nng.Socket(nng.Protocols.REP0) as socket:
                url = 'inproc://%s' % uuid.uuid4()
                socket.listen(url)
                client.socket.dial(url)

                task = tasks.spawn(client.m.greet(name='world'))
                with self.assertRaises(kernels.KernelTimeout):
                    kernels.run(timeout=0)

                socket.recv()
                socket.send(b'{"result": {"greet": 42}, "error": null}')

                kernels.run(timeout=1)
                self.assertTrue(task.is_completed())
                with self.assertRaisesRegex(AssertionError, r'expect.*str'):
                    task.get_result_nonblocking()
示例#8
0
文件: echo.py 项目: clchiou/garage
 def serve():
     try:
         with nng.Socket(nng.Protocols.REP0) as socket:
             print(
                 f'name={socket.name!r}\n'
                 f'protocol_name={socket.protocol_name!r}\n'
                 f'max_recv_size={socket.max_recv_size!r}\n'
                 f'min_reconnect_time={socket.min_reconnect_time!r}\n'
                 f'max_reconnect_time={socket.max_reconnect_time!r}\n',
                 end='',
             )
             socket.listen(argv[2])
             while True:
                 data = socket.recv()
                 print('serve: recv: %r' % data)
                 socket.send(data)
     except nng.NngError as exc:
         print('serve: nng error: %s' % exc)
示例#9
0
    def test_success(self):
        with clients.Client(Request, Response, WIRE_DATA) as client:
            with nng.Socket(nng.Protocols.REP0) as socket:
                url = 'inproc://%s' % uuid.uuid4()
                socket.listen(url)
                client.socket.dial(url)

                task = tasks.spawn(client.m.greet(name='world'))
                with self.assertRaises(kernels.KernelTimeout):
                    kernels.run(timeout=0)

                request = WIRE_DATA.to_upper(Request, socket.recv())
                self.assertEqual(request.args, Request.m.greet(name='world'))

                response = Response(result=Response.Result(
                    greet='hello world'))
                socket.send(WIRE_DATA.to_lower(response))

                kernels.run(timeout=1)
                self.assertTrue(task.is_completed())
                self.assertEqual(task.get_result_nonblocking(), 'hello world')
示例#10
0
    def test_tcp_options(self):
        with nng.Socket(nng.Protocols.REP0) as socket:

            # Set ``create_only`` to false to have ``tcp_bound_port``.
            endpoint = socket.listen('tcp://127.0.0.1:0')
            for name, prop in self.get_properties(endpoint):
                with self.subTest((endpoint, name)):
                    if (name.startswith('ipc_') or name.startswith('tls_')
                            or name.startswith('ws_')
                            or name == 'remote_address'):
                        self.assert_no_prop(endpoint, name, prop)
                    else:
                        self.assert_prop(endpoint, name, prop)

            endpoint = socket.dial('tcp://127.0.0.1:8000', create_only=True)
            for name, prop in self.get_properties(endpoint):
                with self.subTest((endpoint, name)):
                    if (name.startswith('ipc_') or name.startswith('tls_')
                            or name.startswith('ws_')
                            or name == 'remote_address'
                            or name == 'tcp_bound_port'):
                        self.assert_no_prop(endpoint, name, prop)
                    else:
                        self.assert_prop(endpoint, name, prop)