@@ -92,7 +92,7 @@ void testHeartbeatsShouldBeTriggeredAutomaticallyWhenEnabled() throws Exception
9292 @ Test
9393 void testHeartbeatsShouldOccurAtLeastOncePerSecondWhenNotThrottled () throws Exception {
9494 AtomicInteger flushCount = new AtomicInteger ();
95- TraceProcessingWorker worker =
95+ try ( TraceProcessingWorker worker =
9696 new TraceProcessingWorker (
9797 10 ,
9898 mock (HealthMetrics .class ),
@@ -101,12 +101,11 @@ void testHeartbeatsShouldOccurAtLeastOncePerSecondWhenNotThrottled() throws Exce
101101 FAST_LANE ,
102102 1 ,
103103 TimeUnit .NANOSECONDS , // stop heartbeats from being throttled
104- null );
104+ null )) {
105105
106- // processor is started
107- worker .start ();
106+ // processor is started
107+ worker .start ();
108108
109- try {
110109 // heartbeat occurs automatically approximately once per second
111110 // wait 1 second initial delay, then poll up to 1 second
112111 Thread .sleep (1000 );
@@ -116,9 +115,6 @@ void testHeartbeatsShouldOccurAtLeastOncePerSecondWhenNotThrottled() throws Exce
116115 Thread .sleep (50 );
117116 }
118117 assertTrue (flushCount .get () > 1 );
119- } finally {
120- // cleanup
121- worker .close ();
122118 }
123119 }
124120
@@ -129,7 +125,9 @@ void testHeartbeatsShouldOccurAtLeastOncePerSecondWhenNotThrottled() throws Exce
129125 @ Test
130126 void testAFlushShouldClearThePrimaryQueue () {
131127 AtomicInteger flushCount = new AtomicInteger ();
132- TraceProcessingWorker worker =
128+ // prevent heartbeats from helping the flush happen
129+
130+ try (TraceProcessingWorker worker =
133131 new TraceProcessingWorker (
134132 10 ,
135133 mock (HealthMetrics .class ),
@@ -138,9 +136,7 @@ void testAFlushShouldClearThePrimaryQueue() {
138136 FAST_LANE ,
139137 100 ,
140138 TimeUnit .SECONDS , // prevent heartbeats from helping the flush happen
141- null );
142-
143- try {
139+ null )) {
144140 // there is pending work it is completed before a flush
145141 // processing this span will throw an exception, but it should be caught
146142 // and not disrupt the flush
@@ -153,9 +149,6 @@ void testAFlushShouldClearThePrimaryQueue() {
153149 assertTrue (flushed );
154150 assertEquals (1 , flushCount .get ());
155151 assertTrue (worker .getPrimaryQueue ().isEmpty ());
156- } finally {
157- // cleanup
158- worker .close ();
159152 }
160153 }
161154
@@ -195,7 +188,9 @@ void testShouldReportFailureIfSerializationFails(
195188 .when (healthMetrics )
196189 .onFailedSerialize (any (), any ());
197190
198- TraceProcessingWorker worker =
191+ // prevent heartbeats from helping the flush happen
192+
193+ try (TraceProcessingWorker worker =
199194 new TraceProcessingWorker (
200195 10 ,
201196 healthMetrics ,
@@ -204,10 +199,8 @@ void testShouldReportFailureIfSerializationFails(
204199 FAST_LANE ,
205200 100 ,
206201 TimeUnit .SECONDS , // prevent heartbeats from helping the flush happen
207- null );
208- worker .start ();
209-
210- try {
202+ null )) {
203+ worker .start ();
211204 // a trace is processed but can't be passed on
212205 worker .publish (mock (DDSpan .class ), priority , Collections .singletonList (mock (DDSpan .class )));
213206
@@ -218,9 +211,6 @@ void testShouldReportFailureIfSerializationFails(
218211 Thread .sleep (50 );
219212 }
220213 assertEquals (1 , errorReported .get ());
221- } finally {
222- // cleanup
223- worker .close ();
224214 }
225215 }
226216
@@ -276,7 +266,7 @@ void testTraceShouldBePostProcessed() throws Exception {
276266
277267 SpanPostProcessor .Holder .INSTANCE = mockProcessor ;
278268
279- TraceProcessingWorker worker =
269+ try ( TraceProcessingWorker worker =
280270 new TraceProcessingWorker (
281271 10 ,
282272 healthMetrics ,
@@ -285,10 +275,8 @@ void testTraceShouldBePostProcessed() throws Exception {
285275 FAST_LANE ,
286276 100 ,
287277 TimeUnit .SECONDS ,
288- null );
289- worker .start ();
290-
291- try {
278+ null )) {
279+ worker .start ();
292280 // traces are submitted
293281 List <DDSpan > trace = new ArrayList <>();
294282 trace .add (span1 );
@@ -305,9 +293,7 @@ void testTraceShouldBePostProcessed() throws Exception {
305293 assertTrue (processedSpan1 .get ());
306294 assertTrue (processedSpan2 .get ());
307295 } finally {
308- // cleanup
309296 SpanPostProcessor .Holder .INSTANCE = SpanPostProcessor .Holder .NOOP ;
310- worker .close ();
311297 }
312298 }
313299
@@ -350,7 +336,9 @@ void testTracesShouldBeProcessed(
350336 .when (countingDispatcher )
351337 .addTrace (any ());
352338 HealthMetrics healthMetrics = mock (HealthMetrics .class );
353- TraceProcessingWorker worker =
339+ // prevent heartbeats from helping the flush happen
340+
341+ try (TraceProcessingWorker worker =
354342 new TraceProcessingWorker (
355343 10 ,
356344 healthMetrics ,
@@ -359,10 +347,8 @@ void testTracesShouldBeProcessed(
359347 FAST_LANE ,
360348 100 ,
361349 TimeUnit .SECONDS , // prevent heartbeats from helping the flush happen
362- null );
363- worker .start ();
364-
365- try {
350+ null )) {
351+ worker .start ();
366352 // traces are submitted
367353 int submitted = 0 ;
368354 for (int i = 0 ; i < traceCount ; ++i ) {
@@ -380,10 +366,8 @@ void testTracesShouldBeProcessed(
380366 Thread .sleep (50 );
381367 }
382368 assertEquals (expectedSubmitted , acceptedCount .get ());
383- } finally {
384- // cleanup
385- worker .close ();
386369 }
370+ // cleanup
387371 }
388372
389373 // -------------------------------------------------------------------------
@@ -490,7 +474,7 @@ public <T extends CoreSpan<T>> boolean setSamplingPriority(T span) {
490474 }
491475 };
492476
493- TraceProcessingWorker worker =
477+ try ( TraceProcessingWorker worker =
494478 new TraceProcessingWorker (
495479 10 ,
496480 healthMetrics ,
@@ -499,10 +483,8 @@ public <T extends CoreSpan<T>> boolean setSamplingPriority(T span) {
499483 FAST_LANE ,
500484 100 ,
501485 TimeUnit .SECONDS ,
502- singleSpanSampler );
503- worker .start ();
504-
505- try {
486+ singleSpanSampler )) {
487+ worker .start ();
506488 // traces are submitted
507489 for (int i = 0 ; i < traceCount ; ++i ) {
508490 List <DDSpan > trace = new ArrayList <>();
@@ -523,9 +505,6 @@ public <T extends CoreSpan<T>> boolean setSamplingPriority(T span) {
523505 assertEquals (acceptedTraces , acceptedCount .get ());
524506 assertEquals (acceptedSpans , acceptedSpanCount .get ());
525507 assertEquals (sampledSingleSpans , sampledSpansCount .get ());
526- } finally {
527- // cleanup
528- worker .close ();
529508 }
530509 }
531510
@@ -604,7 +583,7 @@ public <T extends CoreSpan<T>> boolean setSamplingPriority(T span) {
604583 }
605584 };
606585
607- TraceProcessingWorker worker =
586+ try ( TraceProcessingWorker worker =
608587 new TraceProcessingWorker (
609588 10 ,
610589 healthMetrics ,
@@ -613,10 +592,8 @@ public <T extends CoreSpan<T>> boolean setSamplingPriority(T span) {
613592 FAST_LANE ,
614593 100 ,
615594 TimeUnit .SECONDS ,
616- singleSpanSampler );
617- worker .start ();
618-
619- try {
595+ singleSpanSampler )) {
596+ worker .start ();
620597 // traces are submitted
621598 for (int i = 0 ; i < traceCount ; ++i ) {
622599 List <DDSpan > trace = new ArrayList <>();
@@ -637,9 +614,6 @@ public <T extends CoreSpan<T>> boolean setSamplingPriority(T span) {
637614 assertEquals (expectedChunks , chunksCount .get ());
638615 assertEquals (expectedSpans , spansCount .get ());
639616 assertEquals (sampledSingleSpans , sampledSpansCount .get ());
640- } finally {
641- // cleanup
642- worker .close ();
643617 }
644618 }
645619}
0 commit comments