/*
 * Copyright 2014 Real Logic Ltd.
 *
 * 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 uk.co.real_logic.agrona.concurrent;

import org.junit.Test;
import uk.co.real_logic.agrona.LangUtil;

import java.nio.channels.ClosedByInterruptException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;

public class AgentRunnerTest
{
    @SuppressWarnings("unchecked")
    private final Consumer<Throwable> exceptionConsumer = mock(Consumer.class);

    private final Agent mockAgent = mock(Agent.class);
    private final IdleStrategy idleStrategy = new NoOpIdleStrategy();
    private final AgentRunner runner = new AgentRunner(idleStrategy, exceptionConsumer, null, mockAgent);

    @Test
    public void shouldReturnAgent()
    {
        assertThat(runner.agent(), is(mockAgent));
    }

    @Test
    public void shouldNotDoWorkOnClosedRunner() throws Exception
    {
        runner.close();
        runner.run();

        verify(mockAgent, never()).doWork();
        verify(mockAgent, never()).onClose();
    }

    @Test
    public void shouldReportExceptionThrownByAgent() throws Exception
    {
        final CountDownLatch latch = new CountDownLatch(1);
        final RuntimeException expectedException = new RuntimeException();
        when(mockAgent.doWork()).thenThrow(expectedException);

        final Consumer<Throwable> exceptionHandler =
            (ex) ->
            {
                assertThat(ex, is(expectedException));
                latch.countDown();
            };

        final AgentRunner runner = new AgentRunner(idleStrategy, exceptionHandler, null, mockAgent);
        new Thread(runner).start();

        if (!latch.await(3, TimeUnit.SECONDS))
        {
            fail("Should have called exception handler");
        }

        runner.close();
    }

    @Test
    public void shouldNotReportClosedByInterruptException() throws Exception
    {
        when(mockAgent.doWork()).thenThrow(new ClosedByInterruptException());

        assertExceptionNotReported();
    }

    @Test
    public void shouldNotReportRethrownClosedByInterruptException() throws Exception
    {
        when(mockAgent.doWork()).thenAnswer(inv ->
        {
            try
            {
                throw new ClosedByInterruptException();
            }
            catch (ClosedByInterruptException e)
            {
                LangUtil.rethrowUnchecked(e);
                return null;
            }
        });

        assertExceptionNotReported();
    }

    private void assertExceptionNotReported() throws InterruptedException
    {
        final Consumer<Throwable> exceptionHandler = mock(Consumer.class);

        final AgentRunner runner = new AgentRunner(idleStrategy, exceptionHandler, null, mockAgent);
        new Thread(runner).start();

        Thread.sleep(100);

        runner.close();

        verify(exceptionHandler, never()).accept(any());
    }

}

@@ -2882,15 +2882,34 @@
 wer(
+%0A            (
 inv
+)
  -%3E%0A
+    
     
@@ -2926,16 +2926,20 @@
         
+    
 try%0A    
@@ -2938,34 +2938,42 @@
 try%0A            
-%7B%0A
+    %7B%0A    
                 
@@ -3016,34 +3016,40 @@
 ();%0A            
-%7D%0A
+    %7D%0A  
             catc
@@ -3048,15 +3048,23 @@
     
+  
 catch (
+final 
 Clos
@@ -3087,16 +3087,17 @@
 eption e
+x
 )%0A      
@@ -3094,34 +3094,42 @@
 ex)%0A            
+ 
-%7B%0A
+   %7B%0A    
                 
@@ -3155,19 +3155,39 @@
 hecked(e
+x
 );%0A
+                %7D%0A%0A
         
@@ -3207,34 +3207,24 @@
 n null;%0A    
-        %7D%0A
         %7D);%0A
@@ -3341,32 +3341,71 @@
 Exception%0A    %7B%0A
+        @SuppressWarnings(%22unchecked%22)%0A
         final Co

Fix warnings.