/*
 * 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 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();
    }
}


@@ -656,16 +656,110 @@
 t.Test;%0A
+import uk.co.real_logic.agrona.LangUtil;%0A%0Aimport java.nio.channels.ClosedByInterruptException;
 %0Aimport 
@@ -2515,11 +2515,1111 @@
 ;%0A    %7D%0A
-%7D
 %0A
+    @Test%0A    public void shouldNotReportClosedByInterruptException() throws Exception%0A    %7B%0A        when(mockAgent.doWork()).thenThrow(new ClosedByInterruptException());%0A%0A        assertExceptionNotReported();%0A    %7D%0A%0A    @Test%0A    public void shouldNotReportRethrownClosedByInterruptException() throws Exception%0A    %7B%0A        when(mockAgent.doWork()).thenAnswer(inv -%3E%0A        %7B%0A            try%0A            %7B%0A                throw new ClosedByInterruptException();%0A            %7D%0A            catch (ClosedByInterruptException e)%0A            %7B%0A                LangUtil.rethrowUnchecked(e);%0A                return null;%0A            %7D%0A        %7D);%0A%0A        assertExceptionNotReported();%0A    %7D%0A%0A    private void assertExceptionNotReported() throws InterruptedException%0A    %7B%0A        final Consumer%3CThrowable%3E exceptionHandler = mock(Consumer.class);%0A%0A        final AgentRunner runner = new AgentRunner(idleStrategy, exceptionHandler, null, mockAgent);%0A        new Thread(runner).start();%0A%0A        Thread.sleep(100);%0A%0A        runner.close();%0A%0A        verify(exceptionHandler, never()).accept(any());%0A    %7D%0A%0A%7D
 %0A

add more exceptions to check behaviour in the presence of exceptions