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

https://github.com/regularfry/jclouds · Java · 134 lines · 95 code · 17 blank · 22 comment · 3 complexity · 8021211d307956c4d446a2746a50a0fc 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 com.google.inject.name.Names;
  28. import org.testng.annotations.Test;
  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. /**
  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(ExecutorService.class, Names
  44. .named(Constants.PROPERTY_USER_THREADS))) != null;
  45. assert i.getInstance(Key.get(ExecutorService.class, Names
  46. .named(Constants.PROPERTY_IO_WORKER_THREADS))) != null;
  47. }
  48. private Injector createInjector() {
  49. Injector i = Guice.createInjector(new LifeCycleModule() {
  50. @SuppressWarnings("unused")
  51. @Provides
  52. @Named(Constants.PROPERTY_USER_THREADS)
  53. int p() {
  54. return 1;
  55. }
  56. @SuppressWarnings("unused")
  57. @Provides
  58. @Named(Constants.PROPERTY_MAX_CONNECTIONS_PER_CONTEXT)
  59. int p2() {
  60. return 1;
  61. }
  62. @SuppressWarnings("unused")
  63. @Provides
  64. @Named(Constants.PROPERTY_IO_WORKER_THREADS)
  65. int p3() {
  66. return 1;
  67. }
  68. }, new ExecutorServiceModule());
  69. return i;
  70. }
  71. @Test
  72. void testBindsCloser() {
  73. Injector i = createInjector();
  74. assert i.getInstance(Closer.class) != null;
  75. }
  76. @Test
  77. void testCloserClosesExecutor() throws IOException {
  78. Injector i = createInjector();
  79. ExecutorService executor = i.getInstance(Key.get(ExecutorService.class, Names
  80. .named(Constants.PROPERTY_USER_THREADS)));
  81. assert !executor.isShutdown();
  82. Closer closer = i.getInstance(Closer.class);
  83. closer.close();
  84. assert executor.isShutdown();
  85. }
  86. @Test
  87. void testCloserPreDestroyOrder() throws IOException {
  88. Injector i = createInjector();
  89. ExecutorService userThreads = i.getInstance(Key.get(ExecutorService.class, Names
  90. .named(Constants.PROPERTY_USER_THREADS)));
  91. assert !userThreads.isShutdown();
  92. ExecutorService ioThreads = i.getInstance(Key.get(ExecutorService.class, Names
  93. .named(Constants.PROPERTY_IO_WORKER_THREADS)));
  94. assert !ioThreads.isShutdown();
  95. Closer closer = i.getInstance(Closer.class);
  96. closer.close();
  97. assert userThreads.isShutdown();
  98. assert ioThreads.isShutdown();
  99. }
  100. static class PostConstructable {
  101. boolean isStarted;
  102. @PostConstruct
  103. void start() {
  104. isStarted = true;
  105. }
  106. }
  107. @Test
  108. void testPostConstruct() {
  109. Injector i = createInjector().createChildInjector(new AbstractModule() {
  110. protected void configure() {
  111. bind(PostConstructable.class);
  112. }
  113. });
  114. PostConstructable postConstructable = i.getInstance(PostConstructable.class);
  115. assert postConstructable.isStarted;
  116. }
  117. }