package net.openhft.chronicle.network;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.threads.InvalidEventHandlerException;
import net.openhft.chronicle.network.api.TcpHandler;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;

import static junit.framework.TestCase.fail;

@Ignore("see https://github.com/OpenHFT/Chronicle-Network/issues/75")
public class TcpEventHandlerReleaseTest extends NetworkTestCommon {
    private static final String hostPort = "host.port";

    @Before
    public void setUp() throws IOException {
        TCPRegistry.createServerSocketChannelFor(hostPort);
    }

    @Test
    public void testRelease() throws IOException {
        try (TcpEventHandler t = createTcpEventHandler()) {
            t.loopFinished();
            t.close();
            // check second close OK
        }

    }

    @Test
    public void testBuffersReleasedWhenSocketChannelClosed() throws IOException {
        try (TcpEventHandler t = createTcpEventHandler()) {
            t.socketChannel().close();
            try {
                t.action();
                fail();
            } catch (InvalidEventHandlerException e) {
                // expected.
            }
            t.loopFinished();
        }
    }

    public TcpEventHandler createTcpEventHandler() throws IOException {
        NetworkContext nc = new VanillaNetworkContext();
        nc.socketChannel(TCPRegistry.createSocketChannel(hostPort));
        TcpEventHandler tcpEventHandler = new TcpEventHandler(nc);
        tcpEventHandler.tcpHandler(NullTcpHandler.INSTANCE);
        return tcpEventHandler;
    }

    enum NullTcpHandler implements TcpHandler {
        INSTANCE;

        @Override
        public void process(@NotNull Bytes in, @NotNull Bytes out, NetworkContext nc) {

        }

        @Override
        public void close() {

        }

        @Override
        public boolean isClosed() {
            return false;
        }
    }
}

@@ -75,16 +75,55 @@
 .Bytes;%0A
+import net.openhft.chronicle.core.Jvm;%0A
 import n
@@ -186,16 +186,16 @@
 eption;%0A
-
 import n
@@ -310,33 +310,8 @@
 re;%0A
-import org.junit.Ignore;%0A
 impo
@@ -408,78 +408,52 @@
 il;%0A
-%0A@Ignore(%22see https://github.com/OpenHFT/Chronicle-Network/issues/75%22)
+import static org.junit.Assume.assumeFalse;%0A
 %0Apub
@@ -1011,32 +1011,153 @@
 s IOException %7B%0A
+        // @Ignore(%22see https://github.com/OpenHFT/Chronicle-Network/issues/75%22)%0A        assumeFalse(Jvm.isJava9Plus());%0A
         try (Tcp

Enable for Java 8, https://github.com/OpenHFT/Chronicle-Network/issues/75