PageRenderTime 2920ms CodeModel.GetById 25ms RepoModel.GetById 2ms app.codeStats 1ms

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

https://gitlab.com/vectorci/airlift
Java | 194 lines | 156 code | 23 blank | 15 comment | 0 complexity | b0b3c5e0e28faa72665d3834be11a618 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.ImmutableList;
  18. import com.google.common.collect.ImmutableMap;
  19. import com.google.common.collect.ImmutableSet;
  20. import com.google.inject.Binder;
  21. import com.google.inject.Guice;
  22. import com.google.inject.Injector;
  23. import com.google.inject.Key;
  24. import com.google.inject.Module;
  25. import io.airlift.configuration.ConfigurationFactory;
  26. import io.airlift.configuration.ConfigurationModule;
  27. import io.airlift.discovery.client.testing.InMemoryDiscoveryClient;
  28. import io.airlift.discovery.client.testing.TestingDiscoveryModule;
  29. import io.airlift.node.testing.TestingNodeModule;
  30. import org.testng.annotations.Test;
  31. import java.net.URI;
  32. import static com.google.common.collect.Iterables.getOnlyElement;
  33. import static io.airlift.discovery.client.DiscoveryBinder.discoveryBinder;
  34. import static io.airlift.discovery.client.ServiceAnnouncement.serviceAnnouncement;
  35. import static io.airlift.discovery.client.ServiceTypes.serviceType;
  36. import static org.testng.Assert.assertEquals;
  37. public class TestHttpServiceSelectorBinder
  38. {
  39. @Test
  40. public void testHttpSelectorString()
  41. {
  42. Injector injector = Guice.createInjector(
  43. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())),
  44. new TestingNodeModule(),
  45. new TestingDiscoveryModule(),
  46. new Module()
  47. {
  48. @Override
  49. public void configure(Binder binder)
  50. {
  51. discoveryBinder(binder).bindHttpSelector("apple");
  52. }
  53. }
  54. );
  55. InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
  56. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", "fake://server-http").build()));
  57. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  58. assertEquals(getOnlyElement(selector.selectHttpService()), URI.create("fake://server-http"));
  59. }
  60. @Test
  61. public void testHttpSelectorAnnotation()
  62. {
  63. Injector injector = Guice.createInjector(
  64. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())),
  65. new TestingNodeModule(),
  66. new TestingDiscoveryModule(),
  67. new Module()
  68. {
  69. @Override
  70. public void configure(Binder binder)
  71. {
  72. discoveryBinder(binder).bindHttpSelector(serviceType("apple"));
  73. }
  74. }
  75. );
  76. InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
  77. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", "fake://server-http").build()));
  78. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  79. assertEquals(getOnlyElement(selector.selectHttpService()), URI.create("fake://server-http"));
  80. ServiceSelectorManager manager = injector.getInstance(ServiceSelectorManager.class);
  81. assertEquals(manager.getServiceSelectors().size(), 1);
  82. manager.attemptRefresh();
  83. manager.forceRefresh();
  84. }
  85. @Test
  86. public void testHttpsSelector()
  87. {
  88. Injector injector = Guice.createInjector(
  89. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())),
  90. new TestingNodeModule(),
  91. new TestingDiscoveryModule(),
  92. new Module()
  93. {
  94. @Override
  95. public void configure(Binder binder)
  96. {
  97. discoveryBinder(binder).bindHttpSelector("apple");
  98. }
  99. }
  100. );
  101. InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
  102. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("https", "fake://server-https").build()));
  103. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  104. assertEquals(getOnlyElement(selector.selectHttpService()), URI.create("fake://server-https"));
  105. }
  106. @Test
  107. public void testFavorHttpsOverHttpSelector()
  108. {
  109. Injector injector = Guice.createInjector(
  110. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())),
  111. new TestingNodeModule(),
  112. new TestingDiscoveryModule(),
  113. new Module()
  114. {
  115. @Override
  116. public void configure(Binder binder)
  117. {
  118. discoveryBinder(binder).bindHttpSelector("apple");
  119. }
  120. }
  121. );
  122. InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
  123. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", "fake://server-http").build(),
  124. serviceAnnouncement("apple").addProperty("https", "fake://server-https").build()));
  125. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  126. assertEquals(selector.selectHttpService(), ImmutableList.of(URI.create("fake://server-https"), URI.create("fake://server-http")));
  127. }
  128. @Test
  129. public void testNoHttpServices()
  130. {
  131. Injector injector = Guice.createInjector(
  132. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())),
  133. new TestingNodeModule(),
  134. new TestingDiscoveryModule(),
  135. new Module()
  136. {
  137. @Override
  138. public void configure(Binder binder)
  139. {
  140. discoveryBinder(binder).bindHttpSelector("apple");
  141. }
  142. }
  143. );
  144. InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
  145. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("foo", "fake://server-https").build()));
  146. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  147. assertEquals(selector.selectHttpService(), ImmutableList.of());
  148. }
  149. @Test
  150. public void testInvalidUris()
  151. {
  152. Injector injector = Guice.createInjector(
  153. new ConfigurationModule(new ConfigurationFactory(ImmutableMap.<String, String>of())),
  154. new TestingNodeModule(),
  155. new TestingDiscoveryModule(),
  156. new Module()
  157. {
  158. @Override
  159. public void configure(Binder binder)
  160. {
  161. discoveryBinder(binder).bindHttpSelector("apple");
  162. }
  163. }
  164. );
  165. InMemoryDiscoveryClient discoveryClient = injector.getInstance(InMemoryDiscoveryClient.class);
  166. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("http", ":::INVALID:::").build()));
  167. discoveryClient.announce(ImmutableSet.of(serviceAnnouncement("apple").addProperty("https", ":::INVALID:::").build()));
  168. HttpServiceSelector selector = injector.getInstance(Key.get(HttpServiceSelector.class, serviceType("apple")));
  169. assertEquals(selector.selectHttpService(), ImmutableList.of());
  170. }
  171. }