package com.hazelcast.spi.impl.operationservice.impl;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.nio.Address;
import com.hazelcast.spi.InternalCompletableFuture;
import com.hazelcast.spi.exception.TargetNotMemberException;
import com.hazelcast.spi.impl.operationservice.InternalOperationService;
import com.hazelcast.test.ExceptionThrowingCallable;
import com.hazelcast.test.ExpectedRuntimeException;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.NightlyTest;
import com.hazelcast.test.annotation.ParallelTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.net.UnknownHostException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelTest.class})
public class OperationServiceImpl_invokeOnTargetTest extends HazelcastTestSupport {

    private HazelcastInstance local;
    private InternalOperationService operationService;
    private HazelcastInstance remote;

    @Before
    public void setup() {
        HazelcastInstance[] nodes = createHazelcastInstanceFactory(2).newInstances();
        warmUpPartitions(nodes);

        local = nodes[0];
        remote = nodes[1];
        operationService = getOperationService(local);
    }

    @Test
    public void whenLocal() {
        String expected = "foobar";
        DummyOperation operation = new DummyOperation(expected);
        InternalCompletableFuture<String> invocation = operationService.invokeOnTarget(
                null, operation, getAddress(local));
        assertEquals(expected, invocation.join());

        //todo: we need to verify that the call was run on the calling thread
    }

    @Test
    public void whenRemote() {
        String expected = "foobar";
        DummyOperation operation = new DummyOperation(expected);
        InternalCompletableFuture<String> invocation = operationService.invokeOnTarget(
                null, operation, getAddress(remote));
        assertEquals(expected, invocation.join());
    }

    //this test is very slow, so it is marked as a nightly test
    @Test
    @Category(NightlyTest.class)
    public void whenNonExistingTarget() throws UnknownHostException {
        Address remoteAddress = getAddress(remote);
        remote.shutdown();

        String expected = "foobar";
        DummyOperation operation = new DummyOperation(expected);
        InternalCompletableFuture<String> invocation = operationService.invokeOnTarget(
                null, operation, remoteAddress);

        try {
            invocation.join();
            fail();
        } catch (TargetNotMemberException e) {
        }
    }

    @Test
    public void whenExceptionThrownInOperationRun() {
        DummyOperation operation = new DummyOperation(new ExceptionThrowingCallable());
        InternalCompletableFuture<String> invocation = operationService.invokeOnTarget(
                null, operation, getAddress(remote));

        try {
            invocation.join();
            fail();
        } catch (ExpectedRuntimeException expected) {
        }
    }

}

@@ -127,16 +127,58 @@
 ddress;%0A
+import com.hazelcast.spi.ExceptionAction;%0A
 import c
@@ -568,58 +568,8 @@
 rt;%0A
-import com.hazelcast.test.annotation.NightlyTest;%0A
 impo
@@ -2270,110 +2270,13 @@
     
-//this test is very slow, so it is marked as a nightly test%0A    @Test%0A    @Category(NightlyTest.class)
+@Test
 %0A   
@@ -2434,100 +2434,819 @@
     
-String expected = %22foobar%22;%0A        DummyOperation operation = new DummyOperation(expected);
+// ensure local instance observes remote shutdown%0A        assertClusterSizeEventually(1, local);%0A%0A        String expected = %22foobar%22;%0A        DummyOperation operation = new DummyOperation(expected) %7B%0A            @Override%0A            public ExceptionAction onInvocationException(Throwable throwable) %7B%0A                // Don't retry when TargetNotMemberException is received.%0A                // Invocation is registered before checking target. If invocation is retried%0A                // when TargetNotMemberException is get, invocation may fail with MemberLeftException too.%0A                if (throwable instanceof TargetNotMemberException) %7B%0A                    return ExceptionAction.THROW_EXCEPTION;%0A                %7D%0A                return super.onInvocationException(throwable);%0A            %7D%0A        %7D;%0A
 %0A   

Fix OperationServiceImpl_invokeOnTargetTest