aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/test/java/ai/vespa/metricsproxy/service/VespaServiceTest.java
blob: 54004e7872a274ad22c19860c96485d7ba79437d (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
72
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.service;

import ai.vespa.metricsproxy.metric.Metrics;
import ai.vespa.metricsproxy.metric.model.MetricId;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static ai.vespa.metricsproxy.TestUtil.getFileContents;
import static ai.vespa.metricsproxy.service.RemoteMetricsFetcher.METRICS_PATH;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author Unknown
 */
public class VespaServiceTest {
    private MockHttpServer httpServer;
    private static final String response;

    static {
        response = getFileContents("metrics-state.json");
        HttpMetricFetcher.CONNECTION_TIMEOUT = 60000; // 60 secs in unit tests
    }

    @Before
    public void setupHTTPServer() {
        try {
            httpServer = new MockHttpServer(response, METRICS_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testService() {
        VespaService service = new VespaService("qrserver", "container/qrserver.0");
        assertThat(service.getServiceName(), is("qrserver"));
        assertThat(service.getInstanceName(), is("qrserver"));
        assertThat(service.getPid(), is(-1));
        assertThat(service.getConfigId(), is("container/qrserver.0"));


        service = VespaService.create("qrserver2", "container/qrserver.0", -1);
        assertThat(service.getServiceName(), is("qrserver"));
        assertThat(service.getInstanceName(), is("qrserver2"));
        assertThat(service.getPid(), is(-1));
        assertThat(service.getConfigId(), is("container/qrserver.0"));
    }

    @Test
    // TODO: Make it possible to test this without running a HTTP server to create the response
    public void testMetricsFetching() {
        VespaService service = VespaService.create("service1", "id", httpServer.port());
        Metrics metrics = service.getMetrics();
        assertThat(metrics.getMetric(MetricId.toMetricId("queries.count")).getValue().intValue(), is(28));

        // Shutdown server and check that no metrics are returned (should use empty metrics
        // when unable to fetch new metrics)
        shutdown();

        metrics = service.getMetrics();
        assertThat(metrics.size(), is(0));
    }

    @After
    public void shutdown() {
        this.httpServer.close();
    }

}