Skip to content

Commit 29f5b47

Browse files
committed
refactor: improve ElasticJobTest and EmbedZookeeperServer for better error handling and OS compatibility
1 parent b751d37 commit 29f5b47

File tree

2 files changed

+76
-82
lines changed

2 files changed

+76
-82
lines changed

instrumentation/apache-elasticjob-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/ElasticJobTest.java

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@
1616
import com.sun.net.httpserver.HttpServer;
1717
import io.opentelemetry.api.common.AttributeKey;
1818
import io.opentelemetry.api.trace.SpanKind;
19-
import io.opentelemetry.api.trace.StatusCode;
2019
import io.opentelemetry.instrumentation.test.utils.PortUtils;
2120
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
2221
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
2322
import io.opentelemetry.sdk.trace.data.SpanData;
23+
import io.opentelemetry.sdk.trace.data.StatusData;
24+
import java.io.File;
2425
import java.io.IOException;
26+
import java.io.InputStream;
2527
import java.net.HttpURLConnection;
2628
import java.net.InetSocketAddress;
2729
import java.nio.file.Files;
2830
import java.nio.file.Path;
29-
import java.nio.file.Paths;
30-
import java.nio.file.attribute.PosixFilePermissions;
31+
import java.nio.file.StandardCopyOption;
3132
import java.time.Duration;
3233
import java.util.List;
34+
import java.util.Locale;
3335
import java.util.Set;
3436
import java.util.concurrent.CountDownLatch;
3537
import java.util.concurrent.TimeUnit;
@@ -43,11 +45,8 @@
4345
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
4446
import org.apache.shardingsphere.elasticjob.script.props.ScriptJobProperties;
4547
import org.junit.jupiter.api.AfterAll;
46-
import org.junit.jupiter.api.AfterEach;
4748
import org.junit.jupiter.api.BeforeAll;
4849
import org.junit.jupiter.api.Test;
49-
import org.junit.jupiter.api.condition.DisabledOnOs;
50-
import org.junit.jupiter.api.condition.OS;
5150
import org.junit.jupiter.api.extension.RegisterExtension;
5251

