Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
ut
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanyuan2021 committed Sep 27, 2023
1 parent 566e5ec commit 33357ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ private static Object doCreateServiceProxy(Biz biz, Object service, ClassLoader
clientClassLoader = callerClass.getClassLoader();
}

Biz clientBiz = ArkClient.getBizManagerService().getBizByClassLoader(clientClassLoader);
BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry.getBizRuntimeContext(clientBiz);
BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry.getBizRuntimeContextByClassLoader(clientClassLoader);
Map<ClassLoader, Map<String, ServiceProxyCache>> serviceProxyCaches =
bizRuntimeContext.getServiceProxyCaches();
Map<String, ServiceProxyCache> cacheMap = serviceProxyCaches.computeIfAbsent(service.getClass().getClassLoader(), o -> new ConcurrentHashMap<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alipay.sofa.serverless.common.environment.ConditionalOnNotMasterBiz;
import com.alipay.sofa.serverless.common.exception.BizRuntimeException;
import com.alipay.sofa.serverless.common.exception.ErrorCodes;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -33,10 +34,13 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.Properties;

Expand All @@ -58,29 +62,60 @@ public class SpringServiceFinderTest {

@Test
public void testSpringServiceFinder() {
ConfigurableApplicationContext masterCtx = buildApplicationContext("masterBiz");
masterCtx.getBeanFactory().registerSingleton("baseBean", new BaseBean());

ConfigurableApplicationContext bizCtx = buildApplicationContext("biz1");
bizCtx.getBeanFactory().registerSingleton("moduleBean", new ModuleBean());

ArkClient.setBizManagerService(bizManagerService);

ArkClient.setMasterBiz(masterBiz);
Mockito.when(bizManagerService.getBiz("master", "1.0.0")).thenReturn(masterBiz);
Mockito.when(masterBiz.getBizState()).thenReturn(BizState.ACTIVATED);
Mockito.when(masterBiz.getBizClassLoader()).thenReturn(ClassLoader.getSystemClassLoader());
Mockito.when(masterBiz.getBizName()).thenReturn("master");
Mockito.when(masterBiz.getBizVersion()).thenReturn("1.0.0");
BizRuntimeContext masterBizRuntime = new BizRuntimeContext(masterBiz, masterCtx);
BizRuntimeContextRegistry.registerSpringContext(masterBizRuntime);

ArkClient.setBizManagerService(bizManagerService);
Mockito.when(bizManagerService.getBiz("biz1", "version1")).thenReturn(biz1);

Properties properties = new Properties();
properties.setProperty("spring.application.name", "biz1");
SpringApplication springApplication = new SpringApplication(CustomConfiguration.class);
springApplication.setDefaultProperties(properties);
springApplication.setWebApplicationType(WebApplicationType.NONE);
ConfigurableApplicationContext bizContext = springApplication.run();
bizContext.getBeanFactory().registerSingleton("moduleBean", new ModuleBean());
Mockito.when(biz1.getBizState()).thenReturn(BizState.ACTIVATED);
Mockito.when(biz1.getBizClassLoader()).thenReturn(new URLClassLoader(new URL[0]));
Mockito.when(biz1.getBizName()).thenReturn("biz1");
Mockito.when(biz1.getBizVersion()).thenReturn("version1");
BizRuntimeContext bizRuntime = new BizRuntimeContext(biz1, bizCtx);
BizRuntimeContextRegistry.registerSpringContext(bizRuntime);

BaseBean baseBean = SpringServiceFinder.getBaseService("baseBean");
Assert.assertNotNull(baseBean);
BaseBean baseBean1 = SpringServiceFinder.getBaseService(BaseBean.class);
Assert.assertNotNull(baseBean1);
Map<String, BaseBean> baseBeanMap = SpringServiceFinder.listBaseServices(BaseBean.class);
Assert.assertNotNull(baseBeanMap);
Assert.assertEquals(1, baseBeanMap.size());
ModuleBean moduleBean = SpringServiceFinder.getModuleService("biz1", "version1",
"moduleBean");
Assert.assertNotNull(moduleBean);
ModuleBean moduleBean1 = SpringServiceFinder.getModuleService("biz1", "version1",
ModuleBean.class);
Assert.assertNotNull(moduleBean1);
Map<String, ModuleBean> moduleBeanMap = SpringServiceFinder.listModuleServices("biz1",
"version1", ModuleBean.class);
Assert.assertNotNull(moduleBeanMap);
Assert.assertEquals(1, moduleBeanMap.size());

Assert.assertEquals("base", baseBean.test());
Assert.assertEquals("module", moduleBean.test());
}

public ConfigurableApplicationContext buildApplicationContext(String appName) {
Properties properties = new Properties();
properties.setProperty("spring.application.name", appName);
SpringApplication springApplication = new SpringApplication(CustomConfiguration.class);
springApplication.setDefaultProperties(properties);
springApplication.setWebApplicationType(WebApplicationType.NONE);
return springApplication.run();
}

@Configuration
Expand All @@ -89,9 +124,17 @@ public static class CustomConfiguration {
}

public class BaseBean {

public String test() {
return "base";
}
}

public class ModuleBean {

public String test() {
return "module";
}
}

}

0 comments on commit 33357ac

Please sign in to comment.