1616import com .sun .net .httpserver .HttpServer ;
1717import io .opentelemetry .api .common .AttributeKey ;
1818import io .opentelemetry .api .trace .SpanKind ;
19- import io .opentelemetry .api .trace .StatusCode ;
2019import io .opentelemetry .instrumentation .test .utils .PortUtils ;
2120import io .opentelemetry .instrumentation .testing .junit .AgentInstrumentationExtension ;
2221import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
2322import io .opentelemetry .sdk .trace .data .SpanData ;
23+ import io .opentelemetry .sdk .trace .data .StatusData ;
24+ import java .io .File ;
2425import java .io .IOException ;
26+ import java .io .InputStream ;
2527import java .net .HttpURLConnection ;
2628import java .net .InetSocketAddress ;
2729import java .nio .file .Files ;
2830import java .nio .file .Path ;
29- import java .nio .file .Paths ;
30- import java .nio .file .attribute .PosixFilePermissions ;
31+ import java .nio .file .StandardCopyOption ;
3132import java .time .Duration ;
3233import java .util .List ;
34+ import java .util .Locale ;
3335import java .util .Set ;
3436import java .util .concurrent .CountDownLatch ;
3537import java .util .concurrent .TimeUnit ;
4345import org .apache .shardingsphere .elasticjob .reg .zookeeper .ZookeeperRegistryCenter ;
4446import org .apache .shardingsphere .elasticjob .script .props .ScriptJobProperties ;
4547import org .junit .jupiter .api .AfterAll ;
46- import org .junit .jupiter .api .AfterEach ;
4748import org .junit .jupiter .api .BeforeAll ;
4849import org .junit .jupiter .api .Test ;
49- import org .junit .jupiter .api .condition .DisabledOnOs ;
50- import org .junit .jupiter .api .condition .OS ;
5150import org .junit .jupiter .api .extension .RegisterExtension ;
5251
5352public 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 {
0 commit comments