forked from sofastack/sofa-serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ColdClear
committed
Sep 10, 2023
1 parent
8eac006
commit 289d507
Showing
10 changed files
with
161 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
...t-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/BaseIndicatorTest.java
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
.../arklet-core/src/test/java/com/alipay/sofa/serverless/arklet/core/health/HealthTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.alipay.sofa.serverless.arklet.core.health; | ||
|
||
import com.alipay.sofa.serverless.arklet.core.ArkletComponentRegistry; | ||
import com.alipay.sofa.serverless.arklet.core.BaseTest; | ||
import com.alipay.sofa.serverless.arklet.core.health.custom.CustomIndicator; | ||
import com.alipay.sofa.serverless.arklet.core.health.model.Constants; | ||
import com.alipay.sofa.serverless.arklet.core.health.model.Health; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class HealthTests extends BaseTest { | ||
|
||
private HealthService healthService; | ||
|
||
private void validateHealth(Health health, final String[] expectedMetrics) { | ||
Assert.assertTrue(health != null && !health.getHealthData().isEmpty()); | ||
Map<String, Object> healthData = health.getHealthData(); | ||
for (String metric: expectedMetrics) { | ||
Assert.assertTrue(healthData.containsKey(metric) && !((Map<?, ?>) healthData.get(metric)).isEmpty()); | ||
} | ||
} | ||
|
||
private void validateHealth(Health health, String errorCode, String errorMessage) { | ||
Assert.assertTrue(health != null && !health.getHealthData().isEmpty()); | ||
Assert.assertTrue(health.getHealthData().containsKey(errorCode)); | ||
Assert.assertEquals(health.getHealthData().get(errorCode), errorMessage); | ||
} | ||
|
||
@Before | ||
public void initHealthService() throws IOException { | ||
this.healthService = ArkletComponentRegistry.getHealthServiceInstance(); | ||
|
||
// ClassLoader cl = Thread.currentThread().getContextClassLoader(); | ||
// URL testBiz = cl.getResource("test-biz.jar"); | ||
// BizOperation bizOperation = new BizOperation(); | ||
// bizOperation.setBizVersion("test version"); | ||
// ArkClient.getBizFactoryService().createBiz(bizOperation, new File(testBiz.getFile())); | ||
} | ||
|
||
@Test | ||
public void registerCustomCIndicator() { | ||
healthService.registerIndicator(new CustomIndicator()); | ||
CustomIndicator indicator = (CustomIndicator) healthService.getIndicator("custom"); | ||
Assert.assertNotNull(indicator); | ||
} | ||
|
||
@Test | ||
public void testGetHealth() { | ||
final String[] allMetrics = new String[]{Constants.CPU, Constants.JVM}; | ||
final String[] testMetrics = new String[]{Constants.CPU}; | ||
final String[] errorMetrics = new String[]{"nonMetrics"}; | ||
validateHealth(healthService.getHealth(), allMetrics); | ||
validateHealth(healthService.getHealth(new String[0]), allMetrics); | ||
validateHealth(healthService.getHealth(testMetrics), testMetrics); | ||
validateHealth(healthService.getHealth(errorMetrics), Constants.HEALTH_ERROR, "indicator not registered"); | ||
} | ||
|
||
@Test | ||
public void testIndicators() { | ||
Assert.assertNotNull(healthService.getIndicator(Constants.CPU)); | ||
Assert.assertNotNull(healthService.getIndicator(Constants.JVM)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...e/src/test/java/com/alipay/sofa/serverless/arklet/core/health/custom/CustomIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.alipay.sofa.serverless.arklet.core.health.custom; | ||
|
||
import com.alipay.sofa.serverless.arklet.core.health.indicator.ArkletBaseIndicator; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CustomIndicator extends ArkletBaseIndicator { | ||
public CustomIndicator() { | ||
super("custom"); | ||
} | ||
|
||
@Override | ||
protected Map<String, Object> getHealthDetails() { | ||
Map<String, Object> cpuHealthDetails = new HashMap<>(); | ||
cpuHealthDetails.put("key", "value"); | ||
return cpuHealthDetails; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...src/test/java/com/alipay/sofa/serverless/arklet/core/health/indicator/IndicatorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.alipay.sofa.serverless.arklet.core.health.indicator; | ||
|
||
import com.alipay.sofa.serverless.arklet.core.health.model.Health.HealthBuilder; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
public class IndicatorTests { | ||
|
||
@Test | ||
public void testCpuIndicator() { | ||
ArkletBaseIndicator indicator = new CpuIndicator(); | ||
final String[] indicatorMetrics = new String[]{"count", "type", "total used (%)", "user used (%)", "system used (%)", "free (%)"}; | ||
final String indicatorId = "cpu"; | ||
Map<String, Object> indicatorData = indicator.getHealthDetails(); | ||
Assert.assertEquals(indicator.getIndicatorId(), indicatorId); | ||
Assert.assertNotNull(indicatorData); | ||
Assert.assertNotNull(indicator.getHealthModel(new HealthBuilder())); | ||
for (String indicatorMetric: indicatorMetrics) { | ||
Assert.assertNotNull(indicatorData.get(indicatorMetric)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testJvmIndicator() { | ||
ArkletBaseIndicator indicator = new JvmIndicator(); | ||
final String[] indicatorMetrics = new String[]{"java version", "java home", "total memory(M)", "max memory(M)", | ||
"free memory(M)", "run time(s)", "init heap memory(M)", "used heap memory(M)", "committed heap memory(M)", | ||
"max heap memory(M)", "init non heap memory(M)", "used non heap memory(M)", "committed non heap memory(M)", | ||
"max non heap memory(M)", "loaded class count", "unload class count", "total class count"}; | ||
final String indicatorId = "jvm"; | ||
Map<String, Object> indicatorData = indicator.getHealthDetails(); | ||
Assert.assertEquals(indicator.getIndicatorId(), indicatorId); | ||
Assert.assertNotNull(indicatorData); | ||
Assert.assertNotNull(indicator.getHealthModel(new HealthBuilder())); | ||
for (String indicatorMetric: indicatorMetrics) { | ||
Assert.assertNotNull(indicatorData.get(indicatorMetric)); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters