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



##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/FailureSimulatingTcpDiscoverySpi.java
##########
@@ -0,0 +1,404 @@
+/*
+ * 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;
+
+/**
+ * The TcpDiscovery able to simulate network failure.
+ */
+public class FailureSimulatingTcpDiscoverySpi 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, int)
+     */
+    public void enableNetworkTimeoutSimulation(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.
+     * @param forceTimeout If positive of 0, overrides the delay preset in 
{@link #enableNetworkTimeoutSimulation(int,
+     *                     int)}.
+     * @see #enableNetworkTimeoutSimulation(int, int)
+     */
+    private void simulateTimeout(Socket sock, int forceTimeout) 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;
+
+        int timeout = forceTimeout >= 0 ? forceTimeout : 
simulatedTimeout.get2();
+
+        try {
+            Thread.sleep(timeout);
+        }
+        catch (InterruptedException ignored) {
+            // No-op.
+        }
+
+        throw new SocketTimeoutException("Simulated failure after delay: " + 
timeout + "ms.");
+    }
+
+    /**
+     * @see #simulateTimeout(Socket, int)
+     */
+    private void simulateTimeout(Socket sock) throws SocketTimeoutException {
+        simulateTimeout(sock, -1);
+    }
+
+    /**
+     * 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 {
+                    simulateTimeout(delegate);
+
+                    src.write(b);

Review comment:
       Real TcpDiscoverySPI woul require simulate-failure test-only methods. 
Was declared by previous reviews. This PR is of second version. Previous 
contained simulation within TcpDiscoverySPI. But brought extra test only 
methods and required to simulate failure in every new place if TcpDiscovery 
gets modified. That's why I made separate SPI based only on socket. Look OK to 
simulate failure directly by socket, not by higher logic. Exceptions are thrown 
from socket/streams in reality. We simulate exceptions from socket/streams. We 
are independent of code change of TcpDiscovery and do not need to watch for its 
changes to simulate failure from new code places.




-- 
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