aboutsummaryrefslogtreecommitdiffstats
path: root/routing-generator/src/test/java/com/yahoo/vespa/hosted/routing/RoutingGeneratorTest.java
blob: 543a3b0a4e890d4d3ff0a6df20b8b1547ab02ea5 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.routing;

import com.yahoo.cloud.config.LbServicesConfig;
import com.yahoo.config.ConfigInstance;
import com.yahoo.config.subscription.ConfigSet;
import com.yahoo.test.ManualClock;
import com.yahoo.vespa.config.ConfigKey;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * @author mpolden
 */
public class RoutingGeneratorTest {

    @Test(timeout = 2000)
    public void config_subscription() {
        RouterMock router = new RouterMock();
        RoutingGenerator generator = new RoutingGenerator(new ConfigSetMock(), router, new ManualClock());
        try {
            router.awaitLoad(generator);
            assertNotNull("Router loads table", router.currentTable);
            assertEquals("Routing generator and router has same table",
                         generator.routingTable().get(),
                         router.currentTable);
        } finally {
            generator.deconstruct();
        }
    }

    private static class RouterMock implements Router {
        private volatile RoutingTable currentTable = null;

        @Override
        public void load(RoutingTable table) {
            currentTable = table;
        }

        public void awaitLoad(RoutingGenerator generator) {
            AtomicBoolean completed = new AtomicBoolean(false);
            while (!completed.get()) {
                generator.routingTable().ifPresent(table -> completed.set(table == currentTable));
                if (!completed.get()) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }

    }

    private static class ConfigSetMock extends ConfigSet {

        private int attempt = 0;

        public ConfigSetMock() {
            addBuilder("*", new LbServicesConfig.Builder());
        }

        @Override
        public ConfigInstance.Builder get(ConfigKey<?> key) {
            if (++attempt <= 5) {
                throw new RuntimeException("Failed to get config on attempt " + attempt);
            }
            return super.get(key);
        }

    }

}