ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aonis...@apache.org
Subject [2/2] ambari git commit: AMBARI-18454. Analyze and Optimize Ambari Server Unit Tests (aonishuk)
Date Mon, 26 Sep 2016 10:22:50 GMT
AMBARI-18454. Analyze and Optimize Ambari Server Unit Tests (aonishuk)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/7d8adc45
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/7d8adc45
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/7d8adc45

Branch: refs/heads/branch-2.5
Commit: 7d8adc45f04606c3063d8bb619234d9595039ce5
Parents: 6edb4af
Author: Andrew Onishuk <aonishuk@hortonworks.com>
Authored: Mon Sep 26 13:22:48 2016 +0300
Committer: Andrew Onishuk <aonishuk@hortonworks.com>
Committed: Mon Sep 26 13:22:48 2016 +0300

----------------------------------------------------------------------
 .../server/controller/ControllerModule.java     | 44 ++++++++++++--------
 .../actionmanager/TestActionScheduler.java      | 19 +++++----
 .../server/agent/HeartbeatProcessorTest.java    | 27 +++++++-----
 .../server/orm/InMemoryDefaultTestModule.java   | 23 +++++++++-
 .../server/upgrade/UpgradeCatalog222Test.java   | 15 ++++++-
 5 files changed, 87 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/7d8adc45/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
index 9cfa3fa..48d4e34 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/ControllerModule.java
@@ -373,8 +373,8 @@ public class ControllerModule extends AbstractModule {
     requestStaticInjection(AuthorizationHelper.class);
 
     bindByAnnotation(null);
-    bindNotificationDispatchers();
-    registerUpgradeChecks();
+    bindNotificationDispatchers(null);
+    registerUpgradeChecks(null);
   }
 
   // ----- helper methods ----------------------------------------------------
