package io.aeron.test;

import io.aeron.driver.reports.LossReportReader;
import io.aeron.driver.reports.LossReportUtil;
import org.agrona.concurrent.AtomicBuffer;
import org.agrona.concurrent.UnsafeBuffer;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import static java.nio.channels.FileChannel.MapMode.READ_ONLY;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class LossReportTestUtil
{
    public static void verifyLossOccurredForStream(
        final String aeronDirectoryName,
        final int streamId) throws IOException
    {
        final File lossReportFile = LossReportUtil.file(aeronDirectoryName);
        assertTrue(lossReportFile.exists());

        try (
            RandomAccessFile file = new RandomAccessFile(lossReportFile, "r");
            FileChannel channel = file.getChannel())
        {
            final MappedByteBuffer mappedByteBuffer = channel.map(READ_ONLY, 0, channel.size());
            final AtomicBuffer buffer = new UnsafeBuffer(mappedByteBuffer);

            final LossReportReader.EntryConsumer lossEntryConsumer = mock(LossReportReader.EntryConsumer.class);
            LossReportReader.read(buffer, lossEntryConsumer);

            verify(lossEntryConsumer).accept(
                longThat(l -> l > 0), longThat(l -> l > 0), anyLong(), anyLong(), anyInt(), eq(streamId), any(), any());
        }
    }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2014-2019 Real Logic Ltd.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * https://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -714,16 +714,42 @@
 rtUtil;%0A
+import org.agrona.IoUtil;%0A
 import o
@@ -1302,25 +1302,16 @@
 rStream(
-%0A        
 final St
@@ -1334,24 +1334,16 @@
 oryName,
-%0A       
  final i
@@ -1354,16 +1354,24 @@
 treamId)
+%0A       
  throws 
@@ -1523,25 +1523,59 @@
     
-try (%0A   
+MappedByteBuffer mappedByteBuffer = null;%0A%0A
         
  Ran
@@ -1570,17 +1570,21 @@
         
- 
+try (
 RandomAc
@@ -1721,31 +1721,8 @@
     
-final MappedByteBuffer 
 mapp
@@ -2095,33 +2095,35 @@
        longThat(
-l
+(l)
  -%3E l %3E 0), long
@@ -2117,16 +2117,32 @@
  l %3E 0),
+%0A               
  longTha
@@ -2143,17 +2143,19 @@
 ongThat(
-l
+(l)
  -%3E l %3E 
@@ -2161,67 +2161,243 @@
  0),
- anyLong(), anyLong(), anyInt(), eq(streamId), any(), any()
+%0A                anyLong(),%0A                anyLong(),%0A                anyInt(),%0A                eq(streamId),%0A                any(),%0A                any());%0A        %7D%0A        finally%0A        %7B%0A            IoUtil.unmap(mappedByteBuffer
 );%0A 

Fix Windows build by unmapping file used in test.