Ejemplo n.º 1
0
    def poll(self, timeout=None, flags=_zmq.POLLIN):
        """poll the socket for events
        
        returns a Future for the poll results.
        """

        if self.closed:
            raise _zmq.ZMQError(_zmq.ENOTSUP)

        p = self._poller_class()
        p.register(self, flags)
        f = p.poll(timeout)
        
        future = self._Future()
        def unwrap_result(f):
            if future.done():
                return
            if f.exception():
                future.set_exception(f.exception())
            else:
                evts = dict(f.result())
                future.set_result(evts.get(self, 0))
        
        f.add_done_callback(unwrap_result)
        return future
Ejemplo n.º 2
0
 def done(self, client_id, filepath, timeout=None):
     """Tell the server a client is done with the file"""
     if not isinstance(filepath, bytes):
         filepath = filepath.encode('utf8')
     self._send(b'done', client_id, filepath, timeout=timeout)
     response = self._recv(timeout).decode('utf8')
     if response != 'ok':
         raise zmq.ZMQError('Invalid response from server: ' + response)
Ejemplo n.º 3
0
    def test_publish_silent_zmq_errors_when_socket_closed(self):
        publisher = StatsPublisher()
        publisher.socket = mock.MagicMock()
        publisher.socket.closed = True
        publisher.socket.send_multipart.side_effect = zmq.ZMQError()

        stat = {'subtopic': 1, 'foo': 'bar'}
        publisher.publish('foobar', stat)
Ejemplo n.º 4
0
 def bind(self, socket, enable_curve=True):
     if enable_curve:
         socket.curve_server = True  # must come before bind
     try:
         f = self.endpoint.format(socket.type)
         socket.bind(f)
     except zmq.ZMQError as e:
         raise zmq.ZMQError(f"unable to bind {f}: {e}") from e
Ejemplo n.º 5
0
 def test_bad_pending_write(self):
     self.tr._buffer = [(4, [b'data'])]
     self.tr._buffer_size = 4
     self.sock.send_multipart.side_effect = zmq.ZMQError(errno.ENOTSOCK)
     self.tr._do_write()
     self.assertEqual(0, self.tr._buffer_size)
     self.assertEqual([], self.tr._buffer)
     self.assertTrue(self.tr._closing)
Ejemplo n.º 6
0
def test_port_bind_failure_gives_up_retries():
    heart = Heartbeat(None)
    with patch.object(heart, '_try_bind_socket') as mock_try_bind:
        mock_try_bind.side_effect = zmq.ZMQError(errno.EADDRINUSE,
                                                 "fails for non-bind")
        with pytest.raises(zmq.ZMQError):
            heart._bind_socket()
        assert mock_try_bind.call_count == 100
Ejemplo n.º 7
0
 def ping(self, timeout=None):
     """Ping the server for a response. Timeout in seconds"""
     start_time = monotonic()
     self._send(b'hello', timeout=timeout)
     response = self._recv(timeout).decode('utf8')
     if response == 'hello':
         return round((monotonic() - start_time) * 1000, 2)
     raise zmq.ZMQError('Invalid response from server: ' + response)
Ejemplo n.º 8
0
    def test__read_ready_got_EAGAIN(self):
        self.sock.recv_multipart.side_effect = zmq.ZMQError(errno.EAGAIN)
        self.tr._fatal_error = mock.Mock()

        self.tr._read_ready()

        self.assertFalse(self.tr._fatal_error.called)
        self.assertFalse(self.proto.msg_received.called)
Ejemplo n.º 9
0
def test_port_bind_failure_raises():
    heart = Heartbeat(None)
    with patch.object(heart, '_try_bind_socket') as mock_try_bind:
        mock_try_bind.side_effect = zmq.ZMQError(
            -100, "fails for unknown error types")
        with pytest.raises(zmq.ZMQError):
            heart._bind_socket()
        assert mock_try_bind.call_count == 1
Ejemplo n.º 10
0
    def test_publish_reraise_zmq_errors(self):
        publisher = StatsPublisher()
        publisher.socket = mock.MagicMock()
        publisher.socket.closed = False
        publisher.socket.send_multipart.side_effect = zmq.ZMQError()

        stat = {'subtopic': 1, 'foo': 'bar'}
        self.assertRaises(zmq.ZMQError, publisher.publish, 'foobar', stat)
