aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/MockConfigConvergenceChecker.java
blob: ad62bb4b616b33238b7a83f6e1330d59af5cc82b (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server;

import com.yahoo.config.model.api.ServiceInfo;
import com.yahoo.vespa.config.server.application.Application;
import com.yahoo.vespa.config.server.application.ConfigConvergenceChecker;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MockConfigConvergenceChecker extends ConfigConvergenceChecker {

    private final long wantedGeneration;
    private final List<ServiceInfo> servicesThatFailFirstIteration;

    private int iteration = 0;

    public MockConfigConvergenceChecker(long wantedGeneration) {
        this(wantedGeneration, List.of());
    }

    public MockConfigConvergenceChecker(long wantedGeneration, List<ServiceInfo> servicesThatFailFirstIteration) {
        this.wantedGeneration = wantedGeneration;
        this.servicesThatFailFirstIteration = servicesThatFailFirstIteration;
    }

    @Override
    public Map<ServiceInfo, Long> getServiceConfigGenerations(Application application, Duration timeoutPerService) {
        return Map.of();
    }

    @Override
    public ServiceListResponse checkConvergenceForAllServices(Application application, Duration timeoutPerService) {
        return new ServiceListResponse(Map.of(), wantedGeneration, wantedGeneration);
    }

    @Override
    public ServiceResponse getServiceConfigGeneration(Application application, String hostAndPortToCheck, Duration timeout) {
        return new ServiceResponse(ServiceResponse.Status.ok, wantedGeneration);
    }

    @Override
    public ServiceListResponse checkConvergenceUnlessDeferringChangesUntilRestart(Application application, Set<String> hostnames) {
        iteration++;
        if (servicesThatFailFirstIteration.isEmpty() || iteration > 1)
            return new ServiceListResponse(Map.of(), wantedGeneration, wantedGeneration);

        Map<ServiceInfo, Long> services = new HashMap<>();
        for (var service : servicesThatFailFirstIteration) {
            services.put(service, wantedGeneration - 1);
        }
        return new ServiceListResponse(services, wantedGeneration, wantedGeneration - 1);
    }

}