summaryrefslogtreecommitdiffstats
path: root/service-monitor/src/test/java/com/yahoo/vespa/service/model/ServiceModelCacheTest.java
blob: 2d6921df3743c9677716767c5cc832bf4c388a27 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.service.model;

import com.yahoo.jdisc.Timer;
import com.yahoo.vespa.service.monitor.ServiceModel;
import org.junit.Test;

import java.util.function.Supplier;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ServiceModelCacheTest {
    @SuppressWarnings("unchecked")
    private final Supplier<ServiceModel> rawSupplier = mock(Supplier.class);
    private final Timer timer = mock(Timer.class);
    private final ServiceModelCache cache = new ServiceModelCache(rawSupplier, timer);

    @Test
    public void sanityCheck() {
        ServiceModel serviceModel = mock(ServiceModel.class);
        when(rawSupplier.get()).thenReturn(serviceModel);

        long timeMillis = 0;
        when(timer.currentTimeMillis()).thenReturn(timeMillis);

        // Will always populate cache the first time
        ServiceModel actualServiceModel = cache.get();
        assertTrue(actualServiceModel == serviceModel);
        verify(rawSupplier, times(1)).get();

        // Cache hit
        timeMillis += ServiceModelCache.EXPIRY_MILLIS / 2;
        when(timer.currentTimeMillis()).thenReturn(timeMillis);
        actualServiceModel = cache.get();
        assertTrue(actualServiceModel == serviceModel);

        // Cache expired
        timeMillis += ServiceModelCache.EXPIRY_MILLIS + 1;
        when(timer.currentTimeMillis()).thenReturn(timeMillis);

        ServiceModel serviceModel2 = mock(ServiceModel.class);
        when(rawSupplier.get()).thenReturn(serviceModel2);

        actualServiceModel = cache.get();
        assertTrue(actualServiceModel == serviceModel2);
        // '2' because it's cumulative with '1' from the first times(1).
        verify(rawSupplier, times(2)).get();

        // Cache hit #2
        timeMillis += 1;
        when(timer.currentTimeMillis()).thenReturn(timeMillis);
        actualServiceModel = cache.get();
        assertTrue(actualServiceModel == serviceModel2);
    }
}