aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/di/componentgraph/core/ReuseComponentsTest.java
blob: 59c939863ddb9c668ef938ac074cc0146c1b0777 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di.componentgraph.core;

import com.yahoo.component.ComponentId;
import com.yahoo.component.provider.ComponentRegistry;
import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.config.test.TestConfig;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.ComponentTakingAllSimpleComponents;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.ComponentTakingComponent;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.ComponentTakingConfig;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.ComponentTakingConfigAndComponent;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.ComponentTakingExecutor;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.ExecutorProvider;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.SimpleComponent;
import com.yahoo.container.di.componentgraph.core.ComponentGraphTest.SimpleComponent2;
import com.yahoo.vespa.config.ConfigKey;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author gjoranv
 * @author Tony Vaagenes
 * @author ollivir
 */
public class ReuseComponentsTest {
    @Test
    void require_that_component_is_reused_when_componentNode_is_unmodified() {
        reuseAndTest(SimpleComponent.class, SimpleComponent.class);
        reuseAndTest(ExecutorProvider.class, Executor.class);
    }

    private <T> void reuseAndTest(Class<?> classToRegister, Class<T> classToLookup) {
        ComponentGraph graph = buildGraphAndSetNoConfigs(classToRegister);
        T instance = getComponent(graph, classToLookup);

        ComponentGraph newGraph = buildGraphAndSetNoConfigs(classToRegister);
        newGraph.reuseNodes(graph);
        T instance2 = getComponent(newGraph, classToLookup);

        assertSame(instance2, instance);
    }

    @Test
    void require_that_component_is_not_reused_when_class_is_changed() {
        assertThrows(IllegalStateException.class, () -> {
            ComponentGraph graph = buildGraphAndSetNoConfigs(SimpleComponent.class);
            SimpleComponent instance = getComponent(graph, SimpleComponent.class);

            ComponentGraph newGraph = buildGraphAndSetNoConfigs(SimpleComponent2.class);
            newGraph.reuseNodes(graph);
            SimpleComponent2 instance2 = getComponent(newGraph, SimpleComponent2.class);

            assertEquals(instance2.getId(), instance.getId());
            @SuppressWarnings("unused")
            SimpleComponent throwsException = getComponent(newGraph, SimpleComponent.class);
        });
    }

    @SuppressWarnings("deprecation")
    @Test
    void require_that_component_is_not_reused_when_config_is_changed() {
        Class<ComponentTakingConfig> componentClass = ComponentTakingConfig.class;

        ComponentGraph graph = buildGraph(componentClass);
        graph.setAvailableConfigs(Collections.singletonMap(new ConfigKey<>(TestConfig.class, "component"),
                ConfigGetter.getConfig(TestConfig.class, "raw: stringVal \"oldConfig\"")));
        ComponentTakingConfig instance = getComponent(graph, componentClass);

        ComponentGraph newGraph = buildGraph(componentClass);
        newGraph.setAvailableConfigs(Collections.singletonMap(new ConfigKey<>(TestConfig.class, "component"),
                ConfigGetter.getConfig(TestConfig.class, "raw: stringVal \"newConfig\"")));
        newGraph.reuseNodes(graph);
        ComponentTakingConfig instance2 = getComponent(newGraph, componentClass);

        assertNotSame(instance2, instance);
    }

    @SuppressWarnings("deprecation")
    @Test
    void require_that_component_is_not_reused_when_injected_component_is_changed() {
        Function<String, ComponentGraph> buildGraph = config -> {
            ComponentGraph graph = new ComponentGraph();

            ComponentNode rootComponent = mockComponentNode(ComponentTakingComponent.class, "root_component");

            String configId = "componentTakingConfigId";
            ComponentNode injectedComponent = mockComponentNode(ComponentTakingConfig.class, "injected_component", configId);

            rootComponent.inject(injectedComponent);

            graph.add(rootComponent);
            graph.add(injectedComponent);

            graph.complete();
            graph.setAvailableConfigs(Collections.singletonMap(new ConfigKey<>(TestConfig.class, configId),
                    ConfigGetter.getConfig(TestConfig.class, "raw: stringVal \"" + config + "\"")));

            return graph;
        };

        ComponentGraph oldGraph = buildGraph.apply("oldGraph");
        ComponentTakingComponent oldInstance = getComponent(oldGraph, ComponentTakingComponent.class);

        ComponentGraph newGraph = buildGraph.apply("newGraph");
        newGraph.reuseNodes(oldGraph);
        ComponentTakingComponent newInstance = getComponent(newGraph, ComponentTakingComponent.class);

        assertNotSame(newInstance, oldInstance);
    }

    @Test
    void require_that_component_is_not_reused_when_injected_component_registry_has_one_component_removed() {
        Function<Boolean, ComponentGraph> buildGraph = useBothInjectedComponents -> {
            ComponentGraph graph = new ComponentGraph();
            graph.add(mockComponentNode(ComponentTakingAllSimpleComponents.class, "root_component"));

            /* Below if-else has code duplication, but explicit ordering of the two components
             * was necessary to reproduce erroneous behaviour in ComponentGraph.reuseNodes that
             * occurred before ComponentRegistryNode got its own 'equals' implementation.
             */
            if (useBothInjectedComponents) {
                graph.add(mockComponentNode(SimpleComponent.class, "injected_component2"));
                graph.add(mockComponentNode(SimpleComponent.class, "injected_component1"));
            } else {
                graph.add(mockComponentNode(SimpleComponent.class, "injected_component1"));
            }

            graph.complete();
            graph.setAvailableConfigs(Collections.emptyMap());
            return graph;
        };

        ComponentGraph oldGraph = buildGraph.apply(true);
        ComponentRegistry<SimpleComponent> oldSimpleComponentRegistry = getComponent(oldGraph, ComponentTakingAllSimpleComponents.class).simpleComponents;

        ComponentGraph newGraph = buildGraph.apply(false);
        newGraph.reuseNodes(oldGraph);
        ComponentRegistry<SimpleComponent> newSimpleComponentRegistry = getComponent(newGraph, ComponentTakingAllSimpleComponents.class).simpleComponents;

        assertNotSame(newSimpleComponentRegistry, oldSimpleComponentRegistry);
    }

