Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/main' into feat-merge-dy…
Browse files Browse the repository at this point in the history
…namic-record
  • Loading branch information
lucas-myx committed Jan 15, 2024
2 parents bafb0e1 + 664afac commit 7322219
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 39 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

name: Build and Test

on: [pull_request, workflow_dispatch]
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build-source:
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.arex.agent.bootstrap;

import static org.junit.jupiter.api.Assertions.*;

import java.lang.instrument.Instrumentation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* @since 2024/1/15
*/
@ExtendWith(MockitoExtension.class)
class InstrumentationHolderTest {

@Mock
Instrumentation instrumentation;
@Mock
ClassLoader agentClassLoader;

@Test
void getInstrumentation() {
InstrumentationHolder.setInstrumentation(instrumentation);
assertEquals(instrumentation, InstrumentationHolder.getInstrumentation());
}

@Test
void getAgentClassLoader() {
InstrumentationHolder.setAgentClassLoader(agentClassLoader);
assertEquals(agentClassLoader, InstrumentationHolder.getAgentClassLoader());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.arex.agent.bootstrap;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

/**
* @since 2024/1/15
*/
class TraceContextManagerTest {

@Test
void test() {
TraceContextManager.init("test-ip");
String get1 = TraceContextManager.get(true);
String get2 = TraceContextManager.get();
assertEquals(get1, get2);

TraceContextManager.set(get2 + "-1");
String get3 = TraceContextManager.get();
assertEquals(get2 + "-1", get3);

String get4 = TraceContextManager.remove();
assertEquals(get3, get4);

String get5 = TraceContextManager.generateId();
assertTrue(get5.startsWith("AREX-test-ip-"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
class CallableWrapperTest {

@Test
void get() {
void get() throws Exception {
assertNull(CallableWrapper.get(null));
TraceContextManager.set("mock");
assertNotNull(CallableWrapper.get(new CallableTest<>()));
assertNotNull(CallableWrapper.get(() -> "mock"));
Callable<Object> objectCallable = CallableWrapper.get(new CallableTest<>());
assertNotNull(objectCallable);
Callable<String> stringCallable = CallableWrapper.get(() -> "mock");
assertEquals("mock", stringCallable.call());
assertNotNull(stringCallable.toString());
assertTrue(stringCallable.hashCode() > 0);
assertFalse(stringCallable.equals(objectCallable));
TraceContextManager.remove();
}

Expand All @@ -26,4 +31,4 @@ public final void setRawResult(T v) {}
public final boolean exec() { return true; }
public final T call() { return null; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ class RunnableWrapperTest {
void get() {
assertNull(RunnableWrapper.get(null));
TraceContextManager.set("mock");
assertNotNull(RunnableWrapper.get(new RunnableTest<>()));
assertNotNull(RunnableWrapper.get(() -> {}));
Runnable objectRunnable = RunnableWrapper.get(new RunnableTest<>());
assertNotNull(objectRunnable);
Runnable emptyRunnable = RunnableWrapper.get(() -> {});
assertDoesNotThrow(emptyRunnable::run);
assertNotNull(emptyRunnable.toString());
assertTrue(emptyRunnable.hashCode() > 0);
assertFalse(emptyRunnable.equals(objectRunnable));
TraceContextManager.remove();
}

Expand All @@ -25,4 +30,4 @@ public final void setRawResult(T v) {}
public final boolean exec() { return true; }
public final void run() {}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.arex.foundation.services;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @since 2024/1/15
*/
class TimerServiceTest {

@Test
void schedule() {
ScheduledFuture<?> scheduleTest = TimerService.schedule(() -> System.out.println("schedule test"), 0,
TimeUnit.MILLISECONDS);
Assertions.assertNotNull(scheduleTest);
scheduleTest.cancel(true);
}

@Test
void scheduleAtFixedRate() throws InterruptedException {
AtomicInteger runs = new AtomicInteger();
ScheduledFuture<?> scheduleTest = TimerService.scheduleAtFixedRate(() -> {
if (runs.get() >= 1) {
throw new RuntimeException("scheduleAtFixedRate test stop");
}
System.out.println("scheduleAtFixedRate test");
runs.getAndIncrement();
}, 0,
50, TimeUnit.MILLISECONDS);
Assertions.assertNotNull(scheduleTest);
scheduleTest.cancel(true);
TimeUnit.MILLISECONDS.sleep(150);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import com.arextest.common.annotation.ArexMock;
import io.arex.agent.bootstrap.model.MockResult;
import io.arex.inst.dynamic.common.DynamiConstants;
import io.arex.inst.dynamic.common.DynamicConstants;
import io.arex.inst.dynamic.common.DynamicClassExtractor;
import io.arex.inst.extension.MethodInstrumentation;
import io.arex.inst.extension.TypeInstrumentation;
Expand All @@ -29,15 +29,15 @@ public class ArexMockInstrumentation extends TypeInstrumentation {

@Override
protected ElementMatcher<TypeDescription> typeMatcher() {
return inheritsAnnotation(named(DynamiConstants.AREX_MOCK));
return inheritsAnnotation(named(DynamicConstants.AREX_MOCK));
}

@Override
public List<MethodInstrumentation> methodAdvices() {
Junction<MethodDescription> matcher = isMethod()
.and(not(returns(TypeDescription.VOID)))
.and(isAnnotatedWith(named(DynamiConstants.AREX_MOCK)))
.and(not(isAnnotatedWith(named(DynamiConstants.SPRING_CACHE))));
.and(isAnnotatedWith(named(DynamicConstants.AREX_MOCK)))
.and(not(isAnnotatedWith(named(DynamicConstants.SPRING_CACHE))));
MethodInstrumentation method = new MethodInstrumentation(matcher,
ArexMockAdvice.class.getName());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.arex.inst.dynamic.common;

public class DynamiConstants {
public class DynamicConstants {

public static final String AREX_MOCK = "com.arextest.common.annotation.ArexMock";
public static final String SPRING_CACHE = "org.springframework.cache.annotation.Cacheable";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.arex.inst.dynamic;

import io.arex.agent.bootstrap.util.CollectionUtil;
import io.arex.inst.dynamic.common.DynamiConstants;
import io.arex.inst.dynamic.common.DynamicConstants;
import io.arex.inst.dynamic.common.DynamicClassExtractor;
import io.arex.inst.runtime.config.Config;

Expand Down Expand Up @@ -107,7 +107,7 @@ public List<MethodInstrumentation> methodAdvices() {
ElementMatcher.Junction<MethodDescription> matcher = null;
if (onlyClass != null) {
matcher = isMethod().and(isPublic()).and(not(takesNoArguments()))
.and(not(isAnnotatedWith(namedOneOf(DynamiConstants.SPRING_CACHE, DynamiConstants.AREX_MOCK))));
.and(not(isAnnotatedWith(namedOneOf(DynamicConstants.SPRING_CACHE, DynamicConstants.AREX_MOCK))));
if (isNotAbstractClass(onlyClass.getClazzName())) {
matcher = matcher.and(not(isOverriddenFrom(namedOneOf(Config.get().getDynamicAbstractClassList()))));
}
Expand All @@ -129,7 +129,7 @@ public List<MethodInstrumentation> methodAdvices() {
private ElementMatcher.Junction<MethodDescription> builderMethodMatcher(DynamicClassEntity entity) {
ElementMatcher.Junction<MethodDescription> matcher =
parseTypeMatcher(entity.getOperation(), this::parseMethodMatcher)
.and(not(isAnnotatedWith(namedOneOf(DynamiConstants.SPRING_CACHE, DynamiConstants.AREX_MOCK))));
.and(not(isAnnotatedWith(namedOneOf(DynamicConstants.SPRING_CACHE, DynamicConstants.AREX_MOCK))));
if (CollectionUtil.isNotEmpty(entity.getParameters())) {
matcher = matcher.and(takesArguments(entity.getParameters().size()));
for (int i = 0; i < entity.getParameters().size(); i++) {
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@
**/test/**,
**/*Test.java,
**/*Transformer.java,
**/*Constants.java,
**/target/**,
**/model/**,
**/constants/**,
**/context/**,
**/foundation/internal/**,
**/bootstrap/internal/**,
**/wrapper/**,
**/thirdparty/**,
Expand Down

0 comments on commit 7322219

Please sign in to comment.