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

https://github.com/andreisavu/jclouds · Java · 138 lines · 97 code · 17 blank · 24 comment · 3 complexity · 32e19dcd00161f82f5da5544a8c0b2c8 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 java.io.IOException;
  21. import java.util.concurrent.ExecutorService;
  22. import javax.annotation.PostConstruct;
  23. import javax.inject.Named;
  24. import org.jclouds.Constants;
  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.inject.AbstractModule;
  30. import com.google.inject.Guice;
  31. import com.google.inject.Injector;
  32. import com.google.inject.Key;
  33. import com.google.inject.Provides;
  34. import com.google.inject.name.Names;
  35. /**
  36. *
  37. * @author Adrian Cole
  38. */
  39. @Test
  40. public class LifeCycleModuleTest {
  41. @Test
  42. void testBindsExecutor() {
  43. Injector i = createInjector();
  44. assert i.getInstance(Key.get(ExecutorService.class, Names
  45. .named(Constants.PROPERTY_USER_THREADS))) != null;
  46. assert i.getInstance(Key.get(ExecutorService.class, Names
  47. .named(Constants.PROPERTY_IO_WORKER_THREADS))) != null;
  48. }
  49. private Injector createInjector() {
  50. Injector i = Guice.createInjector(new LifeCycleModule() {
  51. @SuppressWarnings("unused")
  52. @Provides
  53. @Named(Constants.PROPERTY_USER_THREADS)
  54. int p() {
  55. return 1;
  56. }
  57. @SuppressWarnings("unused")
  58. @Provides
  59. @Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)
  60. int p2() {
  61. return 1;
  62. }
  63. @SuppressWarnings("unused")
  64. @Provides
  65. @Named(Constants.PROPERTY_IO_WORKER_THREADS)
  66. int p3() {
  67. return 1;
  68. }
  69. }, new ExecutorServiceModule());
  70. // TODO: currently have to manually invoke the execution list, as otherwise it may occur
  71. // before everything is wired up
  72. i.getInstance(ExecutionList.class).execute();
  73. return i;
  74. }
  75. @Test
  76. void testBindsCloser() {
  77. Injector i = createInjector();
  78. assert i.getInstance(Closer.class) != null;
  79. }
  80. @Test
  81. void testCloserClosesExecutor() throws IOException {
  82. Injector i = createInjector();
  83. ExecutorService executor = i.getInstance(Key.get(ExecutorService.class, Names
  84. .named(Constants.PROPERTY_USER_THREADS)));
  85. assert !executor.isShutdown();
  86. Closer closer = i.getInstance(Closer.class);
  87. closer.close();
  88. assert executor.isShutdown();
  89. }
  90. @Test
  91. void testCloserPreDestroyOrder() throws IOException {
  92. Injector i = createInjector();
  93. ExecutorService userThreads = i.getInstance(Key.get(ExecutorService.class, Names
  94. .named(Constants.PROPERTY_USER_THREADS)));
  95. assert !userThreads.isShutdown();
  96. ExecutorService ioThreads = i.getInstance(Key.get(ExecutorService.class, Names
  97. .named(Constants.PROPERTY_IO_WORKER_THREADS)));
  98. assert !ioThreads.isShutdown();
  99. Closer closer = i.getInstance(Closer.class);
  100. closer.close();
  101. assert userThreads.isShutdown();
  102. assert ioThreads.isShutdown();
  103. }
  104. static class PostConstructable {
  105. boolean isStarted;
  106. @PostConstruct
  107. void start() {
  108. isStarted = true;
  109. }
  110. }
  111. @Test
  112. void testPostConstruct() {
  113. Injector i = createInjector().createChildInjector(new AbstractModule() {
  114. protected void configure() {
  115. bind(PostConstructable.class);
  116. }
  117. });
  118. PostConstructable postConstructable = i.getInstance(PostConstructable.class);
  119. assert postConstructable.isStarted;
  120. }
  121. }