    @SuppressWarnings("deprecation")
    @Test
    void require_that_injected_component_is_reused_even_when_dependent_component_is_changed() {
        Function<String, ComponentGraph> buildGraph = config -> {
            ComponentGraph graph = new ComponentGraph();

            String configId = "componentTakingConfigAndComponent";
            ComponentNode rootComponent = mockComponentNode(ComponentTakingConfigAndComponent.class, "root_component", configId);

            ComponentNode injectedComponent = mockComponentNode(SimpleComponent.class, "injected_component");

            rootComponent.inject(injectedComponent);

            graph.add(rootComponent);
            graph.add(injectedComponent);

            graph.complete();
            graph.setAvailableConfigs(Collections.singletonMap(new ConfigKey<>(TestConfig.class, configId),
                    ConfigGetter.getConfig(TestConfig.class, "raw: stringVal \"" + config + "\"")));

            return graph;
        };

        ComponentGraph oldGraph = buildGraph.apply("oldGraph");
        SimpleComponent oldInjectedComponent = getComponent(oldGraph, SimpleComponent.class);
        ComponentTakingConfigAndComponent oldDependentComponent = getComponent(oldGraph, ComponentTakingConfigAndComponent.class);

        ComponentGraph newGraph = buildGraph.apply("newGraph");
        newGraph.reuseNodes(oldGraph);
        SimpleComponent newInjectedComponent = getComponent(newGraph, SimpleComponent.class);
        ComponentTakingConfigAndComponent newDependentComponent = getComponent(newGraph, ComponentTakingConfigAndComponent.class);

        assertNotSame(newDependentComponent, oldDependentComponent);
        assertSame(newInjectedComponent, oldInjectedComponent);
    }

    @Test
    void require_that_node_depending_on_guice_node_is_reused() {
        Supplier<ComponentGraph> makeGraph = () -> {
            ComponentGraph graph = new ComponentGraph();
            graph.add(mockComponentNode(ComponentTakingExecutor.class, "dummyId"));
            graph.complete(ComponentGraphTest.singletonExecutorInjector);
            graph.setAvailableConfigs(Collections.emptyMap());
            return graph;
        };

        Function<ComponentGraph, ComponentTakingExecutor> componentRetriever = graph -> getComponent(graph, ComponentTakingExecutor.class);

        ComponentGraph oldGraph = makeGraph.get();
        componentRetriever.apply(oldGraph);  // Ensure creation of GuiceNode
        ComponentGraph newGraph = makeGraph.get();
        newGraph.reuseNodes(oldGraph);
        assertSame(componentRetriever.apply(oldGraph), componentRetriever.apply(newGraph));
    }

    @Test
    void require_that_node_equals_only_checks_first_level_components_to_inject() {
        Function<String, Node> createNodeWithInjectedNodeWithInjectedNode = indirectlyInjectedComponentId -> {
            ComponentNode targetComponent = mockComponentNode(SimpleComponent.class, "target");
            ComponentNode directlyInjectedComponent = mockComponentNode(SimpleComponent.class, "directlyInjected");
            ComponentNode indirectlyInjectedComponent = mockComponentNode(SimpleComponent.class, indirectlyInjectedComponentId);
            directlyInjectedComponent.inject(indirectlyInjectedComponent);
            targetComponent.inject(directlyInjectedComponent);

            completeNode(targetComponent);
            completeNode(directlyInjectedComponent);
            completeNode(indirectlyInjectedComponent);

            return targetComponent;
        };

        Node targetNode1 = createNodeWithInjectedNodeWithInjectedNode.apply("indirectlyInjected_1");
        Node targetNode2 = createNodeWithInjectedNodeWithInjectedNode.apply("indirectlyInjected_2");
        assertEquals(targetNode1, targetNode2);
    }

    private void completeNode(ComponentNode node) {
        node.setArguments(new Object[0]);
        node.setAvailableConfigs(Collections.emptyMap());
    }

    private ComponentGraph buildGraph(Class<?> componentClass) {
        String commonComponentId = "component";
        ComponentGraph g = new ComponentGraph();
        g.add(mockComponentNode(componentClass, commonComponentId, commonComponentId));
        g.complete();
        return g;
    }

    private ComponentGraph buildGraphAndSetNoConfigs(Class<?> componentClass) {
        ComponentGraph g = buildGraph(componentClass);
        g.setAvailableConfigs(Collections.emptyMap());
        return g;
    }

    private static ComponentNode mockComponentNode(Class<?> clazz, String componentId, String configId) {
        return new ComponentNode(new ComponentId(componentId), configId, clazz);
    }

    private static ComponentNode mockComponentNode(Class<?> clazz, String componentId) {
        return mockComponentNode(clazz, componentId, "");
    }

    private static <T> T getComponent(ComponentGraph graph, Class<T> clazz) {
        return graph.getInstance(clazz);
    }
}