Skip to content

Commit 9f24ccb

Browse files
committed
Exception is thrown when component failed to register
Signed-off-by: JermaineHua <[email protected]>
1 parent c53e51c commit 9f24ccb

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/impl/ComponentManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private ComponentInfo doRegister(ComponentInfo ci) {
187187
ci.register();
188188
} catch (Throwable t) {
189189
LOGGER.error(ErrorCode.convert("01-03003", ci.getName()), t);
190-
return null;
190+
throw new ServiceRuntimeException(ErrorCode.convert("01-03003", ci.getName()));
191191
}
192192

193193
LOGGER.info("Registering component: {}", ci.getName());
@@ -209,6 +209,7 @@ private ComponentInfo doRegister(ComponentInfo ci) {
209209
} catch (Throwable t) {
210210
ci.exception(new Exception(t));
211211
LOGGER.error(ErrorCode.convert("01-03004", ci.getName()), t);
212+
throw new ServiceRuntimeException(ErrorCode.convert("01-03004", ci.getName()));
212213
}
213214

214215
return ci;

sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/test/java/com/alipay/sofa/runtime/impl/ComponentManagerImplTests.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,30 @@
4747
* @author huzijie
4848
* @version ComponentManagerImplTests.java, v 0.1 2023年04月10日 3:37 PM huzijie Exp $
4949
*/
50-
@ExtendWith({ MockitoExtension.class, OutputCaptureExtension.class })
50+
@ExtendWith({MockitoExtension.class, OutputCaptureExtension.class})
5151
public class ComponentManagerImplTests {
5252

5353
static {
5454
LogOutPutUtils.openOutPutForLoggers(ComponentManagerImpl.class);
5555
}
5656

5757
@Mock
58-
private ClientFactoryInternal clientFactoryInternal;
58+
private ClientFactoryInternal clientFactoryInternal;
5959

6060
@Mock
61-
private DemoComponent componentInfoA;
61+
private DemoComponent componentInfoA;
6262

6363
@Mock
64-
private DemoComponent componentInfoB;
64+
private DemoComponent componentInfoB;
6565

66-
private ComponentManagerImpl componentManager;
66+
private ComponentManagerImpl componentManager;
6767

6868
private final ClientFactoryInternal clientFactory = new ClientFactoryImpl();
6969

7070
@BeforeEach
7171
public void setUp() {
7272
componentManager = new ComponentManagerImpl(clientFactoryInternal, this.getClass()
73-
.getClassLoader());
73+
.getClassLoader());
7474
}
7575

7676
@Test
@@ -84,11 +84,11 @@ public void registerAndGetComponents() {
8484
assertThat(componentManager.getComponents()).contains(componentInfoA, componentInfoB);
8585
assertThat(componentManager.getComponentTypes()).containsExactly(componentInfoA.getType());
8686
assertThat(componentManager.getComponentInfosByType(componentInfoA.getType()))
87-
.containsExactly(componentInfoA, componentInfoB);
87+
.containsExactly(componentInfoA, componentInfoB);
8888
assertThat(componentManager.getComponentInfo(componentInfoA.getName())).isEqualTo(
89-
componentInfoA);
89+
componentInfoA);
9090
assertThat(componentManager.getComponentInfosByApplicationContext(null)).containsExactly(
91-
componentInfoA, componentInfoB);
91+
componentInfoA, componentInfoB);
9292
}
9393

9494
@Test
@@ -97,12 +97,12 @@ public void unRegister() {
9797
componentManager.register(componentInfoA);
9898
assertThat(componentManager.getComponents()).contains(componentInfoA);
9999
assertThat(componentManager.getComponentInfosByType(componentInfoA.getType()))
100-
.containsExactly(componentInfoA);
100+
.containsExactly(componentInfoA);
101101

102102
componentManager.unregister(componentInfoA);
103103
assertThat(componentManager.getComponents()).doesNotContain(componentInfoA);
104104
assertThat(componentManager.getComponentInfosByType(componentInfoA.getType()))
105-
.doesNotContain(componentInfoA);
105+
.doesNotContain(componentInfoA);
106106
}
107107

108108
@Test
@@ -138,31 +138,38 @@ public void registerException(CapturedOutput capturedOutput) {
138138
componentInfoA = new DemoComponent("A");
139139
componentInfoA.setRegisterException(true);
140140

141-
assertThat(componentManager.registerAndGet(componentInfoA)).isNull();
142-
assertThat(capturedOutput.getOut()).contains("01-03003");
143-
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
141+
try {
142+
assertThat(componentManager.registerAndGet(componentInfoA)).isNull();
143+
} catch (ServiceRuntimeException e) {
144+
assertThat(capturedOutput.getOut()).contains("01-03003");
145+
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
146+
}
144147
}
145148

