aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/TargetCachingSlobrokHostFetcherTest.java
blob: f330f110241f489b698b1574f677ae6eca11f815 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol;

import com.yahoo.jrt.slobrok.api.IMirror;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.messagebus.routing.RoutingContext;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * @author vekterli
 */
public class TargetCachingSlobrokHostFetcherTest {

    private static String idOfIndex(int index) {
        return String.format("storage/cluster.foo/distributor/%d/default", index);
    }

    private static String idOfWildcardLookup() {
        return "storage/cluster.foo/distributor/*/default";
    }

    private static String lookupSpecOfIndex(int index) {
        return String.format("tcp/localhost:%d", index);
    }

    private static String resolvedSpecOfIndex(int index) {
        return String.format("tcp/localhost:%d/default", index);
    }

    private static List<Mirror.Entry> dummyEntries(int... indices) {
        return Arrays.stream(indices)
                .mapToObj(index -> new Mirror.Entry(idOfIndex(index), lookupSpecOfIndex(index))).toList();
    }

    static class Fixture {
        SlobrokPolicy mockSlobrokPolicy = mock(SlobrokPolicy.class);
        IMirror mockMirror = mock(IMirror.class);
        ContentPolicy.SlobrokHostPatternGenerator patternGenerator = new ContentPolicy.SlobrokHostPatternGenerator("foo");
        ContentPolicy.TargetCachingSlobrokHostFetcher hostFetcher = new ContentPolicy.TargetCachingSlobrokHostFetcher(patternGenerator, mockSlobrokPolicy, 60);
        RoutingContext routingContext = mock(RoutingContext.class);

        Fixture() {
            when(mockMirror.updates()).thenReturn(1);
            when(routingContext.getMirror()).thenReturn(mockMirror);
            when(mockSlobrokPolicy.lookup(any(), eq(idOfIndex(1)))).thenReturn(dummyEntries(1));
            when(mockSlobrokPolicy.lookup(any(), eq(idOfIndex(2)))).thenReturn(dummyEntries(2));
            when(mockSlobrokPolicy.lookup(any(), eq(idOfWildcardLookup()))).thenReturn(dummyEntries(1, 2, 3, 4));
        }
    }

    @Test
    public void lookup_passed_through_on_first_fetch() {
        Fixture fixture = new Fixture();

        String spec = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);
        assertEquals(resolvedSpecOfIndex(1), spec);
        verify(fixture.mockSlobrokPolicy, times(1)).lookup(any(), eq(idOfIndex(1)));
    }

    @Test
    public void cached_index_does_not_do_slobrok_lookup() {
        Fixture fixture = new Fixture();

        String spec1 = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);
        String spec2 = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);
        assertEquals(spec1, spec2);
        // Only invoked once
        verify(fixture.mockSlobrokPolicy, times(1)).lookup(any(), anyString());
    }

    @Test
    public void multiple_indexes_are_cached() {
        Fixture fixture = new Fixture();

        String spec1_1 = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);
        String spec2_1 = fixture.hostFetcher.getTargetSpec(2, fixture.routingContext);

        assertEquals(resolvedSpecOfIndex(1), spec1_1);
        assertEquals(resolvedSpecOfIndex(2), spec2_1);

        String spec1_2 = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);
        String spec2_2 = fixture.hostFetcher.getTargetSpec(2, fixture.routingContext);
        assertEquals(spec1_1, spec1_2);
        assertEquals(spec2_1, spec2_2);

        verify(fixture.mockSlobrokPolicy, times(1)).lookup(any(), eq(idOfIndex(1)));
        verify(fixture.mockSlobrokPolicy, times(1)).lookup(any(), eq(idOfIndex(2)));
    }

    @Test
    public void generation_change_evicts_cache() {
        Fixture fixture = new Fixture();

        when(fixture.mockMirror.updates()).thenReturn(1).thenReturn(2);
        when(fixture.mockSlobrokPolicy.lookup(any(), eq(idOfIndex(1))))
                .thenReturn(dummyEntries(1)).thenReturn(dummyEntries(2));

        String spec1 = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);
        String spec2 = fixture.hostFetcher.getTargetSpec(1, fixture.routingContext);

        assertEquals(resolvedSpecOfIndex(1), spec1);
        assertEquals(resolvedSpecOfIndex(2), spec2);
    }

    @Test
    public void wildcard_null_distributor_index_is_not_cached() {
        Fixture fixture = new Fixture();

        String spec = fixture.hostFetcher.getTargetSpec(null, fixture.routingContext);
        assertNotNull(spec);
        spec = fixture.hostFetcher.getTargetSpec(null, fixture.routingContext);
        assertNotNull(spec);

        verify(fixture.mockSlobrokPolicy, times(2)).lookup(any(), eq(idOfWildcardLookup()));
    }

}