Ejemplo n.º 11
0
 def test__maybe_pause_protocol_already_paused(self):
     self.tr.set_write_buffer_limits(high=10)
     self.tr._protocol_paused = True
     self.sock.send_multipart.side_effect = zmq.ZMQError(errno.EAGAIN)
     self.tr.write([b'binary data'])
     self.assertEqual(11, self.tr._buffer_size)
     self.assertTrue(self.tr._protocol_paused)
     self.assertFalse(self.proto.pause_writing.called)
Ejemplo n.º 12
0
 def test_setsockopt_EINTR(self):
     self.sock.setsockopt.side_effect = [zmq.ZMQError(errno.EINTR), None]
     self.assertIsNone(self.tr.setsockopt("opt", "val"))
     self.assertEqual(
         [mock.call("opt", "val"),
          mock.call("opt", "val")],
         self.sock.setsockopt.call_args_list,
     )
Ejemplo n.º 13
0
 def test_partial_write(self):
     self.sock.send_multipart.side_effect = zmq.ZMQError(errno.EAGAIN)
     self.tr.write((b'a', b'b'))
     self.sock.send_multipart.assert_called_with((b'a', b'b'), zmq.DONTWAIT)
     self.assertFalse(self.proto.pause_writing.called)
     self.assertEqual([(2, (b'a', b'b'))], list(self.tr._buffer))
     self.assertEqual(2, self.tr._buffer_size)
     self.assertFalse(self.exc_handler.called)
     self.loop.assert_writer(self.sock, self.tr._write_ready)
Ejemplo n.º 14
0
    def _send_request(self, data) -> None:
        """ Send request to ZeroMQ system socket """

        try:
            self.sys_socket.send_json(data)
            msg = self.sys_socket.recv_string()
            assert msg == 'OK', 'Something wrong on server side...'
        except:
            raise zmq.ZMQError("Something wrong while sending request...")
Ejemplo n.º 15
0
    def test__read_ready_got_fatal_error(self):
        self.sock.recv_multipart.side_effect = zmq.ZMQError(errno.EINVAL)
        self.tr._fatal_error = mock.Mock()

        self.tr._read_ready()

        self.assertFalse(self.proto.msg_received.called)
        exc = self.tr._fatal_error.call_args[0][0]
        self.assertIsInstance(exc, OSError)
        self.assertEqual(exc.errno, errno.EINVAL)
Ejemplo n.º 16
0
    def recv(self):
        if len(self.__outqueue) == 0:
            raise zmq.ZMQError("Incoming message queue is empty")

        msg = self.__outqueue.pop(0)

        if self.__verbose:
            print("%s(%s) -> %s" %
                  (type(self).__name__, self.__name, str(msg)))

        return msg
Ejemplo n.º 17
0
def test_port_bind_failure_recovery():
    try:
        errno.WSAEADDRINUSE
    except AttributeError:
        # Fake windows address in-use code
        errno.WSAEADDRINUSE = 12345

    try:
        heart = Heartbeat(None)
        with patch.object(heart, '_try_bind_socket') as mock_try_bind:
            mock_try_bind.side_effect = [
                zmq.ZMQError(errno.EADDRINUSE, "fails for non-bind unix"),
                zmq.ZMQError(errno.WSAEADDRINUSE, "fails for non-bind windows")
            ] + [0] * 100
            # Shouldn't raise anything as retries will kick in
            heart._bind_socket()
    finally:
        # Cleanup fake assignment
        if errno.WSAEADDRINUSE == 12345:
            del errno.WSAEADDRINUSE
Ejemplo n.º 18
0
 def release(self, key, client_id=None):
     if not hasattr(self.local, 'sock'):
         self._new_socket()
     try:
         if client_id is None:
             client_id = self.local.client_id
         key = _ensure_bytes(key)
         self.local.sock.send_multipart([b'release', key, client_id])
         events = self.local.poller.poll(self.RESPONSE_TIMEOUT)
         if not events:
             raise zmq.ZMQError('No response from zlock server: timed out')
         response = self.local.sock.recv().decode('utf8')
         if response == 'ok':
             return
         raise zmq.ZMQError(response)
     except:
         if hasattr(self.local, 'sock'):
             self.local.sock.close(linger=False)
             del self.local.sock
         raise
