Skip to content

Commit

Permalink
Introduce -DSpringReloadDelayMillis= for improve Spring redefinition …
Browse files Browse the repository at this point in the history
…time

default value is 1600. It is 3000 for tests. Value ~500 is reliable

-DSpringTestSleepTimeFactor=[0..1] for scaling junit sleep time, 0.1 is reliable
  • Loading branch information
skybber committed Nov 23, 2024
1 parent ba9f157 commit 56a8dbe
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,45 @@
*/
public class SpringReloadConfig {

private static final String SPRING_RELOAD_DELAY_MILLIS = "SpringReloadDelayMillis";
private static final String SPRING_TEST_SLEEP_TIME_FACTOR = "SpringTestSleepTimeFactor";
public static int reloadDelayMillis = 1600;
private static boolean isReloadDelayMillisProperty = false;
private static double testSleepTimeFactor = 1.0;

static {
String delayProperty = System.getProperty(SPRING_RELOAD_DELAY_MILLIS);
if (delayProperty != null) {
try {
reloadDelayMillis = Integer.parseInt(delayProperty);
isReloadDelayMillisProperty = true;
} catch (NumberFormatException e) {
System.err.println("Invalid format for -D" + SPRING_RELOAD_DELAY_MILLIS + ". Using default value: " + reloadDelayMillis);
}
}
String fac = System.getProperty(SPRING_TEST_SLEEP_TIME_FACTOR);
if (fac != null) {
try {
testSleepTimeFactor = Double.parseDouble(fac);
} catch (NumberFormatException e) {
System.err.println("Invalid format for -D" + SPRING_TEST_SLEEP_TIME_FACTOR + ".");
}
}
}

public static void setDelayMillis(int delayMillis) {
if (isReloadDelayMillisProperty) {
return;
}
if (delayMillis > 30000) {
reloadDelayMillis = 30000;
return;
}
SpringReloadConfig.reloadDelayMillis = delayMillis;
reloadDelayMillis = delayMillis;
}

public static long scaleTestSleepTime(long timeMillis) {
System.out.println("sleeping: " + Math.round(testSleepTimeFactor * timeMillis));
return Math.round(testSleepTimeFactor * timeMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.hotswap.agent.plugin.hotswapper.HotSwapper;
import org.hotswap.agent.plugin.spring.reload.BeanFactoryAssistant;
import org.hotswap.agent.plugin.spring.reload.SpringReloadConfig;
import org.hotswap.agent.plugin.spring.testBeans.*;
import org.hotswap.agent.plugin.spring.testBeans.iabpp.BeanService;
import org.hotswap.agent.plugin.spring.testBeans.iabpp.BeanServiceImplNoAspect;
Expand Down Expand Up @@ -216,7 +217,7 @@ public void pojoTest() throws Exception {
assertEquals(0, applicationContext.getBeanNamesForType(Pojo.class).length);
// no reload happens
HotSwapper.swapClasses(Pojo.class, Pojo2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
assertEquals(0, applicationContext.getBeanNamesForType(Pojo.class).length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.hotswap.agent.plugin.spring.factorybean.bak.v2.V2BakAnnotationBean4;
import org.hotswap.agent.plugin.spring.factorybean.bak.v2.V2BakAnnotationFactoryBean1;
import org.hotswap.agent.plugin.spring.factorybean.bak.v2.V2BakAnnotationFactoryBean2;
import org.hotswap.agent.plugin.spring.reload.SpringReloadConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void testFactoryBeanChanged() throws Exception {
HotSwapper.swapClasses(AnnotationBean4.class, BakAnnotationBean4.class.getName());
HotSwapper.swapClasses(AnnotationFactoryBean1.class, BakAnnotationFactoryBean1.class.getName());
HotSwapper.swapClasses(AnnotationFactoryBean2.class, BakAnnotationFactoryBean2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
// check
AnnotationBean1 annotationBeanNew1 = applicationContext.getBean(AnnotationBean1.class);
AnnotationBean2 annotationBeanNew2 = applicationContext.getBean(AnnotationBean2.class);
Expand Down Expand Up @@ -121,7 +122,7 @@ public void testFactoryBeanChanged() throws Exception {
HotSwapper.swapClasses(AnnotationBean4.class, V2BakAnnotationBean4.class.getName());
HotSwapper.swapClasses(AnnotationFactoryBean1.class, V2BakAnnotationFactoryBean1.class.getName());
HotSwapper.swapClasses(AnnotationFactoryBean2.class, V2BakAnnotationFactoryBean2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
// check
AnnotationBean1 annotationBeanV2_1 = applicationContext.getBean(AnnotationBean1.class);
AnnotationBean2 annotationBeanV2_2 = applicationContext.getBean(AnnotationBean2.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.hotswap.agent.plugin.spring.factorybean.bak.v21.V2BakXmlFactFactoryBean1;
import org.hotswap.agent.plugin.spring.factorybean.bak.v21.V2BakXmlFactFactoryBean2;
import org.hotswap.agent.plugin.spring.reload.SpringChangedAgent;
import org.hotswap.agent.plugin.spring.reload.SpringReloadConfig;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void testFactoryBeanChanged() throws Exception {
HotSwapper.swapClasses(XmlFactBean4.class, BakXmlFactBean4.class.getName());
HotSwapper.swapClasses(XmlFactoryBean1.class, BakXmlFactFactoryBean1.class.getName());
HotSwapper.swapClasses(XmlFactoryBean2.class, BakXmlFactFactoryBean2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
// check
XmlFactBean1 xmlFactBeanNew1 = applicationContext.getBean(XmlFactBean1.class);
XmlFactBean2 xmlFactBeanNew2 = applicationContext.getBean(XmlFactBean2.class);
Expand Down Expand Up @@ -124,7 +125,7 @@ public void testFactoryBeanChanged() throws Exception {
HotSwapper.swapClasses(XmlFactBean4.class, V2BakXmlFactBean4.class.getName());
HotSwapper.swapClasses(XmlFactoryBean1.class, V2BakXmlFactFactoryBean1.class.getName());
HotSwapper.swapClasses(XmlFactoryBean2.class, V2BakXmlFactFactoryBean2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
// check
XmlFactBean1 xmlFactBeanV2_1 = applicationContext.getBean(XmlFactBean1.class);
XmlFactBean2 xmlFactBeanV2_2 = applicationContext.getBean(XmlFactBean2.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.hotswap.agent.plugin.hotswapper.HotSwapper;
import org.hotswap.agent.plugin.spring.BaseTestUtil;
import org.hotswap.agent.plugin.spring.reload.SpringReloadConfig;
import org.hotswap.agent.plugin.spring.xml.bak.constructor.v1.BakConstructorBean3;
import org.hotswap.agent.plugin.spring.xml.bak.constructor.v1.BakConstructorBean4;
import org.hotswap.agent.plugin.spring.xml.bak.constructor.v1.BakConstructorFactoryBean2;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void testFactoryBeanChanged() throws Exception {
HotSwapper.swapClasses(XmlConstructorBean3.class, BakConstructorBean3.class.getName());
HotSwapper.swapClasses(XmlConstructorBean4.class, BakConstructorBean4.class.getName());
HotSwapper.swapClasses(XmlConstructorFactoryBean2.class, BakConstructorFactoryBean2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
// check
XmlConstructorBean1 xmlConstructorBeanNew1 = applicationContext.getBean(XmlConstructorBean1.class);
XmlConstructorBean2 xmlConstructorBeanNew2 = applicationContext.getBean(XmlConstructorBean2.class);
Expand Down Expand Up @@ -123,7 +124,7 @@ public void testFactoryBeanChanged() throws Exception {
HotSwapper.swapClasses(XmlConstructorFactoryBean2.class, BakConstructorFactoryBean2V2.class.getName());
HotSwapper.swapClasses(XmlConstructorParentBeanMul1.class, BakConstructorParentBeanMul1.class.getName());
HotSwapper.swapClasses(XmlConstructorParentBeanMul2.class, BakConstructorParentBeanMul2.class.getName());
Thread.sleep(8000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(8000));
// check
XmlConstructorBean1 xmlConstructorBeanV2_1 = applicationContext.getBean(XmlConstructorBean1.class);
XmlConstructorBean2 xmlConstructorBeanV2_2 = applicationContext.getBean(XmlConstructorBean2.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.hotswap.agent.plugin.hotswapper.HotSwapper;
import org.hotswap.agent.plugin.spring.BaseTestUtil;
import org.hotswap.agent.plugin.spring.reload.SpringReloadConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void swapPropertyTest() throws Exception {
byte[] content = Files.readAllBytes(propertyFile.getFile().toPath());
try {
modifyPropertyFile();
Thread.sleep(10000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(10000));

Item1 itemChange1 = applicationContext.getBean("item1", Item1.class);
Item1 itemChange11 = applicationContext.getBean("item11", Item1.class);
Expand Down Expand Up @@ -96,7 +97,7 @@ public void swapPropertyTest() throws Exception {
assertNotNull(applicationContext.getBean("item2", Item2.class).getName());
assertNull(applicationContext.getBean("item2", Item2.class).getName2());
HotSwapper.swapClasses(Item2.class, Item2WithoutValue.class.getName());
Thread.sleep(10000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(10000));

assertNull(applicationContext.getBean("item2", Item2.class).getName());
assertNotNull(applicationContext.getBean("item2", Item2.class).getName2());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.hotswap.agent.logging.AgentLogger;
import org.hotswap.agent.plugin.hotswapper.HotSwapper;
import org.hotswap.agent.plugin.spring.BaseTestUtil;
import org.hotswap.agent.plugin.spring.reload.SpringReloadConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void swapSingleClassTest() throws Exception {
moveClass("org.hotswap.agent.plugin.spring.xml.scanbak.ScanBakItem2",
"org.hotswap.agent.plugin.spring.xml.scan.ScanBakItem2", ScanItem.class.getClassLoader());

Thread.sleep(12000);
Thread.sleep(SpringReloadConfig.scaleTestSleepTime(12000));
LOGGER.info("swap class finished");
assertNotNull(applicationContext.getBean(ScanItem.class).getName());
assertNotNull(applicationContext.getBean(ScanItem2.class).getName());
Expand Down

0 comments on commit 56a8dbe

Please sign in to comment.