Skip to content

Commit

Permalink
add mock modules and use to test async plugin (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
observeralone authored Dec 29, 2021
1 parent 07dd271 commit 5768c27
Show file tree
Hide file tree
Showing 29 changed files with 1,103 additions and 0 deletions.
44 changes: 44 additions & 0 deletions mock/config-mock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2021, MegaEase
~ All rights reserved.
~
~ Licensed 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mock</artifactId>
<groupId>com.megaease.easeagent</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>config-mock</artifactId>

<dependencies>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>config</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>log4j2-mock</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed 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.megaease.easeagent.mock.config;

import com.megaease.easeagent.config.ConfigFactory;
import com.megaease.easeagent.config.Configs;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class ConfigMock {
private static final String MOCK_CONFIG_FILE = "mock_agent.properties";
private static final Configs CONFIGS;

static {
Map<String, String> initConfigs = new HashMap<>();
initConfigs.put("name", "demo-service");
initConfigs.put("system", "demo-system");

initConfigs.put("observability.outputServer.timeout", "10000");
initConfigs.put("observability.outputServer.enabled", "true");
initConfigs.put("observability.tracings.output.enabled", "true");
initConfigs.put("plugin.observability.global.tracing.enabled", "true");
initConfigs.put("plugin.observability.global.metric.enabled", "true");
CONFIGS = new Configs(initConfigs);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource(MOCK_CONFIG_FILE);
if (url != null) {
Configs configsFromOuterFile = ConfigFactory.loadFromFile(new File(url.getFile()));
CONFIGS.updateConfigsNotNotify(configsFromOuterFile.getConfigs());
}
}

public static Configs getCONFIGS() {
return CONFIGS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed 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.megaease.easeagent.mock.config;

import org.junit.Test;

import static org.junit.Assert.*;

public class ConfigMockTest {

@Test
public void getCONFIGS() {
assertTrue(ConfigMock.getCONFIGS().getBoolean("test.mock.key"));
assertEquals(ConfigMock.getCONFIGS().getString("test.mock.keyStr"), "testValue");
assertNull(ConfigMock.getCONFIGS().getString("test.mock.keyStrAAAAAAAAAA"));
}
}
2 changes: 2 additions & 0 deletions mock/config-mock/src/test/resources/mock_agent.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test.mock.key=true
test.mock.keyStr=testValue
49 changes: 49 additions & 0 deletions mock/context-mock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2021, MegaEase
~ All rights reserved.
~
~ Licensed 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mock</artifactId>
<groupId>com.megaease.easeagent</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>context-mock</artifactId>


<dependencies>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>context</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>config-mock</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>utils-mock</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed 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.megaease.easeagent.mock.context;

import com.megaease.easeagent.context.ContextManager;
import com.megaease.easeagent.mock.config.ConfigMock;
import com.megaease.easeagent.mock.utils.MockProvider;
import com.megaease.easeagent.plugin.api.Context;
import com.megaease.easeagent.plugin.api.InitializeContext;
import com.megaease.easeagent.plugin.api.metric.MetricProvider;
import com.megaease.easeagent.plugin.api.trace.TracingProvider;
import com.megaease.easeagent.plugin.bridge.EaseAgent;

import java.util.Iterator;
import java.util.ServiceLoader;

public class ContextManagerMock {
private static final ContextManager CONTEXT_MANAGER_MOCK = ContextManager.build(ConfigMock.getCONFIGS());

static {
ServiceLoader<MockProvider> loader = ServiceLoader.load(MockProvider.class);
Iterator<MockProvider> iterator = loader.iterator();
while (iterator.hasNext()) {
MockProvider mockProvider = iterator.next();
Object o = mockProvider.get();
if (o == null) {
continue;
}
if (o instanceof TracingProvider) {
CONTEXT_MANAGER_MOCK.setTracing((TracingProvider) o);
} else if (o instanceof MetricProvider) {
CONTEXT_MANAGER_MOCK.setMetric((MetricProvider) o);
}
}
}

public static ContextManager getContextManagerMock() {
return CONTEXT_MANAGER_MOCK;
}

public static Context getContext() {
return EaseAgent.contextSupplier.get();
}

public static InitializeContext getInitializeContext() {
return EaseAgent.initializeContextSupplier.get();
}
}
49 changes: 49 additions & 0 deletions mock/metrics-mock/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2021, MegaEase
~ All rights reserved.
~
~ Licensed 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mock</artifactId>
<groupId>com.megaease.easeagent</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>metrics-mock</artifactId>


<dependencies>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>metrics</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>report-mock</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.megaease.easeagent</groupId>
<artifactId>utils-mock</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* Licensed 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.megaease.easeagent.mock.metrics;

import com.megaease.easeagent.metrics.MetricProvider;
import com.megaease.easeagent.metrics.jvm.gc.JVMGCMetricV2;
import com.megaease.easeagent.metrics.jvm.memory.JVMMemoryMetricV2;
import com.megaease.easeagent.mock.config.ConfigMock;
import com.megaease.easeagent.mock.report.ReportMock;
import com.megaease.easeagent.mock.utils.MockProvider;

public class MetricProviderMock implements MockProvider {
private static final MetricProvider METRIC_PROVIDER = new MetricProvider();
private static final JVMGCMetricV2 JVMGC_METRIC_V_2;
private static final JVMMemoryMetricV2 JVM_MEMORY_METRIC_V_2;

static {
METRIC_PROVIDER.setConfig(ConfigMock.getCONFIGS());
METRIC_PROVIDER.setAgentReport(ReportMock.getAgentReport());
JVMGC_METRIC_V_2 = METRIC_PROVIDER.jvmGcMetricV2();
JVM_MEMORY_METRIC_V_2 = METRIC_PROVIDER.jvmMemoryMetricV2();
}


public static MetricProvider getMetricProvider() {
return METRIC_PROVIDER;
}

public static JVMGCMetricV2 getJvmgcMetricV2() {
return JVMGC_METRIC_V_2;
}

public static JVMMemoryMetricV2 getJvmMemoryMetricV2() {
return JVM_MEMORY_METRIC_V_2;
}

@Override
public Object get() {
return METRIC_PROVIDER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.megaease.easeagent.mock.metrics.MetricProviderMock
Loading

0 comments on commit 5768c27

Please sign in to comment.