PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bootstrap/src/test/java/io/airlift/bootstrap/TestLifeCycleManager.java

https://gitlab.com/CORP-RESELLER/airlift
Java | 328 lines | 274 code | 37 blank | 17 comment | 0 complexity | 9ab882812d86115f6efa1fba20d7ad45 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.bootstrap;
  17. import com.google.common.collect.Sets;
  18. import com.google.inject.Binder;
  19. import com.google.inject.CreationException;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. import com.google.inject.Module;
  23. import com.google.inject.PrivateModule;
  24. import com.google.inject.Scopes;
  25. import com.google.inject.Stage;
  26. import org.testng.Assert;
  27. import org.testng.annotations.BeforeMethod;
  28. import org.testng.annotations.Test;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import java.util.Set;
  32. import java.util.concurrent.CopyOnWriteArrayList;
  33. public class TestLifeCycleManager
  34. {
  35. private final static List<String> stateLog = new CopyOnWriteArrayList<String>();
  36. @BeforeMethod
  37. public void setup()
  38. {
  39. stateLog.clear();
  40. }
  41. public static void note(String str)
  42. {
  43. // I'm assuming that tests are run serially
  44. stateLog.add(str);
  45. }
  46. @Test
  47. public void testImmediateStarts()
  48. throws Exception
  49. {
  50. Module module = new Module()
  51. {
  52. @Override
  53. public void configure(Binder binder)
  54. {
  55. binder.bind(InstanceThatRequiresStart.class).in(Scopes.SINGLETON);
  56. binder.bind(InstanceThatUsesInstanceThatRequiresStart.class).in(Scopes.SINGLETON);
  57. }
  58. };
  59. Injector injector = Guice.createInjector(
  60. Stage.PRODUCTION,
  61. new LifeCycleModule(),
  62. module
  63. );
  64. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  65. lifeCycleManager.start();
  66. Assert.assertEquals(stateLog, Arrays.asList("InstanceThatUsesInstanceThatRequiresStart:OK"));
  67. }
  68. @Test
  69. public void testPrivateModule()
  70. throws Exception
  71. {
  72. Module module = new Module()
  73. {
  74. @Override
  75. public void configure(Binder binder)
  76. {
  77. final PrivateModule privateModule = new PrivateModule()
  78. {
  79. @Override
  80. protected void configure()
  81. {
  82. binder().bind(SimpleBase.class).to(SimpleBaseImpl.class).in(Scopes.SINGLETON);
  83. binder().expose(SimpleBase.class);
  84. }
  85. };
  86. binder.install(privateModule);
  87. }
  88. };
  89. Injector injector = Guice.createInjector(
  90. Stage.PRODUCTION,
  91. new LifeCycleModule(),
  92. module
  93. );
  94. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  95. lifeCycleManager.start();
  96. Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl"));
  97. lifeCycleManager.stop();
  98. Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl", "preSimpleBaseImpl"));
  99. }
  100. @Test
  101. public void testSubClassAnnotated()
  102. throws Exception
  103. {
  104. Module module = new Module()
  105. {
  106. @Override
  107. public void configure(Binder binder)
  108. {
  109. binder.bind(SimpleBase.class).to(SimpleBaseImpl.class).in(Scopes.SINGLETON);
  110. }
  111. };
  112. Injector injector = Guice.createInjector(
  113. Stage.PRODUCTION,
  114. new LifeCycleModule(),
  115. module
  116. );
  117. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  118. lifeCycleManager.start();
  119. Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl"));
  120. lifeCycleManager.stop();
  121. Assert.assertEquals(stateLog, Arrays.asList("postSimpleBaseImpl", "preSimpleBaseImpl"));
  122. }
  123. @Test
  124. public void testExecuted()
  125. throws Exception
  126. {
  127. Injector injector = Guice.createInjector(
  128. Stage.PRODUCTION,
  129. new LifeCycleModule(),
  130. new Module()
  131. {
  132. @Override
  133. public void configure(Binder binder)
  134. {
  135. binder.bind(ExecutedInstance.class).in(Scopes.SINGLETON);
  136. }
  137. }
  138. );
  139. ExecutedInstance instance = injector.getInstance(ExecutedInstance.class);
  140. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  141. lifeCycleManager.start();
  142. instance.waitForStart();
  143. Assert.assertEquals(stateLog, Arrays.asList("Starting"));
  144. lifeCycleManager.stop();
  145. instance.waitForEnd();
  146. Assert.assertEquals(stateLog, Arrays.asList("Starting", "Done"));
  147. }
  148. @Test
  149. public void testDeepDependency()
  150. throws Exception
  151. {
  152. Module module = new Module()
  153. {
  154. @Override
  155. public void configure(Binder binder)
  156. {
  157. binder.bind(AnInstance.class).in(Scopes.SINGLETON);
  158. binder.bind(AnotherInstance.class).in(Scopes.SINGLETON);
  159. binder.bind(DependentInstance.class).in(Scopes.SINGLETON);
  160. }
  161. };
  162. Injector injector = Guice.createInjector(
  163. Stage.PRODUCTION,
  164. new LifeCycleModule(),
  165. module
  166. );
  167. injector.getInstance(AnotherInstance.class);
  168. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  169. lifeCycleManager.start();
  170. Assert.assertEquals(stateLog, Arrays.asList("postDependentInstance"));
  171. lifeCycleManager.stop();
  172. Assert.assertEquals(stateLog, Arrays.asList("postDependentInstance", "preDependentInstance"));
  173. }
  174. @Test
  175. public void testIllegalMethods()
  176. throws Exception
  177. {
  178. try {
  179. Guice.createInjector(
  180. Stage.PRODUCTION,
  181. new Module()
  182. {
  183. @Override
  184. public void configure(Binder binder)
  185. {
  186. binder.bind(IllegalInstance.class).in(Scopes.SINGLETON);
  187. }
  188. },
  189. new LifeCycleModule()
  190. );
  191. Assert.fail();
  192. }
  193. catch (CreationException dummy) {
  194. // correct behavior
  195. }
  196. }
  197. @Test
  198. public void testDuplicateMethodNames()
  199. throws Exception
  200. {
  201. Injector injector = Guice.createInjector(
  202. Stage.PRODUCTION,
  203. new Module()
  204. {
  205. @Override
  206. public void configure(Binder binder)
  207. {
  208. binder.bind(FooTestInstance.class).in(Scopes.SINGLETON);
  209. }
  210. },
  211. new LifeCycleModule()
  212. );
  213. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  214. lifeCycleManager.start();
  215. lifeCycleManager.stop();
  216. Assert.assertEquals(stateLog, Arrays.asList("foo"));
  217. }
  218. @Test
  219. public void testJITInjection()
  220. throws Exception
  221. {
  222. Injector injector = Guice.createInjector(
  223. Stage.PRODUCTION,
  224. new LifeCycleModule(),
  225. new Module()
  226. {
  227. @Override
  228. public void configure(Binder binder)
  229. {
  230. binder.bind(AnInstance.class).in(Scopes.SINGLETON);
  231. binder.bind(DependentInstance.class).in(Scopes.SINGLETON);
  232. }
  233. }
  234. );
  235. injector.getInstance(AnInstance.class);
  236. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  237. lifeCycleManager.start();
  238. lifeCycleManager.stop();
  239. Assert.assertEquals(stateLog, Arrays.asList("postDependentInstance", "preDependentInstance"));
  240. }
  241. @Test
  242. public void testNoPreDestroy()
  243. throws Exception
  244. {
  245. Injector injector = Guice.createInjector(
  246. Stage.PRODUCTION,
  247. new LifeCycleModule(),
  248. new Module()
  249. {
  250. @Override
  251. public void configure(Binder binder)
  252. {
  253. binder.bind(PostConstructOnly.class).in(Scopes.SINGLETON);
  254. binder.bind(PreDestroyOnly.class).in(Scopes.SINGLETON);
  255. }
  256. }
  257. );
  258. injector.getInstance(PostConstructOnly.class);
  259. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  260. lifeCycleManager.start();
  261. Assert.assertEquals(stateLog, Arrays.asList("makeMe"));
  262. lifeCycleManager.stop();
  263. Assert.assertEquals(stateLog, Arrays.asList("makeMe", "unmakeMe"));
  264. }
  265. @Test
  266. public void testModule()
  267. throws Exception
  268. {
  269. Module module = new Module()
  270. {
  271. @Override
  272. public void configure(Binder binder)
  273. {
  274. binder.bind(DependentBoundInstance.class).to(DependentInstanceImpl.class).in(Scopes.SINGLETON);
  275. binder.bind(DependentInstance.class).in(Scopes.SINGLETON);
  276. binder.bind(InstanceOne.class).in(Scopes.SINGLETON);
  277. binder.bind(InstanceTwo.class).in(Scopes.SINGLETON);
  278. }
  279. };
  280. Injector injector = Guice.createInjector(
  281. Stage.PRODUCTION,
  282. new LifeCycleModule(),
  283. module
  284. );
  285. LifeCycleManager lifeCycleManager = injector.getInstance(LifeCycleManager.class);
  286. lifeCycleManager.start();
  287. lifeCycleManager.stop();
  288. Set<String> stateLogSet = Sets.newHashSet(stateLog);
  289. Assert.assertEquals(stateLogSet, Sets.newHashSet("postDependentBoundInstance", "postDependentInstance", "postMakeOne", "postMakeTwo", "preDestroyTwo", "preDestroyOne", "preDependentInstance", "preDependentBoundInstance"));
  290. }
  291. }