146149
@Test
147150
public void resolveException(CapturedOutput capturedOutput) {
148151
componentInfoA = new DemoComponent("A");
149152
componentInfoA.setResolveException(true);
150153

151-
assertThat(componentManager.registerAndGet(componentInfoA)).isEqualTo(componentInfoA);
152-
assertThat(componentInfoA.isHealthy().isHealthy()).isFalse();
153-
assertThat(capturedOutput.getOut()).contains("01-03004");
154-
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
154+
try {
155+
assertThat(componentManager.registerAndGet(componentInfoA)).isEqualTo(componentInfoA);
156+
157+
} catch (ServiceRuntimeException e) {
158+
assertThat(componentInfoA.isHealthy().isHealthy()).isFalse();
159+
assertThat(capturedOutput.getOut()).contains("01-03004");
160+
assertThat(capturedOutput.getOut()).contains(componentInfoA.getName().toString());
161+
}
155162
}
156163

157164
@Test
158165
public void normalShutdown() {
159166
ComponentManager componentManager = initComponentManager(false, false);
160167
ComponentInfo demoComponentInfo = componentManager
161-
.getComponentInfosByType(DEMO_COMPONENT_TYPE).stream().findFirst().get();
168+
.getComponentInfosByType(DEMO_COMPONENT_TYPE).stream().findFirst().get();
162169
ComponentInfo springComponentInfo = componentManager
163-
.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
170+
.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
164171
GenericApplicationContext applicationContext = (GenericApplicationContext) springComponentInfo
165-
.getImplementation().getTarget();
172+
.getImplementation().getTarget();
166173

167174
assertThat(componentManager.size()).isEqualTo(2);
168175
assertThat(demoComponentInfo.isActivated()).isTrue();
@@ -179,11 +186,11 @@ public void normalShutdown() {
179186
public void skipAllComponentShutdown() {
180187
ComponentManager componentManager = initComponentManager(true, false);
181188
ComponentInfo demoComponentInfo = componentManager
182-
.getComponentInfosByType(DEMO_COMPONENT_TYPE).stream().findFirst().get();
189+
.getComponentInfosByType(DEMO_COMPONENT_TYPE).stream().findFirst().get();
183190
ComponentInfo springComponentInfo = componentManager
184-
.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
191+
.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
185192
GenericApplicationContext applicationContext = (GenericApplicationContext) springComponentInfo
186-
.getImplementation().getTarget();
193+
.getImplementation().getTarget();
187194

188195
assertThat(componentManager.size()).isEqualTo(2);
189196
assertThat(demoComponentInfo.isActivated()).isTrue();
@@ -200,11 +207,11 @@ public void skipAllComponentShutdown() {
200207
public void skipCommonComponentShutdown() {
201208
ComponentManager componentManager = initComponentManager(false, true);
202209
ComponentInfo demoComponentInfo = componentManager
203-
.getComponentInfosByType(DEMO_COMPONENT_TYPE).stream().findFirst().get();
210+
.getComponentInfosByType(DEMO_COMPONENT_TYPE).stream().findFirst().get();
204211
ComponentInfo springComponentInfo = componentManager
205-
.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
212+
.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
206213
GenericApplicationContext applicationContext = (GenericApplicationContext) springComponentInfo
207-
.getImplementation().getTarget();
214+
.getImplementation().getTarget();
208215

209216
assertThat(componentManager.size()).isEqualTo(2);
210217
assertThat(demoComponentInfo.isActivated()).isTrue();
@@ -219,7 +226,7 @@ public void skipCommonComponentShutdown() {
219226

220227
private ComponentManager initComponentManager(boolean skipAll, boolean skipComponent) {
221228
StandardSofaRuntimeManager sofaRuntimeManager = new StandardSofaRuntimeManager("testApp",
222-
this.getClass().getClassLoader(), clientFactory);
229+
this.getClass().getClassLoader(), clientFactory);
223230
ComponentManager componentManager = sofaRuntimeManager.getComponentManager();
224231
SofaRuntimeContext sofaRuntimeContext = sofaRuntimeManager.getSofaRuntimeContext();
225232
sofaRuntimeContext.getProperties().setSkipAllComponentShutdown(skipAll);
@@ -230,9 +237,9 @@ private ComponentManager initComponentManager(boolean skipAll, boolean skipCompo
230237

231238
GenericApplicationContext applicationContext = new GenericApplicationContext();
232239
ComponentName springComponentName = ComponentNameFactory.createComponentName(
233-
SPRING_COMPONENT_TYPE, "testModule");
240+
SPRING_COMPONENT_TYPE, "testModule");
234241
ComponentInfo springComponentInfo = new SpringContextComponent(springComponentName,
235-
new SpringContextImplementation(applicationContext), sofaRuntimeContext);
242+
new SpringContextImplementation(applicationContext), sofaRuntimeContext);
236243
applicationContext.refresh();
237244
componentManager.register(springComponentInfo);
238245

0 commit comments

Comments
 (0)