17
17
package org .springframework .cloud .sleuth .instrument .async .issues .issue410 ;
18
18
19
19
import java .lang .invoke .MethodHandles ;
20
+ import java .util .Date ;
20
21
import java .util .concurrent .CompletableFuture ;
21
22
import java .util .concurrent .ExecutionException ;
22
23
import java .util .concurrent .Executor ;
@@ -182,12 +183,35 @@ public void should_pass_tracing_info_for_completable_futures_with_task_scheduler
182
183
* Related to issue #1232
183
184
*/
184
185
@ Test
185
- public void should_pass_tracing_info_for_completable_futures_with_threadPoolTaskScheduler () {
186
+ public void should_pass_tracing_info_for_submitted_tasks_with_threadPoolTaskScheduler () {
186
187
Span span = this .tracer .nextSpan ().name ("foo" );
187
188
log .info ("Starting test" );
188
189
try (Tracer .SpanInScope ws = this .tracer .withSpanInScope (span )) {
189
190
String response = this .restTemplate .getForObject (
190
- "http://localhost:" + port () + "/threadPoolTaskScheduler" ,
191
+ "http://localhost:" + port () + "/threadPoolTaskScheduler_submit" ,
192
+ String .class );
193
+
194
+ then (response ).isEqualTo (span .context ().traceIdString ());
195
+ Awaitility .await ().untilAsserted (() -> {
196
+ then (this .asyncTask .getSpan ().get ()).isNotNull ();
197
+ then (this .asyncTask .getSpan ().get ().context ().traceId ())
198
+ .isEqualTo (span .context ().traceId ());
199
+ });
200
+ }
201
+ finally {
202
+ span .finish ();
203
+ }
204
+
205
+ then (this .tracer .currentSpan ()).isNull ();
206
+ }
207
+
208
+ @ Test
209
+ public void should_pass_tracing_info_for_scheduled_tasks_with_threadPoolTaskScheduler () {
210
+ Span span = this .tracer .nextSpan ().name ("foo" );
211
+ log .info ("Starting test" );
212
+ try (Tracer .SpanInScope ws = this .tracer .withSpanInScope (span )) {
213
+ String response = this .restTemplate .getForObject (
214
+ "http://localhost:" + port () + "/threadPoolTaskScheduler_schedule" ,
191
215
String .class );
192
216
193
217
then (response ).isEqualTo (span .context ().traceIdString ());
@@ -372,7 +396,7 @@ public Span scheduledThreadPoolExecutor()
372
396
return this .span .get ();
373
397
}
374
398
375
- public Span threadPoolTaskScheduler ()
399
+ public Span threadPoolTaskSchedulerSubmit ()
376
400
throws ExecutionException , InterruptedException {
377
401
log .info ("This task is running with ThreadPoolTaskScheduler" );
378
402
this .threadPoolTaskScheduler .submit (() -> {
@@ -382,6 +406,16 @@ public Span threadPoolTaskScheduler()
382
406
return this .span .get ();
383
407
}
384
408
409
+ public Span threadPoolTaskSchedulerSchedule ()
410
+ throws ExecutionException , InterruptedException {
411
+ log .info ("This task is running with ThreadPoolTaskScheduler" );
412
+ this .threadPoolTaskScheduler .schedule (() -> {
413
+ log .info ("Hello from runnable" );
414
+ AsyncTask .this .span .set (AsyncTask .this .tracer .currentSpan ());
415
+ }, new Date ()).get ();
416
+ return this .span .get ();
417
+ }
418
+
385
419
public AtomicReference <Span > getSpan () {
386
420
return this .span ;
387
421
}
@@ -427,11 +461,18 @@ public String taskScheduler() throws ExecutionException, InterruptedException {
427
461
return this .asyncTask .taskScheduler ().context ().traceIdString ();
428
462
}
429
463
430
- @ RequestMapping ("/threadPoolTaskScheduler" )
431
- public String threadPoolTaskScheduler ()
464
+ @ RequestMapping ("/threadPoolTaskScheduler_submit" )
465
+ public String threadPoolTaskSchedulerSubmit ()
466
+ throws ExecutionException , InterruptedException {
467
+ log .info ("Executing completable via ThreadPoolTaskScheduler" );
468
+ return this .asyncTask .threadPoolTaskSchedulerSubmit ().context ().traceIdString ();
469
+ }
470
+
471
+ @ RequestMapping ("/threadPoolTaskScheduler_schedule" )
472
+ public String threadPoolTaskSchedulerSchedule ()
432
473
throws ExecutionException , InterruptedException {
433
474
log .info ("Executing completable via ThreadPoolTaskScheduler" );
434
- return this .asyncTask .threadPoolTaskScheduler ().context ().traceIdString ();
475
+ return this .asyncTask .threadPoolTaskSchedulerSchedule ().context ().traceIdString ();
435
476
}
436
477
437
478
@ RequestMapping ("/scheduledThreadPoolExecutor" )
0 commit comments