suneet-s commented on a change in pull request #10091: URL: https://github.com/apache/druid/pull/10091#discussion_r447322462
########## File path: services/src/test/java/org/apache/druid/cli/DiscoverySideEffectsProviderTest.java ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.cli; + +import com.google.common.collect.ImmutableList; +import com.google.inject.Injector; +import org.apache.druid.curator.discovery.ServiceAnnouncer; +import org.apache.druid.discovery.DiscoveryDruidNode; +import org.apache.druid.discovery.DruidNodeAnnouncer; +import org.apache.druid.discovery.DruidService; +import org.apache.druid.discovery.NodeRole; +import org.apache.druid.java.util.common.lifecycle.Lifecycle; +import org.apache.druid.server.DruidNode; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(MockitoJUnitRunner.class) +public class DiscoverySideEffectsProviderTest +{ + private static final boolean USE_LEGACY_ANNOUNCER = true; + + private NodeRole nodeRole; + @Mock + private DruidNode druidNode; + @Mock + private DruidNodeAnnouncer announcer; + @Mock + private ServiceAnnouncer legacyAnnouncer; + @Mock + private Lifecycle lifecycle; + @Mock + private Injector injector; + private List<Lifecycle.Handler> lifecycleHandlers; + + private ServerRunnable.DiscoverySideEffectsProvider target; + + @Before + public void setUp() + { + nodeRole = NodeRole.HISTORICAL; + lifecycleHandlers = new ArrayList<>(); + Mockito.when(injector.getInstance(DiscoverableDruidService.class)).thenReturn(new DiscoverableDruidService()); + Mockito.when(injector.getInstance(UnDiscoverableDruidService.class)).thenReturn(new UnDiscoverableDruidService()); + Mockito.doAnswer((invocation) -> { + DiscoveryDruidNode discoveryDruidNode = invocation.getArgument(0); + boolean isAllServicesDiscoverable = + discoveryDruidNode.getServices().values().stream().allMatch(DruidService::isDiscoverable); + Assert.assertTrue(isAllServicesDiscoverable); + return null; + }).when(announcer).announce(ArgumentMatchers.any(DiscoveryDruidNode.class)); + Mockito.doAnswer((invocation) -> lifecycleHandlers.add(invocation.getArgument(0))) + .when(lifecycle).addHandler( + ArgumentMatchers.any(Lifecycle.Handler.class), + ArgumentMatchers.eq(Lifecycle.Stage.ANNOUNCEMENTS) + ); + target = new ServerRunnable.DiscoverySideEffectsProvider( + nodeRole, + ImmutableList.of(DiscoverableDruidService.class, UnDiscoverableDruidService.class), + USE_LEGACY_ANNOUNCER, + druidNode, + announcer, + legacyAnnouncer, + lifecycle, + injector + ); + } + + @Test + public void testGetShouldAddAnnouncementsForDiscoverableServices() throws Exception + { + ServerRunnable.DiscoverySideEffectsProvider.Child child = target.get(); + Assert.assertNotNull(child); + Assert.assertEquals(1, lifecycleHandlers.size()); + lifecycleHandlers.get(0).start(); + } Review comment: Fair point. I wrote this so that the announcer is wired up to fail if it tries to announce an undiscoverable service. It was tricky to wire up the validation because `DiscoveryDruidNode` is instantiated when the provider is called and the Child object that is returned just appears to be an empty object. I couldn't think of a better way to wire up the validations without refactoring some production code. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
