/core/src/test/java/org/jclouds/lifecycle/config/LifeCycleModuleTest.java

https://github.com/richardcloudsoft/legacy-jclouds · Java · 120 lines · 80 code · 16 blank · 24 comment · 3 complexity · cf910c364b620fd58574924a382e7a1a MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.lifecycle.config;
  20. import static com.google.inject.name.Names.named;
  21. import static org.jclouds.Constants.PROPERTY_IO_WORKER_THREADS;
  22. import static org.jclouds.Constants.PROPERTY_USER_THREADS;
  23. import java.io.IOException;
  24. import javax.annotation.PostConstruct;
  25. import org.jclouds.concurrent.config.ExecutorServiceModule;
  26. import org.jclouds.lifecycle.Closer;
  27. import org.testng.annotations.Test;
  28. import com.google.common.util.concurrent.ExecutionList;
  29. import com.google.common.util.concurrent.ListeningExecutorService;
  30. import com.google.inject.AbstractModule;
  31. import com.google.inject.Guice;
  32. import com.google.inject.Injector;
  33. import com.google.inject.Key;
  34. /**
  35. *
  36. * @author Adrian Cole
  37. */
  38. @Test
  39. public class LifeCycleModuleTest {
  40. @Test
  41. void testBindsExecutor() {
  42. Injector i = createInjector();
  43. assert i.getInstance(Key.get(ListeningExecutorService.class, named(PROPERTY_USER_THREADS))) != null;
  44. assert i.getInstance(Key.get(ListeningExecutorService.class, named(PROPERTY_IO_WORKER_THREADS))) != null;
  45. }
  46. private Injector createInjector() {
  47. Injector i = Guice.createInjector(new AbstractModule() {
  48. protected void configure() {
  49. bindConstant().annotatedWith(named(PROPERTY_IO_WORKER_THREADS)).to(1);
  50. bindConstant().annotatedWith(named(PROPERTY_USER_THREADS)).to(1);
  51. }
  52. }, new LifeCycleModule(), new ExecutorServiceModule());
  53. // TODO: currently have to manually invoke the execution list, as otherwise it may occur
  54. // before everything is wired up
  55. i.getInstance(ExecutionList.class).execute();
  56. return i;
  57. }
  58. @Test
  59. void testBindsCloser() {
  60. Injector i = createInjector();
  61. assert i.getInstance(Closer.class) != null;
  62. }
  63. @Test
  64. void testCloserClosesExecutor() throws IOException {
  65. Injector i = createInjector();
  66. ListeningExecutorService executor = i.getInstance(Key.get(ListeningExecutorService.class,
  67. named(PROPERTY_USER_THREADS)));
  68. assert !executor.isShutdown();
  69. Closer closer = i.getInstance(Closer.class);
  70. closer.close();
  71. assert executor.isShutdown();
  72. }
  73. @Test
  74. void testCloserPreDestroyOrder() throws IOException {
  75. Injector i = createInjector();
  76. ListeningExecutorService userExecutor = i.getInstance(Key.get(ListeningExecutorService.class,
  77. named(PROPERTY_USER_THREADS)));
  78. assert !userExecutor.isShutdown();
  79. ListeningExecutorService ioExecutor = i.getInstance(Key.get(ListeningExecutorService.class,
  80. named(PROPERTY_IO_WORKER_THREADS)));
  81. assert !ioExecutor.isShutdown();
  82. Closer closer = i.getInstance(Closer.class);
  83. closer.close();
  84. assert userExecutor.isShutdown();
  85. assert ioExecutor.isShutdown();
  86. }
  87. static class PostConstructable {
  88. boolean isStarted;
  89. @PostConstruct
  90. void start() {
  91. isStarted = true;
  92. }
  93. }
  94. @Test
  95. void testPostConstruct() {
  96. Injector i = createInjector().createChildInjector(new AbstractModule() {
  97. protected void configure() {
  98. bind(PostConstructable.class);
  99. }
  100. });
  101. PostConstructable postConstructable = i.getInstance(PostConstructable.class);
  102. assert postConstructable.isStarted;
  103. }
  104. }