@@ -565,27 +565,29 @@ public class ControllerModule extends AbstractModule {
    * {@link DispatchFactory}.
    */
   @SuppressWarnings("unchecked")
-  private void bindNotificationDispatchers() {
-    ClassPathScanningCandidateComponentProvider scanner =
-        new ClassPathScanningCandidateComponentProvider(false);
+  protected Set<BeanDefinition> bindNotificationDispatchers(Set<BeanDefinition>
beanDefinitions) {
 
     // make the factory a singleton
     DispatchFactory dispatchFactory = DispatchFactory.getInstance();
     bind(DispatchFactory.class).toInstance(dispatchFactory);
 
-    // match all implementations of the dispatcher interface
-    AssignableTypeFilter filter = new AssignableTypeFilter(
+    if (null == beanDefinitions || beanDefinitions.isEmpty()) {
+      ClassPathScanningCandidateComponentProvider scanner =
+        new ClassPathScanningCandidateComponentProvider(false);
+
+      // match all implementations of the dispatcher interface
+      AssignableTypeFilter filter = new AssignableTypeFilter(
         NotificationDispatcher.class);
 
-    scanner.addIncludeFilter(filter);
+      scanner.addIncludeFilter(filter);
 
-    Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents(
-        "org.apache.ambari.server.notifications.dispatchers");
+      beanDefinitions = scanner.findCandidateComponents("org.apache.ambari.server.notifications.dispatchers");
+    }
 
     // no dispatchers is a problem
     if (null == beanDefinitions || beanDefinitions.size() == 0) {
       LOG.error("No instances of {} found to register", NotificationDispatcher.class);
-      return;
+      return null;
     }
 
     // for every discovered dispatcher, singleton-ize them and register with
@@ -611,6 +613,8 @@ public class ControllerModule extends AbstractModule {
             clazz, exception);
       }
     }
+
+    return beanDefinitions;
   }
 
   /**
@@ -619,23 +623,26 @@ public class ControllerModule extends AbstractModule {
    * {@link UpgradeCheckRegistry}.
    */
   @SuppressWarnings("unchecked")
-  private void registerUpgradeChecks() {
-    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
+  protected Set<BeanDefinition> registerUpgradeChecks(Set<BeanDefinition> beanDefinitions)
{
 
     // make the registry a singleton
     UpgradeCheckRegistry registry = new UpgradeCheckRegistry();
     bind(UpgradeCheckRegistry.class).toInstance(registry);
 
-    // match all implementations of the base check class
-    AssignableTypeFilter filter = new AssignableTypeFilter(AbstractCheckDescriptor.class);
-    scanner.addIncludeFilter(filter);
+    if (null == beanDefinitions || beanDefinitions.isEmpty()) {
+      ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
 
-    Set<BeanDefinition> beanDefinitions = scanner.findCandidateComponents(AbstractCheckDescriptor.class.getPackage().getName());
+      // match all implementations of the base check class
+      AssignableTypeFilter filter = new AssignableTypeFilter(AbstractCheckDescriptor.class);
+      scanner.addIncludeFilter(filter);
+
+      beanDefinitions = scanner.findCandidateComponents(AbstractCheckDescriptor.class.getPackage().getName());
+    }
 
     // no dispatchers is a problem
     if (null == beanDefinitions || beanDefinitions.size() == 0) {
       LOG.error("No instances of {} found to register", AbstractCheckDescriptor.class);
-      return;
+      return null;
     }
 
     // for every discovered check, singleton-ize them and register with the
@@ -658,5 +665,6 @@ public class ControllerModule extends AbstractModule {
     for (AbstractCheckDescriptor upgradeCheck : upgradeChecks) {
       LOG.debug("Registered pre-upgrade check {}", upgradeCheck.getClass());
     }
+    return beanDefinitions;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/7d8adc45/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java
b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java
index 2c39bda..ccb95a6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java
@@ -127,29 +127,31 @@ public class TestActionScheduler {
       + " c6402.ambari.apache.org], slave_hosts=[c6401.ambari.apache.org,"
       + " c6402.ambari.apache.org]}";
 
-  private static Injector injector;
+  private final Injector injector;
 
   private final String hostname = "ahost.ambari.apache.org";
   private final int MAX_CYCLE_ITERATIONS = 100;
 
   @Inject
-  HostRoleCommandFactory hostRoleCommandFactory;
+  private HostRoleCommandFactory hostRoleCommandFactory;
 
   @Inject
-  StageFactory stageFactory;
+  private StageFactory stageFactory;
 
   @Inject
-  StageUtils stageUtils;
+  private HostDAO hostDAO;
 
-  @Inject
-  HostDAO hostDAO;
+  private Provider<EntityManager> entityManagerProviderMock = EasyMock.niceMock(Provider.class);
 
-  Provider<EntityManager> entityManagerProviderMock = EasyMock.niceMock(Provider.class);
+
+  public  TestActionScheduler(){
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+  }
 
   @Before
   public void setup() throws Exception {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
+    injector.getInstance(StageUtils.class);
     injector.injectMembers(this);
 
     expect(entityManagerProviderMock.get()).andReturn(null);
@@ -159,7 +161,6 @@ public class TestActionScheduler {
   @After
   public void teardown() {
     injector.getInstance(PersistService.class).stop();
-    injector = null;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/7d8adc45/ambari-server/src/test/java/org/apache/ambari/server/agent/HeartbeatProcessorTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/agent/HeartbeatProcessorTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/agent/HeartbeatProcessorTest.java
index 158b97c..299002b 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/agent/HeartbeatProcessorTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/agent/HeartbeatProcessorTest.java
@@ -59,6 +59,7 @@ import org.apache.ambari.server.actionmanager.StageFactory;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.audit.AuditLogger;
 import org.apache.ambari.server.configuration.Configuration;
+import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.HostDAO;
@@ -102,22 +103,26 @@ public class HeartbeatProcessorTest {
 
   private static final Logger log = LoggerFactory.getLogger(TestHeartbeatHandler.class);
   private Injector injector;
+  private long requestId = 23;
+  private long stageId = 31;
+
+  @Inject
   private Clusters clusters;
-  long requestId = 23;
-  long stageId = 31;
+
+  @Inject
   private UnitOfWork unitOfWork;
 
   @Inject
   Configuration config;
 
   @Inject
-  ActionDBAccessor actionDBAccessor;
+  private ActionDBAccessor actionDBAccessor;
 
   @Inject
-  HeartbeatTestHelper heartbeatTestHelper;
+  private HeartbeatTestHelper heartbeatTestHelper;
 
   @Inject
-  ActionManagerTestHelper actionManagerTestHelper;
+  private ActionManagerTestHelper actionManagerTestHelper;
 
   @Inject
   private HostRoleCommandFactory hostRoleCommandFactory;
@@ -131,17 +136,17 @@ public class HeartbeatProcessorTest {
   @Inject
   private AmbariMetaInfo metaInfo;
 
-  private final static StackId HDP_22_STACK = new StackId("HDP", "2.2.0");
 
-  @Before
-  public void setup() throws Exception {
+  public HeartbeatProcessorTest(){
     InMemoryDefaultTestModule module = HeartbeatTestHelper.getTestModule();
     injector = Guice.createInjector(module);
+  }
+
+
+  @Before
+  public void setup() throws Exception {
     injector.getInstance(GuiceJpaInitializer.class);
-    clusters = injector.getInstance(Clusters.class);
     injector.injectMembers(this);
-    unitOfWork = injector.getInstance(UnitOfWork.class);
-
     EasyMock.replay(injector.getInstance(AuditLogger.class));
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/7d8adc45/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
index 7f5842f..6773176 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/InMemoryDefaultTestModule.java
@@ -50,7 +50,14 @@ public class InMemoryDefaultTestModule extends AbstractModule {
 
     // Access should be synchronised to allow concurrent test runs.
     private static final AtomicReference<Set<BeanDefinition>> foundBeanDefinitions
-        = new AtomicReference<Set<BeanDefinition>>(null);
+        = new AtomicReference<>(null);
+
+    private static final AtomicReference<Set<BeanDefinition>> foundNotificationBeanDefinitions
+        = new AtomicReference<>(null);
+
+    private static final AtomicReference<Set<BeanDefinition>> foundUpgradeChecksDefinitions
+        = new AtomicReference<>(null);
+
 
     public BeanDefinitionsCachingTestControllerModule(Properties properties) throws Exception
{
       super(properties);
@@ -62,6 +69,20 @@ public class InMemoryDefaultTestModule extends AbstractModule {
       foundBeanDefinitions.compareAndSet(null, Collections.unmodifiableSet(newBeanDefinitions));
       return null;
     }
+
+    @Override
+    protected Set<BeanDefinition> bindNotificationDispatchers(Set<BeanDefinition>
beanDefinitions){
+      Set<BeanDefinition> newBeanDefinitions = super.bindNotificationDispatchers(foundNotificationBeanDefinitions.get());
+      foundNotificationBeanDefinitions.compareAndSet(null, Collections.unmodifiableSet(newBeanDefinitions));
+      return null;
+    }
+
+    @Override
+    protected Set<BeanDefinition> registerUpgradeChecks(Set<BeanDefinition> beanDefinitions){
+      Set<BeanDefinition> newBeanDefinition = super.registerUpgradeChecks(foundUpgradeChecksDefinitions.get());
+      foundUpgradeChecksDefinitions.compareAndSet(null, Collections.unmodifiableSet(newBeanDefinition));
+      return null;
+    }
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/ambari/blob/7d8adc45/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
index cdbf39b..8d5dec6 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java
@@ -45,6 +45,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 
+import com.google.inject.Inject;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.ActionManager;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
@@ -96,23 +97,33 @@ public class UpgradeCatalog222Test {
   private Injector injector;
   private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
   private EntityManager entityManager = createNiceMock(EntityManager.class);
+
+  @Inject
   private UpgradeCatalogHelper upgradeCatalogHelper;
+
   private StackEntity desiredStackEntity;
 
   @Rule
   public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
+  public UpgradeCatalog222Test(){
+    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+
+  }
+
   @Before
   public void init() {
     reset(entityManagerProvider);
     expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
     replay(entityManagerProvider);
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
+
     injector.getInstance(GuiceJpaInitializer.class);
 
-    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
     // inject AmbariMetaInfo to ensure that stacks get populated in the DB
     injector.getInstance(AmbariMetaInfo.class);
+
+    injector.injectMembers(this);
+
     // load the stack entity
     StackDAO stackDAO = injector.getInstance(StackDAO.class);
     desiredStackEntity = stackDAO.find("HDP", "2.2.0");


Mime
View raw message