aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTestBase.java
blob: 56a6d49f398e052a7d801cd1a9f7819492974724 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.state;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.container.core.ApplicationMetadataConfig;
import com.yahoo.container.jdisc.RequestHandlerTestDriver;
import com.yahoo.container.jdisc.config.HealthMonitorConfig;
import com.yahoo.jdisc.Timer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;


/**
 * @author gjoranv
 */
public class StateHandlerTestBase {
    final static long SNAPSHOT_INTERVAL = TimeUnit.SECONDS.toMillis(300);
    final static long META_GENERATION = 69;

    static final String URI_BASE = "http://localhost";

    static StateMonitor monitor;
    static RequestHandlerTestDriver testDriver;

    static HealthMonitorConfig healthMonitorConfig;
    static ApplicationMetadataConfig applicationMetadataConfig;
    static MetricsPacketsHandlerConfig metricsPacketsHandlerConfig;

    final AtomicLong currentTimeMillis = new AtomicLong(0);
    Timer timer;

    MockSnapshotProvider snapshotProvider;
    ComponentRegistry<SnapshotProvider> snapshotProviderRegistry;

    @BeforeAll
    public static void setupClass() {
        healthMonitorConfig = new HealthMonitorConfig(new HealthMonitorConfig.Builder()
                                                              .initialStatus("up"));
        applicationMetadataConfig = new ApplicationMetadataConfig(new ApplicationMetadataConfig.Builder()
                                                                          .generation(META_GENERATION));
    }

    @BeforeEach
    public void setupSnapshotProvider() {
        timer = currentTimeMillis::get;
        snapshotProvider = new MockSnapshotProvider();
        snapshotProviderRegistry = new ComponentRegistry<>();
        snapshotProviderRegistry.register(new ComponentId("foo"), snapshotProvider);
        monitor = new StateMonitor(healthMonitorConfig);
    }

    String requestAsString(String requestUri) {
        return testDriver.sendRequest(requestUri).readAll();
    }

    JsonNode requestAsJson(String requestUri) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readTree(mapper.getFactory().createParser(requestAsString(requestUri)));
    }

    void advanceToNextSnapshot() {
        currentTimeMillis.addAndGet(SNAPSHOT_INTERVAL);
    }

}