5352
public class ElasticJobTest {
@@ -84,16 +83,13 @@ public void handle(HttpExchange exchange) throws IOException {
8483

8584
@AfterAll
8685
public static void stop() {
87-
httpServer.stop(0);
88-
}
89-
90-
@AfterEach
91-
public void clearSpans() {
92-
try {
93-
Thread.sleep(100L);
94-
} catch (InterruptedException var2) {
95-
Thread.currentThread().interrupt();
86+
if (httpServer != null) {
87+
httpServer.stop(0);
9688
}
89+
if (regCenter != null) {
90+
regCenter.close();
91+
}
92+
EmbedZookeeperServer.stop();
9793
}
9894

9995
@Test
@@ -273,7 +269,6 @@ public void testDataflowJob() throws InterruptedException {
273269
}
274270

275271
@Test
276-
@DisabledOnOs(OS.WINDOWS)
277272
public void testScriptJob() throws IOException {
278273
ScheduleJobBootstrap bootstrap = setUpScriptJob(regCenter);
279274
try {
@@ -286,12 +281,10 @@ public void testScriptJob() throws IOException {
286281
.hasAttributesSatisfyingExactly(
287282
equalTo(AttributeKey.stringKey("job.system"), "elasticjob"),
288283
equalTo(
289-
AttributeKey.stringKey(
290-
"scheduling.apache-elasticjob.job.name"),
284+
AttributeKey.stringKey("scheduling.apache-elasticjob.job.name"),
291285
"scriptElasticJob"),
292286
equalTo(
293-
AttributeKey.longKey("scheduling.apache-elasticjob.item"),
294-
0L),
287+
AttributeKey.longKey("scheduling.apache-elasticjob.item"), 0L),
295288
equalTo(
296289
AttributeKey.longKey(
297290
"scheduling.apache-elasticjob.sharding.total.count"),
@@ -301,8 +294,7 @@ public void testScriptJob() throws IOException {
301294
"scheduling.apache-elasticjob.sharding.item.parameters"),
302295
"{0=null}"),
303296
satisfies(
304-
AttributeKey.stringKey(
305-
"scheduling.apache-elasticjob.task.id"),
297+
AttributeKey.stringKey("scheduling.apache-elasticjob.task.id"),
306298
taskId -> taskId.contains("scriptElasticJob")))));
307299
} finally {
308300
bootstrap.shutdown();
@@ -326,53 +318,40 @@ public void testFailedJob() throws InterruptedException {
326318
assertThat(job.awaitExecution(10, TimeUnit.SECONDS))
327319
.as("Job should execute within timeout")
328320
.isTrue();
329-
await().atMost(Duration.ofSeconds(2)).until(() -> testing.spans().size() >= 1);
321+
322+
testing.waitAndAssertTraces(
323+
trace ->
324+
trace.hasSpansSatisfyingExactly(
325+
span ->
326+
span.hasKind(SpanKind.INTERNAL)
327+
.hasName("SynchronizedTestFailedJob.execute")
328+
.hasStatus(StatusData.error())
329+
.hasException(new RuntimeException("Simulated job failure for testing"))
330+
.hasAttributesSatisfyingExactly(
331+
equalTo(AttributeKey.stringKey("job.system"), "elasticjob"),
332+
equalTo(
333+
AttributeKey.stringKey("scheduling.apache-elasticjob.job.name"),
334+
"failedElasticJob"),
335+
equalTo(
336+
AttributeKey.longKey("scheduling.apache-elasticjob.item"), 0L),
337+
equalTo(
338+
AttributeKey.longKey(
339+
"scheduling.apache-elasticjob.sharding.total.count"),
340+
1L),
341+
equalTo(
342+
AttributeKey.stringKey(
343+
"scheduling.apache-elasticjob.sharding.item.parameters"),
344+
"failed"),
345+
satisfies(
346+
AttributeKey.stringKey("scheduling.apache-elasticjob.task.id"),
347+
taskId -> taskId.contains("failedElasticJob")),
348+
equalTo(AttributeKey.stringKey("code.function"), "execute"),
349+
equalTo(
350+
AttributeKey.stringKey("code.namespace"),
351+
"io.opentelemetry.javaagent.instrumentation.apacheelasticjob.v3_0.ElasticJobTest$SynchronizedTestFailedJob"))));
330352
} finally {
331353
bootstrap.shutdown();
332354
}
333-
334-
List<SpanData> spans = testing.spans();
335-
assertThat(spans).hasSize(1);
336-
337-
SpanData span = spans.get(0);
338-
assertThat(span.getName()).isEqualTo("SynchronizedTestFailedJob.execute");
339-
assertThat(span.getKind()).isEqualTo(SpanKind.INTERNAL);
340-
assertThat(span.getStatus().getStatusCode()).isEqualTo(StatusCode.ERROR);
341-
assertThat(span.getAttributes().get(AttributeKey.stringKey("job.system")))
342-
.isEqualTo("elasticjob");
343-
assertThat(
344-
span.getAttributes()
345-
.get(AttributeKey.stringKey("scheduling.apache-elasticjob.job.name")))
346-
.isEqualTo("failedElasticJob");
347-
assertThat(span.getAttributes().get(AttributeKey.longKey("scheduling.apache-elasticjob.item")))
348-
.isEqualTo(0L);
349-
assertThat(
350-
span.getAttributes()
351-
.get(AttributeKey.longKey("scheduling.apache-elasticjob.sharding.total.count")))
352-
.isEqualTo(1L);
353-
assertThat(
354-
span.getAttributes()
355-
.get(
356-
AttributeKey.stringKey(
357-
"scheduling.apache-elasticjob.sharding.item.parameters")))
358-
.isEqualTo("failed");
359-
assertThat(span.getAttributes().get(AttributeKey.stringKey("code.function")))
360-
.isEqualTo("execute");
361-
assertThat(span.getAttributes().get(AttributeKey.stringKey("code.namespace")))
362-
.isEqualTo(
363-
"io.opentelemetry.javaagent.instrumentation.apacheelasticjob.v3_0.ElasticJobTest$SynchronizedTestFailedJob");
364-
365-
assertThat(span.getEvents()).hasSize(1);
366-
assertThat(span.getEvents().get(0).getName()).isEqualTo("exception");
367-
assertThat(
368-
span.getEvents().get(0).getAttributes().get(AttributeKey.stringKey("exception.type")))
369-
.isEqualTo("java.lang.RuntimeException");
370-
assertThat(
371-
span.getEvents()
372-
.get(0)
373-
.getAttributes()
374-
.get(AttributeKey.stringKey("exception.message")))
375-
.isEqualTo("Simulated job failure for testing");
376355
}
377356

378357
private static CoordinatorRegistryCenter setUpRegistryCenter() {
@@ -410,9 +389,26 @@ private static ScheduleJobBootstrap setUpScriptJob(CoordinatorRegistryCenter reg
410389
}
411390

412391
private static String buildScriptCommandLine() throws IOException {
413-
Path result = Paths.get(ElasticJobTest.class.getResource("/script/demo.sh").getPath());
414-
Files.setPosixFilePermissions(result, PosixFilePermissions.fromString("rwxr-xr-x"));
415-
return result.toString();
392+
String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
393+
String scriptName = os.contains("win") ? "/script/demo.bat" : "/script/demo.sh";
394+
String extension = os.contains("win") ? ".bat" : ".sh";
395+
396+
Path tempScript = Files.createTempFile("elasticjob-test-", extension);
397+
tempScript.toFile().deleteOnExit();
398+
399+
try (InputStream scriptStream = ElasticJobTest.class.getResourceAsStream(scriptName)) {
400+
if (scriptStream == null) {
401+
throw new IOException("Script resource not found: " + scriptName);
402+
}
403+
Files.copy(scriptStream, tempScript, StandardCopyOption.REPLACE_EXISTING);
404+
}
405+
406+
File scriptFile = tempScript.toFile();
407+
if (!scriptFile.setExecutable(true)) {
408+
throw new IOException("Failed to set executable permission on script: " + tempScript);
409+
}
410+
411+
return tempScript.toString();
416412
}
417413

418414
static class SynchronizedTestSimpleJob extends TestSimpleJob {

instrumentation/apache-elasticjob-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/EmbedZookeeperServer.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ public static void start(int port) {
1919
new TestingServer(
2020
port, new File(String.format("target/test_zk_data/%s/", System.nanoTime())));
2121
} catch (Exception ex) {
22-
// ignore
23-
} finally {
24-
Runtime.getRuntime()
25-
.addShutdownHook(
26-
new Thread(
27-
() -> {
28-
try {
29-
Thread.sleep(1000L);
30-
testingServer.close();
31-
} catch (InterruptedException | IOException ignore) {
32-
// ignore
33-
}
34-
}));
22+
throw new RuntimeException("Failed to start embedded ZooKeeper server", ex);
23+
}
24+
}
25+
26+
public static void stop() {
27+
if (testingServer != null) {
28+
try {
29+
testingServer.close();
30+
} catch (IOException ex) {
31+
throw new RuntimeException("Failed to stop embedded ZooKeeper server", ex);
32+
}
3533
}
3634
}
3735

0 commit comments

Comments
 (0)