/*
 * 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 com.facebook.presto.plugin.blackhole;

import com.facebook.presto.spi.ConnectorPageSink;
import com.facebook.presto.spi.Page;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import io.airlift.slice.Slice;
import io.airlift.units.Duration;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

import static io.airlift.concurrent.MoreFutures.toCompletableFuture;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

class BlackHolePageSink
        implements ConnectorPageSink
{
    private final ListeningScheduledExecutorService executorService;
    private final long pageProcessingDelayMillis;

    public BlackHolePageSink(ListeningScheduledExecutorService executorService, Duration pageProcessingDelay)
    {
        this.executorService = requireNonNull(executorService, "executorService is null");
        this.pageProcessingDelayMillis = requireNonNull(pageProcessingDelay, "pageProcessingDelay is null").toMillis();
    }

    @Override
    public CompletableFuture<?> appendPage(Page page)
    {
        if (pageProcessingDelayMillis > 0) {
            return toCompletableFuture(executorService.schedule(() -> null, pageProcessingDelayMillis, MILLISECONDS));
        }
        return NOT_BLOCKED;
    }

    @Override
    public CompletableFuture<Collection<Slice>> finish()
    {
        return completedFuture(ImmutableList.of());
    }

    @Override
    public void abort() {}
}

@@ -1078,78 +1078,8 @@
 ll;%0A
-import static java.util.concurrent.CompletableFuture.completedFuture;%0A
 impo
@@ -1196,16 +1196,148 @@
 eSink%0A%7B%0A
+    private static final CompletableFuture%3CCollection%3CSlice%3E%3E NON_BLOCKED = CompletableFuture.completedFuture(ImmutableList.of());%0A%0A
     priv
@@ -1446,16 +1446,93 @@
 yMillis;
+%0A    private CompletableFuture%3CCollection%3CSlice%3E%3E appendFuture = NON_BLOCKED;
 %0A%0A    pu
@@ -1925,32 +1925,181 @@
 age page)%0A    %7B%0A
+        appendFuture = scheduleAppend();%0A        return appendFuture;%0A    %7D%0A%0A    private CompletableFuture%3CCollection%3CSlice%3E%3E scheduleAppend()%0A    %7B%0A
         if (page
@@ -2197,20 +2197,34 @@
 e(() -%3E 
-null
+ImmutableList.of()
 , pagePr
@@ -2287,17 +2287,17 @@
 eturn NO
-T
+N
 _BLOCKED
@@ -2401,43 +2401,20 @@
 urn 
-completedFuture(ImmutableList.of())
+appendFuture
 ;%0A  

Fix BlackholePageSink