// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.autoscale;

import com.yahoo.test.ManualClock;
import org.junit.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class NodeMetricsDbTest {

    @Test
    public void testNodeMetricsDb() {
        ManualClock clock = new ManualClock();
        NodeMetricsDb db = new NodeMetricsDb();
        List<NodeMetrics.MetricValue> values = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            values.add(new NodeMetrics.MetricValue("host0", "cpu.util", clock.instant().getEpochSecond(), 0.9f));
            clock.advance(Duration.ofHours(1));
        }
        db.add(values);

        assertEquals(29, db.getWindow(clock.instant().minus(Duration.ofHours(30)), Resource.cpu,    List.of("host0")).measurementCount());
        assertEquals( 0, db.getWindow(clock.instant().minus(Duration.ofHours(30)), Resource.memory, List.of("host0")).measurementCount());
        db.gc(clock);
        assertEquals(23, db.getWindow(clock.instant().minus(Duration.ofHours(30)), Resource.cpu,    List.of("host0")).measurementCount());
        assertEquals( 0, db.getWindow(clock.instant().minus(Duration.ofHours(30)), Resource.memory, List.of("host0")).measurementCount());
    }

}

@@ -245,16 +245,42 @@
 ration;%0A
+import java.time.Instant;%0A
 import j
@@ -299,16 +299,16 @@
 ayList;%0A
-
 import j
@@ -495,16 +495,50 @@
 alClock(
+Instant.ofEpochSecond(1596102538L)
 );%0A     
@@ -857,16 +857,16 @@
       %7D%0A
-
         
@@ -882,16 +882,182 @@
 lues);%0A%0A
+        // Avoid off-by-one bug when the below windows starts exactly on one of the above getEpochSecond() timestamps.%0A        clock.advance(Duration.ofMinutes(1));%0A%0A
         

Fix flaky NodeMetricsDbTest