Ejemplo n.º 19
0
 def _recv(self, timeout=None):
     """Receive with a timeout in seconds"""
     # convert timeout to ms or use default:
     timeout = self.RESPONSE_TIMEOUT if timeout is None else 1000 * timeout
     try:
         events = self.local.poller.poll(timeout)
         if not events:
             raise zmq.ZMQError('No response from zlog server: timed out')
         response = self.local.sock.recv_multipart()
         if len(response) != 2 or response[0] != b'':
             raise zmq.ZMQError('Malformed message from server: ' +
                                str(response))
         if response[1].startswith(b'error:'):
             raise ValueError(response[1].decode('utf8'))
         return response[1]
     except:
         self.local.sock.close(linger=False)
         del self.local.sock
         del self.local.poller
         raise
Ejemplo n.º 20
0
 def test_write_common_error(self):
     self.sock.send_multipart.side_effect = zmq.ZMQError(errno.ENOTSUP)
     self.tr.write((b'a', b'b'))
     self.sock.send_multipart.assert_called_once_with((b'a', b'b'),
                                                      zmq.DONTWAIT)
     self.assertFalse(self.proto.pause_writing.called)
     self.assertFalse(self.tr._buffer)
     self.assertEqual(0, self.tr._buffer_size)
     self.assertNotIn(self.sock, self.loop.writers)
     check_errno(errno.ENOTSUP,
                 self.exc_handler.call_args[0][1]['exception'])
Ejemplo n.º 21
0
    def test_register_with_zmq_error(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)

        m = mock.Mock()
        m.side_effect = zmq.ZMQError(errno.EFAULT, 'not a socket')
        s._poller.register = m

        with self.assertRaises(OSError) as ctx:
            s.register(1, EVENT_READ)
        self.assertEqual(errno.EFAULT, ctx.exception.errno)
        self.assertNotIn(1, s.get_map())
Ejemplo n.º 22
0
 def test_partial_double_write(self):
     self.sock.send_multipart.side_effect = zmq.ZMQError(errno.EAGAIN)
     self.tr.write((b"a", b"b"))
     self.tr.write((b"c", ))
     self.sock.send_multipart.mock_calls = [
         mock.call((b"a", b"b"), zmq.DONTWAIT)
     ]
     self.assertFalse(self.proto.pause_writing.called)
     self.assertEqual([(2, (b"a", b"b")), (1, (b"c", ))],
                      list(self.tr._buffer))
     self.assertEqual(3, self.tr._buffer_size)
     self.assertFalse(self.exc_handler.called)
     self.loop.assert_writer(self.sock, self.tr._write_ready)
Ejemplo n.º 23
0
 def ping(self, timeout=None):
     """Ping the server to test for a response"""
     if timeout is None:
         timeout = self.RESPONSE_TIMEOUT
     else:
         timeout = 1000 * timeout  # convert to ms
     if not hasattr(self.local, 'sock'):
         self._new_socket(timeout)
     try:
         start_time = monotonic()
         self.local.sock.send(b'hello', zmq.NOBLOCK)
         events = self.local.poller.poll(timeout)
         if events:
             response = self.local.sock.recv().decode('utf8')
             if response == 'hello':
                 return round((monotonic() - start_time) * 1000, 2)
             raise zmq.ZMQError('Invalid repsonse from server: ' + response)
         raise zmq.ZMQError('No response from zlock server: timed out')
     except:
         self.local.sock.close(linger=False)
         del self.local.sock
         raise
Ejemplo n.º 24
0
def test_port_bind_failure_raises(request):
    cfg = Config()
    with TemporaryWorkingDirectory() as d:
        cfg.ProfileDir.location = d
        cf = 'kernel.json'
        app = DummyKernelApp(config=cfg, connection_file=cf)
        request.addfinalizer(app.close)
        app.initialize()
        with patch.object(app, '_try_bind_socket') as mock_try_bind:
            mock_try_bind.side_effect = zmq.ZMQError(-100, "fails for unknown error types")
            with pytest.raises(zmq.ZMQError):
                app.init_sockets()
            assert mock_try_bind.call_count == 1
