Vladsz83 commented on a change in pull request #8881:
URL: https://github.com/apache/ignite/pull/8881#discussion_r704779206



##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/NetFailureTcpDiscoverySpi.java
##########
@@ -0,0 +1,393 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.SocketChannel;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Tcp Discovery able to simulate network failure.
+ */
+public class NetFailureTcpDiscoverySpi extends TcpDiscoverySpi {
+    /**
+     * If not {@code null}, enables network timeout simulation. First value 
switches traffic droppage: negative for all
+     * incoming, positive for all outgoing, 0 for both.
+     */
+    protected volatile IgnitePair<Integer> simulatedTimeout;
+
+    /** {@inheritDoc} */
+    @Override protected Socket createSocket0() throws IOException {
+        return new SocketWrap(super.createSocket0());
+    }
+
+    /**
+     * Enables simulation of network timeout.
+     *
+     * @param direction If negative, enables timeout simulation for incomming 
traffic. If positive, enables timeout
+     *                  simulation for outgoing traffic. Set 0 to simlate 
failure for both traffics.
+     * @param delay     Milliseconds of awaiting before raising {@code 
SocketTimeoutException}.
+     * @see SocketWrap#simulateTimeout(Socket)
+     */
+    public void setNetworkTimeout(int direction, int delay) {
+        simulatedTimeout = new IgnitePair<>(direction, delay);
+    }
+
+    /**
+     * Simulates network timeout if enabled, raises {@code 
SocketTimeoutException}.
+     *
+     * @param sock The socket to simulate failure at.
+     * @see #setNetworkTimeout(int, int)
+     */
+    private void simulateTimeout(Socket sock) throws SocketTimeoutException {
+        IgnitePair<Integer> simulatedTimeout = this.simulatedTimeout;
+
+        if (simulatedTimeout == null)
+            return;
+
+        boolean isClientSock = sock.getLocalPort() < locPort || 
sock.getLocalPort() > locPort + locPortRange;
+
+        if (isClientSock && simulatedTimeout.get1() < 0 || !isClientSock && 
simulatedTimeout.get1() > 0)
+            return;
+
+        try {
+            Thread.sleep(simulatedTimeout.get2());
+        }
+        catch (InterruptedException ignored) {
+            // No-op.
+        }
+
+        throw new SocketTimeoutException("Simulated failure after delay: " + 
simulatedTimeout.get2() + "ms.");
+    }
+
+    /**
+     * Network failure simulator.
+     */
+    private class SocketWrap extends Socket {
+        /** The real socket to simulate failure of. */
+        private final Socket delegate;
+
+        /**
+         * Constructor.
+         *
+         * @param sock The real socket to simulate failure of.
+         */
+        private SocketWrap(Socket sock) {
+            delegate = sock;
+        }
+
+        /** {@inheritDoc} */
+        @Override public OutputStream getOutputStream() throws IOException {
+            OutputStream src = delegate.getOutputStream();
+
+            return new OutputStream() {
+                /** {@inheritDoc} */
+                @Override public void write(@NotNull byte[] b) throws 
IOException {

Review comment:
       Fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to