PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/vectorci/airlift
Java | 220 lines | 180 code | 25 blank | 15 comment | 0 complexity | d08f9ba44adc4086c99461c5ad50a90c 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.common.collect.ImmutableSet;
  19. import com.google.inject.Binder;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. import com.google.inject.Key;
  23. import com.google.inject.Module;
  24. import com.google.inject.Provider;
  25. import com.google.inject.TypeLiteral;
  26. import io.airlift.configuration.ConfigurationFactory;
  27. import io.airlift.configuration.ConfigurationModule;
  28. import io.airlift.discovery.client.testing.TestingDiscoveryModule;
  29. import io.airlift.node.testing.TestingNodeModule;
  30. import org.testng.Assert;
  31. import org.testng.annotations.Test;
  32. import java.util.Map;
  33. import java.util.Set;
  34. import static io.airlift.discovery.client.DiscoveryBinder.discoveryBinder;
  35. import static io.airlift.discovery.client.ServiceTypes.serviceType;
  36. public class TestDiscoveryBinder
  37. {
  38. @Test
  39. public void testBindSelector()
  40. throws Exception
  41. {
  42. Injector injector = Guice.createInjector(
  43. new TestModule(),
  44. new Module()
  45. {
  46. @Override
  47. public void configure(Binder binder)
  48. {
  49. discoveryBinder(binder).bindServiceAnnouncement(new ServiceAnnouncementProvider().get());
  50. }
  51. }
  52. );
  53. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>() { }));
  54. Assert.assertEquals(announcements, ImmutableSet.of(ANNOUNCEMENT));
  55. }
  56. @Test
  57. public void testBindSelectorProviderClass()
  58. throws Exception
  59. {
  60. Injector injector = Guice.createInjector(
  61. new TestModule(),
  62. new Module()
  63. {
  64. @Override
  65. public void configure(Binder binder)
  66. {
  67. discoveryBinder(binder).bindServiceAnnouncement(ServiceAnnouncementProvider.class);
  68. }
  69. }
  70. );
  71. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>() { }));
  72. Assert.assertEquals(announcements, ImmutableSet.of(ANNOUNCEMENT));
  73. }
  74. @Test
  75. public void testBindSelectorProviderInstance()
  76. throws Exception
  77. {
  78. Injector injector = Guice.createInjector(
  79. new TestModule(),
  80. new Module()
  81. {
  82. @Override
  83. public void configure(Binder binder)
  84. {
  85. discoveryBinder(binder).bindServiceAnnouncement(new ServiceAnnouncementProvider());
  86. }
  87. }
  88. );
  89. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>() { }));
  90. Assert.assertEquals(announcements, ImmutableSet.of(ANNOUNCEMENT));
  91. }
  92. @Test
  93. public void testBindSelectorString()
  94. throws Exception
  95. {
  96. Injector injector = Guice.createInjector(
  97. new TestModule(),
  98. new Module()
  99. {
  100. @Override
  101. public void configure(Binder binder)
  102. {
  103. discoveryBinder(binder).bindSelector("apple");
  104. }
  105. }
  106. );
  107. assertCanCreateServiceSelector(injector, "apple", ServiceSelectorConfig.DEFAULT_POOL);
  108. }
  109. @Test
  110. public void testBindSelectorAnnotation()
  111. throws Exception
  112. {
  113. Injector injector = Guice.createInjector(
  114. new TestModule(),
  115. new Module()
  116. {
  117. @Override
  118. public void configure(Binder binder)
  119. {
  120. discoveryBinder(binder).bindSelector(serviceType("apple"));
  121. }
  122. }
  123. );
  124. assertCanCreateServiceSelector(injector, "apple", ServiceSelectorConfig.DEFAULT_POOL);
  125. }
  126. @Test
  127. public void testBindSelectorStringWithPool()
  128. throws Exception
  129. {
  130. Injector injector = Guice.createInjector(
  131. new TestModule(ImmutableMap.of("discovery.apple.pool", "apple-pool")),
  132. new Module()
  133. {
  134. @Override
  135. public void configure(Binder binder)
  136. {
  137. discoveryBinder(binder).bindSelector("apple");
  138. }
  139. }
  140. );
  141. assertCanCreateServiceSelector(injector, "apple", "apple-pool");
  142. }
  143. @Test
  144. public void testBindSelectorAnnotationWithPool()
  145. throws Exception
  146. {
  147. Injector injector = Guice.createInjector(
  148. new TestModule(ImmutableMap.of("discovery.apple.pool", "apple-pool")),
  149. new Module()
  150. {
  151. @Override
  152. public void configure(Binder binder)
  153. {
  154. discoveryBinder(binder).bindSelector(serviceType("apple"));
  155. }
  156. }
  157. );
  158. assertCanCreateServiceSelector(injector, "apple", "apple-pool");
  159. }
  160. private void assertCanCreateServiceSelector(Injector injector, String expectedType, String expectedPool)
  161. {
  162. ServiceSelector actualServiceSelector = injector.getInstance(Key.get(ServiceSelector.class, serviceType(expectedType)));
  163. Assert.assertNotNull(actualServiceSelector);
  164. Assert.assertEquals(actualServiceSelector.getType(), expectedType);
  165. Assert.assertEquals(actualServiceSelector.getPool(), expectedPool);
  166. }
  167. private static class TestModule implements Module
  168. {
  169. private Map<String,String> configProperties;
  170. private TestModule()
  171. {
  172. configProperties = ImmutableMap.of();
  173. }
  174. private TestModule(Map<String, String> configProperties)
  175. {
  176. this.configProperties = ImmutableMap.copyOf(configProperties);
  177. }
  178. @Override
  179. public void configure(Binder binder)
  180. {
  181. binder.install(new ConfigurationModule(new ConfigurationFactory(configProperties)));
  182. binder.install(new TestingNodeModule());
  183. binder.install(new TestingDiscoveryModule());
  184. }
  185. }
  186. private static final ServiceAnnouncement ANNOUNCEMENT = ServiceAnnouncement.serviceAnnouncement("apple").addProperty("a", "apple").build();
  187. private static class ServiceAnnouncementProvider implements Provider<ServiceAnnouncement>
  188. {
  189. @Override
  190. public ServiceAnnouncement get()
  191. {
  192. return ANNOUNCEMENT;
  193. }
  194. }
  195. }