/*
 * 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.tinkerpop.gremlin.driver;

import org.junit.Test;

import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class ResultSetTest extends AbstractResultSetTest {

    @Test
    public void shouldHaveAllItemsAvailableAsynchronouslyOnReadComplete() throws InterruptedException {
        final CompletableFuture<Void> all = resultSet.allItemsAvailableAsync();
        assertThat(all.isDone(), is(false));
        resultSet.markComplete();
        // flush all tasks in pool
        pool.awaitTermination(2, TimeUnit.SECONDS);
        assertThat(all.isDone(), is(true));
    }

    @Test
    public void shouldHaveAllItemsAvailableAsynchronouslyOnReadCompleteExceptionally() throws InterruptedException {
        final CompletableFuture<Void> all = resultSet.allItemsAvailableAsync();
        assertThat(all.isDone(), is(false));
        resultSet.markError(new RuntimeException());
        // flush all tasks in pool
        pool.awaitTermination(2, TimeUnit.SECONDS);
        assertThat(all.isDone(), is(true));
        assertThat(all.isCompletedExceptionally(), is(true));
    }

    @Test
    public void shouldKeepFirstErrorWhenMarkedFailedTwice() throws InterruptedException {
        // Two threads can race to fail the same stream (e.g. a read-timeout on the event loop and end-of-stream on
        // the reader thread). The first error must win consistently across both the completed future and the error
        // observed via getAvailableItemCount(), so the recorded cause and the future's completion never disagree.
        final CompletableFuture<Void> all = resultSet.allItemsAvailableAsync();
        final RuntimeException first = new RuntimeException("first");
        final RuntimeException second = new RuntimeException("second");

        resultSet.markError(first);
        resultSet.markError(second);

        pool.awaitTermination(2, TimeUnit.SECONDS);
        assertThat(all.isCompletedExceptionally(), is(true));

        try {
            resultSet.getAvailableItemCount();
            fail("Expected the recorded error to be thrown");
        } catch (RuntimeException ex) {
            assertEquals(first, ex.getCause());
        }
    }

    @Test
    public void shouldHaveAllItemsAvailableOnReadComplete() throws InterruptedException {
        assertThat(resultSet.allItemsAvailable(), is(false));
        resultSet.markComplete();
        // flush all tasks in pool
        pool.awaitTermination(2, TimeUnit.SECONDS);
        assertThat(resultSet.allItemsAvailable(), is(true));
    }

    @Test
    public void shouldCompleteHeadersReceivedOnMarkHeadersReceived() throws Exception {
        final CompletableFuture<Void> headers = resultSet.headersReceivedAsync();
        assertThat(headers.isDone(), is(false));

        resultSet.markHeadersReceived();

        headers.get(2, TimeUnit.SECONDS);
        assertThat(headers.isDone(), is(true));
        assertThat(headers.isCompletedExceptionally(), is(false));
    }

    @Test
    public void shouldCompleteHeadersReceivedBeforeReadCompleted() throws Exception {
        // models the streaming path: headers decode first, then body chunks stream in, then read completes. A caller
        // blocked on headersReceivedAsync() must be released strictly before the full body is available.
        final CompletableFuture<Void> headers = resultSet.headersReceivedAsync();
        final CompletableFuture<Void> all = resultSet.allItemsAvailableAsync();
        assertThat(headers.isDone(), is(false));
        assertThat(all.isDone(), is(false));

        // headers arrive first
        resultSet.markHeadersReceived();
        headers.get(2, TimeUnit.SECONDS);
        assertThat(headers.isDone(), is(true));
        // the body is not finished yet, so the "all" future is still outstanding
        assertThat(all.isDone(), is(false));

        // body finishes streaming
        resultSet.add(new Result("test1"));
        resultSet.markComplete();
        all.get(2, TimeUnit.SECONDS);
        assertThat(all.isDone(), is(true));
    }

    @Test
    public void shouldCompleteHeadersReceivedAsBackstopOnMarkComplete() throws Exception {
        // models the non-streaming (aggregator) path which never signals headers explicitly: markComplete() must trip
        // the headers future too, otherwise a caller blocked on it would hang on that path.
        final CompletableFuture<Void> headers = resultSet.headersReceivedAsync();
        assertThat(headers.isDone(), is(false));

        resultSet.markComplete();

        headers.get(2, TimeUnit.SECONDS);
        assertThat(headers.isDone(), is(true));
        assertThat(headers.isCompletedExceptionally(), is(false));
    }

    @Test
    public void shouldCompleteHeadersReceivedExceptionallyOnErrorBeforeHeaders() throws Exception {
        // an error can arrive before any header was signaled (e.g. a write failure or a non-streaming error response);
        // the headers future must fail rather than hang so a caller blocked on it observes the error.
        final CompletableFuture<Void> headers = resultSet.headersReceivedAsync();
        assertThat(headers.isDone(), is(false));

        resultSet.markError(new RuntimeException("boom"));

        try {
            headers.get(2, TimeUnit.SECONDS);
            fail("headersReceivedAsync() should have completed exceptionally");
        } catch (ExecutionException ex) {
            assertThat(ex.getCause().getMessage(), is("boom"));
        }
        assertThat(headers.isDone(), is(true));
        assertThat(headers.isCompletedExceptionally(), is(true));
    }

    @Test
    public void shouldKeepHeadersReceivedSuccessfulWhenErrorArrivesAfterHeaders() throws Exception {
        // headers come back OK and only then does the body fail (e.g. a server-side error in the body footer). The
        // headers future already completed successfully and must not be flipped to a failure by the later error.
        final CompletableFuture<Void> headers = resultSet.headersReceivedAsync();
        resultSet.markHeadersReceived();
        headers.get(2, TimeUnit.SECONDS);
        assertThat(headers.isCompletedExceptionally(), is(false));

        resultSet.markError(new RuntimeException("late boom"));

        // still successfully completed - the late error does not retroactively fail the headers future
        assertThat(headers.isDone(), is(true));
        assertThat(headers.isCompletedExceptionally(), is(false));
    }

    @Test
    public void shouldHaveAllItemsAvailableAsynchronouslyOnReadCompleteWhileLoading() throws Exception {
        final CompletableFuture<Void> all = resultSet.allItemsAvailableAsync();
        assertThat(all.isDone(), is(false));

        final AtomicBoolean atLeastOnce = new AtomicBoolean(false);
        addToQueue(1000, 1, true, true);
        while (!resultSet.allItemsAvailable()) {
            atLeastOnce.set(true);
            if (!atLeastOnce.get())
                assertThat(all.isDone(), is(false));
        }

        assertThat(atLeastOnce.get(), is(true));

        // ensure there is enough time for the readComplete to complete the "all" future
        all.get(30000, TimeUnit.MILLISECONDS);
        assertThat(all.isDone(), is(true));
    }

    @Test
    public void shouldHaveAllItemsAvailableOnReadCompleteWhileLoading() throws Exception {
        assertThat(resultSet.allItemsAvailable(), is(false));

        final AtomicBoolean atLeastOnce = new AtomicBoolean(false);
        addToQueue(1000, 1, true, true);
        while (!resultSet.allItemsAvailable()) {
            atLeastOnce.set(true);
            if (!atLeastOnce.get())
                assertThat(resultSet.allItemsAvailable(), is(false));
        }

        assertThat(atLeastOnce.get(), is(true));
        assertThat(resultSet.allItemsAvailable(), is(true));
    }

    @Test
    public void shouldAwaitEverythingAndFlushOnMarkCompleted() throws Exception {
        final CompletableFuture<List<Result>> future = resultSet.some(4);
        resultSet.add(new Result("test1"));
        resultSet.add(new Result("test2"));
        resultSet.add(new Result("test3"));

        assertThat(future.isDone(), is(false));
        resultSet.markComplete();
        assertThat(future.isDone(), is(true));

        final List<Result> results = future.get();
        assertEquals("test1", results.get(0).getString());
        assertEquals("test2", results.get(1).getString());
        assertEquals("test3", results.get(2).getString());
        assertEquals(3, results.size());

        assertThat(resultSet.allItemsAvailable(), is(true));
        assertEquals(0, resultSet.getAvailableItemCount());
    }

    @Test
    public void shouldGetAllOnlyOnComplete() throws Exception {
        final CompletableFuture<List<Result>> future = resultSet.all();
        resultSet.add(new Result("test1"));
        resultSet.add(new Result("test2"));
        resultSet.add(new Result("test3"));

        assertThat(future.isDone(), is(false));
        resultSet.markComplete();

        final List<Result> results = future.get();
        assertEquals("test1", results.get(0).getString());
        assertEquals("test2", results.get(1).getString());
        assertEquals("test3", results.get(2).getString());
        assertEquals(3, results.size());

        assertThat(future.isDone(), is(true));
        assertThat(resultSet.allItemsAvailable(), is(true));
        assertEquals(0, resultSet.getAvailableItemCount());
    }

    @Test
    public void shouldIterate() throws Exception {
        final Iterator itty = resultSet.iterator();
        final AtomicInteger counter = new AtomicInteger(0);

        addToQueue(100, 1, true, true);

        while (itty.hasNext()) {
            itty.next();
            counter.incrementAndGet();
        }

        assertEquals(100, counter.get());
    }

    @Test
    public void shouldIterateWithoutCheckingHasNext() throws Exception {
        final Iterator itty = resultSet.iterator();
        final AtomicInteger counter = new AtomicInteger(0);

        addToQueue(100, 1, true, true);

        for (int ix = 0; ix < 100; ix++) {
            itty.next();
            counter.incrementAndGet();
        }

        assertEquals(100, counter.get());
        assertThat(itty.hasNext(), is(false));
    }

    @Test
    public void shouldIterateAndThenThrowIfCallToNextExceedsAvailableResultItems() throws Exception {
        final Iterator itty = resultSet.iterator();

        addToQueue(3, 1, true, true);

        assertThat(itty.hasNext(), is(true));
        itty.next();
        assertThat(itty.hasNext(), is(true));
        itty.next();
        itty.next();
        assertThat(itty.hasNext(), is(false));

        try {
            itty.next();
            fail("Should throw exception");
        } catch (NoSuchElementException ignore) {
            // this is the right path - an exception should toss here
        }
    }

    @Test
    public void shouldStream() throws Exception {
        final Stream<Result> stream = resultSet.stream();
        final AtomicInteger counter = new AtomicInteger(0);

        addToQueue(100, 1, true, true);

        stream.forEach(r -> counter.incrementAndGet());

        assertEquals(100, counter.get());
    }
    
    @Test
    public void shouldCallHasNextWithoutSideEffect() throws Exception {
        final Iterator itty = resultSet.iterator();
        final AtomicInteger counter = new AtomicInteger(0);

        addToQueue(100, 1, true, true);

        for (int i = 0; i < 101; i++) {
        	assertThat(itty.hasNext(), is(true));
        }
        
        while (itty.hasNext()) {
            itty.next();
            counter.incrementAndGet();
        }

        assertEquals(100, counter.get());
    }
}
