/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.camel.component.grpc;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.StreamObserver;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GrpcConsumerConcurrentTest extends GrpcTestSupport {
    private static final Logger LOG = LoggerFactory.getLogger(GrpcConsumerConcurrentTest.class);

    private static final int CONCURRENT_THREAD_COUNT = 30;
    private static final int ROUNDS_PER_THREAD_COUNT = 10;
    private static final String GRPC_TEST_PING_VALUE = "PING";
    private static final String GRPC_TEST_PONG_VALUE = "PONG";
    private static final String GRPC_USER_AGENT_PREFIX = "user-agent-";
    private static AtomicInteger idCounter = new AtomicInteger();

    public static Integer createId() {
        return idCounter.getAndIncrement();
    }

    public static Integer getId() {
        return idCounter.get();
    }

    @Test
    public void testAsyncWithConcurrentThreads() throws Exception {
        int asyncPort = getRoutePort("grpc-async");
        runConcurrent(() -> {
            final CountDownLatch latch = new CountDownLatch(1);
            ManagedChannel asyncRequestChannel
                    = NettyChannelBuilder.forAddress("localhost", asyncPort).usePlaintext()
                            .build();
            PingPongGrpc.PingPongStub asyncNonBlockingStub = PingPongGrpc.newStub(asyncRequestChannel);

            PongResponseStreamObserver responseObserver = new PongResponseStreamObserver(latch);
            int instanceId = createId();

            final PingRequest pingRequest
                    = PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(instanceId).build();
            StreamObserver<PingRequest> requestObserver = asyncNonBlockingStub.pingAsyncAsync(responseObserver);
            requestObserver.onNext(pingRequest);
            requestObserver.onNext(pingRequest);
            requestObserver.onCompleted();
            try {
                assertTrue(latch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                LOG.debug("Unhandled exception (probably safe to ignore): {}", e.getMessage(), e);
            }

            PongResponse pongResponse = responseObserver.getPongResponse();

            assertNotNull(pongResponse, "instanceId = " + instanceId);
            assertEquals(instanceId, pongResponse.getPongId());
            assertEquals(GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE, pongResponse.getPongName());

            asyncRequestChannel.shutdown().shutdownNow();
            return null;
        });
    }

    @Test
    public void testHeadersWithConcurrentThreads() throws Exception {
        int headersPort = getRoutePort("grpc-headers");
        runConcurrent(() -> {
            int instanceId = createId();
            final CountDownLatch latch = new CountDownLatch(1);
            ManagedChannel asyncRequestChannel = NettyChannelBuilder.forAddress("localhost", headersPort)
                    .userAgent(GRPC_USER_AGENT_PREFIX + instanceId)
                    .usePlaintext().build();
            PingPongGrpc.PingPongStub asyncNonBlockingStub = PingPongGrpc.newStub(asyncRequestChannel);

            PongResponseStreamObserver responseObserver = new PongResponseStreamObserver(latch);

            final PingRequest pingRequest
                    = PingRequest.newBuilder().setPingName(GRPC_TEST_PING_VALUE).setPingId(instanceId).build();
            StreamObserver<PingRequest> requestObserver = asyncNonBlockingStub.pingAsyncAsync(responseObserver);
            requestObserver.onNext(pingRequest);
            requestObserver.onNext(pingRequest);
            requestObserver.onCompleted();
            try {
                assertTrue(latch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException e) {
                LOG.debug("Interrupted while waiting for the response", e);
            }

            PongResponse pongResponse = responseObserver.getPongResponse();

            assertNotNull(pongResponse, "instanceId = " + instanceId);
            assertEquals(instanceId, pongResponse.getPongId());
            assertEquals(GRPC_USER_AGENT_PREFIX + instanceId, pongResponse.getPongName());

            asyncRequestChannel.shutdown().shutdownNow();
            return null;
        });
    }

    private void runConcurrent(Callable<?> task) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(CONCURRENT_THREAD_COUNT);
        try {
            List<Future<?>> futures = new ArrayList<>();
            for (int thread = 0; thread < CONCURRENT_THREAD_COUNT; thread++) {
                futures.add(executor.submit(() -> {
                    for (int round = 0; round < ROUNDS_PER_THREAD_COUNT; round++) {
                        task.call();
                    }
                    return null;
                }));
            }
            for (Future<?> future : futures) {
                future.get(1, TimeUnit.MINUTES);
            }
        } finally {
            executor.shutdownNow();
        }
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("grpc://localhost:0/org.apache.camel.component.grpc.PingPong?synchronous=true&consumerStrategy=AGGREGATION&hash=1")
                        .routeId("grpc-async")
                        .bean(new GrpcMessageBuilder(), "buildAsyncPongResponse");

                from("grpc://localhost:0/org.apache.camel.component.grpc.PingPong?synchronous=true&consumerStrategy=AGGREGATION&hash=2")
                        .routeId("grpc-headers")
                        .process(new HeaderExchangeProcessor());
            }
        };
    }

    public class PongResponseStreamObserver implements StreamObserver<PongResponse> {
        private PongResponse pongResponse;
        private final CountDownLatch latch;

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

        public PongResponse getPongResponse() {
            return pongResponse;
        }

        @Override
        public void onNext(PongResponse value) {
            pongResponse = value;
        }

        @Override
        public void onError(Throwable t) {
            LOG.info("Exception", t);
            latch.countDown();
        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    }

    static class GrpcMessageBuilder {
        public PongResponse buildAsyncPongResponse(List<PingRequest> pingRequests) {
            return PongResponse.newBuilder().setPongName(pingRequests.get(0).getPingName() + GRPC_TEST_PONG_VALUE)
                    .setPongId(pingRequests.get(0).getPingId()).build();
        }
    }

    static class HeaderExchangeProcessor implements Processor {

        @Override
        @SuppressWarnings("unchecked")
        public void process(Exchange exchange) {
            List<PingRequest> pingRequests = (List<PingRequest>) exchange.getIn().getBody();
            String userAgentName = (String) exchange.getIn().getHeader(GrpcConstants.GRPC_USER_AGENT_HEADER);

            // As user agent name is prepended the library's user agent
            // information it's necessary to extract this value (before first
            // space)
            PongResponse pongResponse
                    = PongResponse.newBuilder().setPongName(userAgentName.substring(0, userAgentName.indexOf(' ')))
                            .setPongId(pingRequests.get(0).getPingId())
                            .build();
            exchange.getIn().setBody(pongResponse);
        }
    }
}
