aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client/src/main/java/ai/vespa/feed/client/impl/DryrunCluster.java
blob: 96cf7998681d431513e15da472617c3dcca42a1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client.impl;

import ai.vespa.feed.client.HttpResponse;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Dryrun implementation that reports every request/operation as successful
 *
 * @author bjorncs
 */
class DryrunCluster implements Cluster {

    private final static Logger log = Logger.getLogger(DryrunCluster.class.getName());

    static final Duration DELAY = Duration.ofMillis(1);

    @Override
    public void dispatch(HttpRequest request, CompletableFuture<HttpResponse> vessel) {
        long millis = DELAY.toMillis();
        log.log(Level.FINE, "Dryrun of request '{0}' with delay of {1}ms", new Object[]{request, millis});
        if (millis > 0) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                vessel.cancel(true);
                Thread.currentThread().interrupt();
                return;
            }
        }
        vessel.complete(new SimpleOkResponse());
    }

    private static class SimpleOkResponse implements HttpResponse {
        @Override public int code() { return 200; }
        @Override public byte[] body() { return "{\"message\":\"dummy dryrun message\"}".getBytes(StandardCharsets.UTF_8); }
    }

}