Ejemplo n.º 25
0
def test_port_bind_failure_gives_up_retries(request):
    cfg = Config()
    with TemporaryWorkingDirectory() as d:
        cfg.ProfileDir.location = d
        cf = "kernel.json"
        app = DummyKernelApp(config=cfg, connection_file=cf)
        request.addfinalizer(app.close)
        app.initialize()
        with patch.object(app, "_try_bind_socket") as mock_try_bind:
            mock_try_bind.side_effect = zmq.ZMQError(errno.EADDRINUSE,
                                                     "fails for non-bind")
            with pytest.raises(zmq.ZMQError):
                app.init_sockets()
            assert mock_try_bind.call_count == 100
Ejemplo n.º 26
0
def test_port_bind_failure_recovery(request):
    try:
        errno.WSAEADDRINUSE
    except AttributeError:
        # Fake windows address in-use code
        p = patch.object(errno, 'WSAEADDRINUSE', 12345, create=True)
        p.start()
        request.addfinalizer(p.stop)

    cfg = Config()
    with TemporaryWorkingDirectory() as d:
        cfg.ProfileDir.location = d
        cf = 'kernel.json'
        app = DummyKernelApp(config=cfg, connection_file=cf)
        request.addfinalizer(app.close)
        app.initialize()
        with patch.object(app, '_try_bind_socket') as mock_try_bind:
            mock_try_bind.side_effect = [
                zmq.ZMQError(errno.EADDRINUSE, "fails for non-bind unix"),
                zmq.ZMQError(errno.WSAEADDRINUSE, "fails for non-bind windows")
            ] + [0] * 100
            # Shouldn't raise anything as retries will kick in
            app.init_sockets()
Ejemplo n.º 27
0
    def test_select_with_zmq_error(self):
        s = self.SELECTOR()
        self.addCleanup(s.close)

        rd, wr = self.make_socketpair()
        s.register(rd, EVENT_READ)

        m = mock.Mock()
        m.side_effect = zmq.ZMQError(errno.EFAULT, 'not a socket')
        s._poller.poll = m

        with self.assertRaises(OSError) as ctx:
            s.select()
        self.assertEqual(errno.EFAULT, ctx.exception.errno)
Ejemplo n.º 28
0
def get_random_port(min_port=49152, max_port=65536, max_tries=100):
    """
    Return available random ZeroMQ port.
    """

    ctx = zmq.Context()
    sock = ctx.socket(zmq.REQ)
    try:
        port = sock.bind_to_random_port('tcp://*', min_port, max_port, max_tries)
    except:
        raise zmq.ZMQError(msg='Could not find free port')
    finally:
        sock.close()
    return port
Ejemplo n.º 29
0
    def test__write_ready_raises_ZMQError(self):
        self.tr._buffer.append((2, (b'a', b'b')))
        self.tr._buffer_size = 2
        self.loop.add_writer(self.sock, self.tr._write_ready)

        self.sock.send_multipart.side_effect = zmq.ZMQError(
            errno.ENOTSUP, 'not supported')
        self.tr._write_ready()
        self.assertFalse(self.proto.pause_writing.called)
        self.assertFalse(self.tr._buffer)
        self.assertEqual(0, self.tr._buffer_size)
        self.assertTrue(self.exc_handler.called)
        self.assertEqual(1, self.loop.remove_writer_count[self.sock])
        self.assertTrue(self.tr._closing)
        self.assertEqual(1, self.loop.remove_reader_count[self.sock])
Ejemplo n.º 30
0
 def test__maybe_pause_protocol_err(self):
     self.tr.set_write_buffer_limits(high=10)
     ceh = self.loop.call_exception_handler = mock.Mock()
     self.assertFalse(self.tr._protocol_paused)
     self.sock.send_multipart.side_effect = zmq.ZMQError(errno.EAGAIN)
     self.proto.pause_writing.side_effect = exc = RuntimeError()
     self.tr.write([b'binary data'])
     self.assertEqual(11, self.tr._buffer_size)
     self.assertTrue(self.tr._protocol_paused)
     ceh.assert_called_with({
         'transport': self.tr,
         'exception': exc,
         'protocol': self.proto,
         'message': 'protocol.pause_writing() failed'
     })