Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,46 @@ dependencies {
testInstrumentation(project(":instrumentation:xxl-job:xxl-job-2.3.0:javaagent"))

testImplementation(project(":instrumentation:xxl-job:xxl-job-common:testing"))

// latest version is tested in a separate test suite
latestDepTestLibrary("com.xuxueli:xxl-job-core:3.2.+") // documented limitation
}

val testLatestDeps = findProperty("testLatestDeps") as Boolean

testing {
suites {
val xxlJob33Test by registering(JvmTestSuite::class) {
dependencies {
val version = if (testLatestDeps) "latest.release" else "3.3.0"
implementation("com.xuxueli:xxl-job-core:$version")
implementation(project(":instrumentation:xxl-job:xxl-job-common:testing"))
}
}
}
}

tasks.withType<Test>().configureEach {
// required on jdk17
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
jvmArgs("-Dotel.instrumentation.xxl-job.experimental-span-attributes=true")
tasks {
withType<Test>().configureEach {
// required on jdk17
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")
jvmArgs("-Dotel.instrumentation.xxl-job.experimental-span-attributes=true")
}

named("compileXxlJob33TestJava", JavaCompile::class).configure {
options.release.set(17)
}
val testJavaVersion =
gradle.startParameter.projectProperties.get("testJavaVersion")?.let(JavaVersion::toVersion)
?: JavaVersion.current()
if (!testJavaVersion.isCompatibleWith(JavaVersion.VERSION_17)) {
named("xxlJob33Test", Test::class).configure {
enabled = false
}
}

check {
dependsOn(testing.suites)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class CustomizedFailedHandler extends IJobHandler {

@Override
public void execute() throws Exception {
public void execute() {
XxlJobHelper.handleFail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.xxljob.v3_3_0;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;

class CustomizedFailedHandler extends IJobHandler {

@Override
public void execute() {
XxlJobHelper.handleFail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.xxljob.v3_3_0;

import com.xxl.tool.response.Response;
import java.lang.reflect.Method;

class ReflectiveMethodsFactory {

private ReflectiveMethodsFactory() {}

public static class ReflectObject {

private ReflectObject() {}

public void initMethod() {}

public void destroyMethod() {}

public Response<String> echo(String param) {
Response<String> result = new Response<>();
result.setData("echo: " + param);
return result;
}
}

private static final Object SINGLETON_OBJECT = new ReflectObject();

static Object getTarget() {
return SINGLETON_OBJECT;
}

static Method getMethod() {
try {
return SINGLETON_OBJECT.getClass().getMethod("echo", String.class);
} catch (Throwable t) {
// Ignore
}
return null;
}

static Method getInitMethod() {
try {
return SINGLETON_OBJECT.getClass().getMethod("initMethod");
} catch (Throwable t) {
// Ignore
}
return null;
}

static Method getDestroyMethod() {
try {
return SINGLETON_OBJECT.getClass().getMethod("destroyMethod");
} catch (Throwable t) {
// Ignore
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.xxljob.v3_3_0;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.IJobHandler;

class SimpleCustomizedHandler extends IJobHandler {

@Override
public void execute() {
XxlJobHelper.handleSuccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.xxljob.v3_3_0;

import static io.opentelemetry.instrumentation.xxljob.XxlJobTestingConstants.DEFAULT_GLUE_UPDATE_TIME;
import static io.opentelemetry.instrumentation.xxljob.XxlJobTestingConstants.GLUE_JOB_GROOVY_SOURCE;
import static io.opentelemetry.instrumentation.xxljob.XxlJobTestingConstants.GLUE_JOB_SHELL_SCRIPT;

import com.xxl.job.core.glue.GlueFactory;
import com.xxl.job.core.glue.GlueTypeEnum;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.impl.GlueJobHandler;
import com.xxl.job.core.handler.impl.MethodJobHandler;
import com.xxl.job.core.handler.impl.ScriptJobHandler;
import com.xxl.job.core.openapi.model.TriggerRequest;
import com.xxl.job.core.thread.JobThread;
import io.opentelemetry.instrumentation.xxljob.AbstractXxlJobTest;

class XxlJobTest extends AbstractXxlJobTest {

private static final MethodJobHandler METHOD_JOB_HANDLER =
new MethodJobHandler(
ReflectiveMethodsFactory.getTarget(),
ReflectiveMethodsFactory.getMethod(),
ReflectiveMethodsFactory.getInitMethod(),
ReflectiveMethodsFactory.getDestroyMethod());

private static final IJobHandler GROOVY_HANDLER;

static {
try {
GROOVY_HANDLER = GlueFactory.getInstance().loadNewInstance(GLUE_JOB_GROOVY_SOURCE);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}

private static final GlueJobHandler GLUE_JOB_HANDLER =
new GlueJobHandler(GROOVY_HANDLER, DEFAULT_GLUE_UPDATE_TIME);

private static final ScriptJobHandler SCRIPT_JOB_HANDLER =
new ScriptJobHandler(
2, DEFAULT_GLUE_UPDATE_TIME, GLUE_JOB_SHELL_SCRIPT, GlueTypeEnum.GLUE_SHELL);

@Override
protected String getPackageName() {
return "io.opentelemetry.javaagent.instrumentation.xxljob.v3_3_0";
}

@Override
protected IJobHandler getGlueJobHandler() {
return GLUE_JOB_HANDLER;
}

@Override
protected IJobHandler getScriptJobHandler() {
return SCRIPT_JOB_HANDLER;
}

@Override
protected IJobHandler getCustomizeHandler() {
return new SimpleCustomizedHandler();
}

@Override
protected IJobHandler getCustomizeFailedHandler() {
return new CustomizedFailedHandler();
}

@Override
protected IJobHandler getMethodHandler() {
return METHOD_JOB_HANDLER;
}

@Override
protected void trigger(JobThread jobThread, String executorParams) {
TriggerRequest triggerParam = new TriggerRequest();
triggerParam.setExecutorTimeout(0);
if (executorParams != null) {
triggerParam.setExecutorParams(executorParams);
}
jobThread.pushTriggerQueue(triggerParam);
jobThread.start();
}

@Override
protected Class<?> getReflectObjectClass() {
return ReflectiveMethodsFactory.ReflectObject.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,24 @@ static void setUp() {
XxlJobFileAppender.initLogPath("build/xxljob/log");
}

@Test
void testGlueJob() {
JobThread jobThread = new JobThread(1, getGlueJobHandler());
private void trigger(JobThread jobThread) {
trigger(jobThread, null);
}

protected void trigger(JobThread jobThread, String executorParams) {
TriggerParam triggerParam = new TriggerParam();
triggerParam.setExecutorTimeout(0);
if (executorParams != null) {
triggerParam.setExecutorParams(executorParams);
}
jobThread.pushTriggerQueue(triggerParam);
jobThread.start();
}

@Test
void testGlueJob() {
JobThread jobThread = new JobThread(1, getGlueJobHandler());
trigger(jobThread);
checkXxlJob(
"CustomizedGroovyHandler.execute",
StatusData.unset(),
Expand All @@ -59,22 +70,15 @@ void testGlueJob() {
@Test
void testScriptJob() {
JobThread jobThread = new JobThread(2, getScriptJobHandler());
TriggerParam triggerParam = new TriggerParam();
triggerParam.setExecutorParams("");
triggerParam.setExecutorTimeout(0);
jobThread.pushTriggerQueue(triggerParam);
jobThread.start();
trigger(jobThread, "");
checkXxlJobWithoutCodeAttributes("GLUE(Shell)", StatusData.unset(), GlueTypeEnum.GLUE_SHELL, 2);
jobThread.toStop("Test finish");
}

@Test
void testSimpleJob() {
JobThread jobThread = new JobThread(3, getCustomizeHandler());
TriggerParam triggerParam = new TriggerParam();
triggerParam.setExecutorTimeout(0);
jobThread.pushTriggerQueue(triggerParam);
jobThread.start();
trigger(jobThread);
checkXxlJob(
"SimpleCustomizedHandler.execute",
StatusData.unset(),
Expand All @@ -84,32 +88,30 @@ void testSimpleJob() {
jobThread.toStop("Test finish");
}

protected Class<?> getReflectObjectClass() {
return ReflectiveMethodsFactory.ReflectObject.class;
}

@Test
public void testMethodJob() {
// method handle is null if test is not supported by tested version of the library
Assumptions.assumeTrue(getMethodHandler() != null);

JobThread jobThread = new JobThread(4, getMethodHandler());
TriggerParam triggerParam = new TriggerParam();
triggerParam.setExecutorTimeout(0);
jobThread.pushTriggerQueue(triggerParam);
jobThread.start();
trigger(jobThread);
checkXxlJob(
"ReflectObject.echo",
StatusData.unset(),
GlueTypeEnum.BEAN,
"io.opentelemetry.instrumentation.xxljob.ReflectiveMethodsFactory$ReflectObject",
getReflectObjectClass().getName(),
"echo");
jobThread.toStop("Test finish");
}

@Test
void testFailedJob() {
JobThread jobThread = new JobThread(5, getCustomizeFailedHandler());
TriggerParam triggerParam = new TriggerParam();
triggerParam.setExecutorTimeout(0);
jobThread.pushTriggerQueue(triggerParam);
jobThread.start();
trigger(jobThread);
checkXxlJob(
"CustomizedFailedHandler.execute",
StatusData.error(),
Expand Down
Loading