Skip to content

Commit

Permalink
Merge 84a4f24 into 8a941a5
Browse files Browse the repository at this point in the history
  • Loading branch information
straybirdzls committed Jan 2, 2019
2 parents 8a941a5 + 84a4f24 commit f7cea94
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,21 @@ private List<String> getFormattedModuleInfo(String key) {
}

protected abstract void loadSpringXMLs();

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AbstractDeploymentDescriptor)) {
return false;
}
AbstractDeploymentDescriptor that = (AbstractDeploymentDescriptor) o;
return Objects.equals(this.getModuleName(), that.getModuleName());
}

@Override
public int hashCode() {
return Objects.hash(getModuleName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void refreshSpringContext(ApplicationRuntimeModel application) {
ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
for (DeploymentDescriptor deployment : application.getResolvedDeployments()) {
if (deployment.isSpringPowered()) {
if (deployment.isSpringPowered() && !application.getFailed().contains(deployment)) {
Thread.currentThread().setContextClassLoader(deployment.getClassLoader());
doRefreshSpringContext(deployment, application);
}
Expand Down Expand Up @@ -236,7 +236,8 @@ public void run() {
Thread.currentThread().setName(
"sofa-module-start-" + deployment.getModuleName());
Thread.currentThread().setContextClassLoader(deployment.getClassLoader());
if (deployment.isSpringPowered()) {
if (deployment.isSpringPowered()
&& !application.getFailed().contains(deployment)) {
doRefreshSpringContext(deployment, application);
}
DependencyTree.Entry<String, DeploymentDescriptor> entry = application
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.isle.integration;

import com.alipay.sofa.healthcheck.core.HealthChecker;
import com.alipay.sofa.isle.ApplicationRuntimeModel;
import com.alipay.sofa.isle.constants.SofaModuleFrameworkConstants;
import com.alipay.sofa.isle.deployment.DeploymentDescriptor;
import com.alipay.sofa.isle.util.AddCustomJar;
import com.alipay.sofa.isle.util.SeparateClassLoaderTestRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.annotation.DirtiesContext;

import static org.junit.Assert.assertEquals;

/**
* @author ruoshan
* @since 2.6.0
*/
@RunWith(SeparateClassLoaderTestRunner.class)
@SpringBootTest(classes = SofaBootTestApplication.class)
@AddCustomJar({ "dev-module-0.1.0.jar", "fail-module-0.1.0.jar" })
@DirtiesContext
public class FailModuleTest implements ApplicationContextAware {
private ApplicationContext applicationContext;

@Test
public void test() {
ApplicationRuntimeModel applicationRuntimeModel = (ApplicationRuntimeModel) applicationContext
.getBean(SofaModuleFrameworkConstants.APPLICATION);

// contains three Deployments
assertEquals(3, applicationRuntimeModel.getAllDeployments().size());
assertEquals(2, applicationRuntimeModel.getInstalled().size());
assertEquals(1, applicationRuntimeModel.getFailed().size());

// check module not in installed list
DeploymentDescriptor failModule = applicationRuntimeModel.getFailed().get(0);
Assert.assertEquals("com.alipay.sofa.fail", failModule.getModuleName());
Assert.assertFalse(applicationRuntimeModel.getInstalled().contains(failModule));
}

@Test
public void testHealthChecker() {
Assert.assertNotNull(applicationContext.getBean("sofaModuleHealthChecker"));
HealthChecker healthChecker = (HealthChecker) applicationContext
.getBean("sofaModuleHealthChecker");
Assert.assertEquals(Status.DOWN, healthChecker.isHealthy().getStatus());
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.isle.integration;

import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.TestPropertySource;

/**
* @author ruoshan
* @since 2.6.0
*/
@TestPropertySource(properties = "com.alipay.sofa.boot.module-start-up-parallel=true")
public class FailModuleWithParallelTest extends FailModuleTest implements ApplicationContextAware {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.isle.scan;

import org.springframework.beans.factory.InitializingBean;

/**
* Test For Fail Module
* @author ruoshan
* @since 2.6.0
*/
public class FailBean implements InitializingBean {

@Override
public void afterPropertiesSet() throws Exception {
throw new RuntimeException("fail bean test!!!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.isle.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
*
* @author ruoshan
* @since 2.6.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AddCustomJar {

String[] value();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.isle.util;

import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URLClassLoader;

/**
*
* @author xuanbei
* @author ruoshan
* @since 2.6.0
*/
public class SeparateClassLoaderTestRunner extends SpringJUnit4ClassRunner {

private final SeparateClassLoader separateClassloader = new SeparateClassLoader();

private Method runMethod;
private Object runnerObject;

public SeparateClassLoaderTestRunner(Class<?> clazz) throws InitializationError {
super(clazz);
try {
AddCustomJar customJar = getAddCustomJarAnnotationRecursively(clazz);
if (customJar != null) {
for (String jar : customJar.value()) {
separateClassloader.addJar(jar);
}
}
Class springJUnit4ClassRunnerClass = separateClassloader
.loadClass(SpringJUnit4ClassRunner.class.getName());
Constructor constructor = springJUnit4ClassRunnerClass.getConstructor(Class.class);
runnerObject = constructor.newInstance(separateClassloader.loadClass(clazz.getName()));
runMethod = springJUnit4ClassRunnerClass.getMethod("run", RunNotifier.class);

} catch (Throwable e) {
throw new InitializationError(e);
}
}

@Override
public void run(RunNotifier notifier) {
ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(separateClassloader);
runMethod.invoke(runnerObject, notifier);
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
Thread.currentThread().setContextClassLoader(oldContextClassLoader);
}
}

public static AddCustomJar getAddCustomJarAnnotationRecursively(Class<?> klass) {
AddCustomJar addCustomJar = klass.getAnnotation(AddCustomJar.class);

if (addCustomJar != null || klass == Object.class) {
return addCustomJar;
}

return getAddCustomJarAnnotationRecursively(klass.getSuperclass());
}

public static class SeparateClassLoader extends URLClassLoader {
public SeparateClassLoader() {
super(((URLClassLoader) getSystemClassLoader()).getURLs(), null);
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (name.startsWith("org.junit") || name.startsWith("java")) {
return getSystemClassLoader().loadClass(name);
}

return super.loadClass(name);
}

void addJar(String jar) {
super.addURL(SeparateClassLoaderTestRunner.class.getClassLoader().getResource(jar));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ spring.application.name=isle-test
com.alipay.sofa.boot.active-profiles=dev
com.alipay.sofa.boot.bean-load-cost=0
com.alipay.sofa.boot.allow-bean-definition-overriding=false
com.alipay.sofa.boot.module-start-up-parallel=false
com.alipay.sofa.boot.module-start-up-parallel=false

logging.path=./logs
Binary file not shown.

0 comments on commit f7cea94

Please sign in to comment.