/*
 * Copyright 2014 Timothy Brooks
 *
 * 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.uncontended.precipice.example;

import net.uncontended.precipice.ResilientAction;
import net.uncontended.precipice.Services;
import net.uncontended.precipice.Status;
import net.uncontended.precipice.SubmissionService;
import net.uncontended.precipice.concurrent.ResilientFuture;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

public class SubmissionExample {

    public static void main(String[] args) throws InterruptedException {
        String serviceName = "Identity Service";
        int poolSize = 5;
        int concurrencyLevel = 100;
        SubmissionService service = Services.submissionService(serviceName, poolSize, concurrencyLevel);

        int millisTimeout = 10;
        ResilientFuture<Integer> successFuture = service.submit(new SuccessAction(), millisTimeout);

        try {
            // Should return 64
            successFuture.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        assert (successFuture.getStatus() == Status.SUCCESS);

        ResilientFuture<Integer> errorFuture = service.submit(new ErrorAction(), millisTimeout);

        try {
            // Should throw ExecutionException
            errorFuture.get();
        } catch (ExecutionException e) {
            // Should be Runtime Exception.
            Throwable cause = e.getCause();
            // Should be "Action Failed."
            cause.getMessage();
        }
        assert (errorFuture.getStatus() == Status.ERROR);

        CountDownLatch latch = new CountDownLatch(1);
        ResilientFuture<String> timeoutFuture = service.submit(new TimeoutAction(latch), millisTimeout);

        assert (timeoutFuture.getStatus() == Status.PENDING);
        try {
            // Should return null
            timeoutFuture.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        // Should return Status.TIMEOUT
        assert (timeoutFuture.getStatus() == Status.TIMEOUT);
        latch.countDown();
        service.shutdown();
    }

    private static class SuccessAction implements ResilientAction<Integer> {

        @Override
        public Integer run() throws Exception {
            return 8 * 8;
        }
    }

    private static class ErrorAction implements ResilientAction<Integer> {

        @Override
        public Integer run() throws Exception {
            throw new RuntimeException("Action failed.");
        }
    }

    private static class TimeoutAction implements ResilientAction<String> {

        private final CountDownLatch latch;

        public TimeoutAction(CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public String run() throws Exception {
            latch.await();
            return "Done";
        }
    }
}

@@ -1400,13 +1400,17 @@
 mit(
-new S
+Actions.s
 ucce
@@ -1730,13 +1730,17 @@
 mit(
-new E
+Actions.e
 rror
@@ -2208,22 +2208,23 @@
 tFuture%3C
-String
+Integer
 %3E timeou
@@ -2252,13 +2252,17 @@
 mit(
-new T
+Actions.t
 imeo
@@ -2647,24 +2647,24 @@
 ountDown();%0A
+
         serv
@@ -2688,765 +2688,7 @@
    %7D
-%0A%0A    private static class SuccessAction implements ResilientAction%3CInteger%3E %7B%0A%0A        @Override%0A        public Integer run() throws Exception %7B%0A            return 8 * 8;%0A        %7D%0A    %7D%0A%0A    private static class ErrorAction implements ResilientAction%3CInteger%3E %7B%0A%0A        @Override%0A        public Integer run() throws Exception %7B%0A            throw new RuntimeException(%22Action failed.%22);%0A        %7D%0A    %7D%0A%0A    private static class TimeoutAction implements ResilientAction%3CString%3E %7B%0A%0A        private final CountDownLatch latch;%0A%0A        public TimeoutAction(CountDownLatch latch) %7B%0A            this.latch = latch;%0A        %7D%0A%0A        @Override%0A        public String run() throws Exception %7B%0A            latch.await();%0A            return %22Done%22;%0A        %7D%0A    %7D
 %0A%7D%0A

Use common actions in submission example