/*
 * Copyright 2016 higherfrequencytrading.com
 *
 * Licensed 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 net.openhft.chronicle.wire;

import net.openhft.chronicle.bytes.MappedBytes;
import org.junit.Test;

import java.io.File;
import java.nio.file.Files;

import static org.junit.Assert.assertEquals;

/**
 * @author lburgazzoli
 */
public class WireResourcesTest {

    @Test
    public void testMappedBytesClose() throws Exception {
        File tmp = Files.createTempFile("chronicle-", ".wire").toFile();
        tmp.deleteOnExit();

        MappedBytes mb0;
        try (MappedBytes mb = MappedBytes.mappedBytes(tmp, 64 * 1024)) {
            assertEquals(1, mb.mappedFile().refCount());
            assertEquals(1, mb.refCount());

            Wire wire = WireType.TEXT.apply(mb);

            assert wire.startUse();
            wire.headerNumber(0);

            assertEquals(1, mb.mappedFile().refCount());
            wire.writeFirstHeader(); // first touches the file.
            assertEquals(2, mb.mappedFile().refCount());

            wire.updateFirstHeader();
            assert wire.endUse();

            assertEquals(2, mb.mappedFile().refCount());
            assertEquals(1, mb.refCount());

            mb0 = mb;
        }
        assertEquals(0, mb0.mappedFile().refCount());
        assertEquals(0, mb0.refCount());
    }

    @Test
    public void testMappedBytesWireRelease() throws Exception {
        File tmp = Files.createTempFile("chronicle-", ".wire").toFile();
        tmp.deleteOnExit();

        Wire wire;
        try (MappedBytes mb = MappedBytes.mappedBytes(tmp, 64 * 1024)) {
            assertEquals(1, mb.mappedFile().refCount());
            assertEquals(1, mb.refCount());

            wire = WireType.TEXT.apply(mb);
            wire.bytes().reserve();

            assertEquals(1, mb.mappedFile().refCount());
            assertEquals(2, mb.refCount());
        } // not really closed as we have one reverse left.

        assertEquals(1, wire.bytes().refCount());

        assert wire.startUse();
        wire.headerNumber(1);
        wire.writeFirstHeader();
        wire.updateFirstHeader();
        assert wire.endUse();

        wire.bytes().release();
        assertEquals(0, wire.bytes().refCount());
    }
}

@@ -651,120 +651,192 @@
 ort 
-net.openhft.chronicle.bytes.MappedBytes;%0Aimport org.junit.Test;%0A%0Aimport java.io.File;%0Aimport java.nio.file.Fil
+java.io.File;%0Aimport java.nio.file.Files;%0A%0Aimport net.openhft.chronicle.bytes.MappedBytes;%0Aimport net.openhft.chronicle.bytes.MappedFile;%0Aimport org.junit.Ignore;%0Aimport org.junit.T
 es
+t
 ;%0A%0Ai
@@ -2835,11 +2835,853 @@
 );%0A    %7D
+%0A%0A    @Ignore(%22mh!!%22)%0A    @Test%0A    public void testMappedBytesWireRelease2() throws Exception %7B%0A        File tmp = Files.createTempFile(%22chronicle-%22, %22.wire%22).toFile();%0A        tmp.deleteOnExit();%0A%0A        Wire wire = WireType.TEXT.apply(MappedBytes.mappedBytes(tmp, 64 * 1024));%0A%0A        assert wire.startUse();%0A        wire.headerNumber(1);%0A        wire.writeFirstHeader();%0A        wire.updateFirstHeader();%0A%0A        assertEquals(1, wire.bytes().refCount());%0A        assertEquals(2, mappedFile(wire).refCount()); // Shouldn't be 1 ?%0A%0A        assert wire.endUse();%0A%0A        wire.bytes().release();%0A        assertEquals(0, wire.bytes().refCount());%0A        assertEquals(0, mappedFile(wire).refCount()); // Shouldn't be 1 ?%0A    %7D%0A%0A    protected MappedFile mappedFile(Wire wire) %7B%0A        return ((MappedBytes)wire.bytes()).mappedFile();%0A    %7D
 %0A%7D%0A

Add test case