PageRenderTime 3296ms CodeModel.GetById 13ms RepoModel.GetById 6ms app.codeStats 0ms

/discovery/src/test/java/io/airlift/discovery/client/AbstractTestDiscoveryModule.java

https://gitlab.com/CORP-RESELLER/airlift
Java | 118 lines | 86 code | 14 blank | 18 comment | 0 complexity | f574605c18db9e86b76025758627c5e0 MD5 | raw file
  1. /*
  2. * Copyright 2010 Proofpoint, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.airlift.discovery.client;
  17. import com.google.common.collect.ImmutableMap;
  18. import com.google.inject.Binder;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Injector;
  21. import com.google.inject.Key;
  22. import com.google.inject.Module;
  23. import io.airlift.configuration.ConfigurationFactory;
  24. import io.airlift.configuration.ConfigurationModule;
  25. import io.airlift.json.JsonModule;
  26. import io.airlift.node.testing.TestingNodeModule;
  27. import org.testng.annotations.Test;
  28. import java.net.URI;
  29. import java.util.Map;
  30. import static com.google.common.base.Preconditions.checkNotNull;
  31. import static com.google.common.collect.Iterables.getOnlyElement;
  32. import static io.airlift.discovery.client.DiscoveryBinder.discoveryBinder;
  33. import static io.airlift.discovery.client.ServiceTypes.serviceType;
  34. import static org.testng.Assert.assertEquals;
  35. import static org.testng.Assert.assertNotNull;
  36. import static org.testng.Assert.assertTrue;
  37. public abstract class AbstractTestDiscoveryModule
  38. {
  39. private final Module discoveryModule;
  40. protected AbstractTestDiscoveryModule(Module discoveryModule)
  41. {
  42. this.discoveryModule = checkNotNull(discoveryModule, "discoveryModule is null");
  43. }
  44. @Test
  45. public void testBinding()
  46. throws Exception
  47. {
  48. Injector injector = Guice.createInjector(
  49. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.of("discovery.uri", "fake://server"))),
  50. new JsonModule(),
  51. new TestingNodeModule(),
  52. discoveryModule);
  53. // should produce a discovery announcement client and a lookup client
  54. assertNotNull(injector.getInstance(DiscoveryAnnouncementClient.class));
  55. assertNotNull(injector.getInstance(DiscoveryLookupClient.class));
  56. // should produce an Announcer
  57. assertNotNull(injector.getInstance(Announcer.class));
  58. // should produce a ServiceSelectorManager
  59. assertNotNull(injector.getInstance(ServiceSelectorManager.class));
  60. }
  61. @Test
  62. public void testMerging()
  63. throws Exception
  64. {
  65. final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl(
  66. URI.create("http://127.0.0.1:4444"),
  67. URI.create("http://example.com:4444"),
  68. null,
  69. null);
  70. Map<String, String> config = ImmutableMap.<String, String>builder()
  71. .put("discovery.uri", "fake://server")
  72. .put("discovery.carrot.pool", "test")
  73. .build();
  74. Injector injector = Guice.createInjector(
  75. new ConfigurationModule(new ConfigurationFactory(config)),
  76. new JsonModule(),
  77. new TestingNodeModule(),
  78. new DiscoveryModule(),
  79. new Module()
  80. {
  81. @Override
  82. public void configure(Binder binder)
  83. {
  84. binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo);
  85. discoveryBinder(binder).bindHttpAnnouncement("apple");
  86. discoveryBinder(binder).bindHttpAnnouncement("banana");
  87. discoveryBinder(binder).bindHttpAnnouncement("carrot");
  88. discoveryBinder(binder).bindHttpSelector("apple");
  89. discoveryBinder(binder).bindHttpSelector("banana");
  90. discoveryBinder(binder).bindHttpSelector("carrot");
  91. discoveryBinder(binder).bindHttpSelector("grape");
  92. }
  93. }
  94. );
  95. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  96. assertEquals(getOnlyElement(selector.selectHttpService()), URI.create("http://127.0.0.1:4444"));
  97. selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("banana")));
  98. assertEquals(getOnlyElement(selector.selectHttpService()), URI.create("http://127.0.0.1:4444"));
  99. selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("carrot")));
  100. assertTrue(selector.selectHttpService().isEmpty());
  101. selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("grape")));
  102. assertTrue(selector.selectHttpService().isEmpty());
  103. }
  104. }