@@ -33,24 +33,31 @@ void shouldReturnSameMetricsInstanceUnderConcurrentAccess() throws InterruptedEx
3333 CountDownLatch startLatch = new CountDownLatch (1 );
3434 List <Metrics > results = new CopyOnWriteArrayList <>();
3535
36- // when
37- for (int i = 0 ; i < threadCount ; i ++) {
38- executor .submit (() -> {
39- try {
40- startLatch .await ();
41- results .add (telemetry .metrics ());
42- } catch (InterruptedException e ) {
43- Thread .currentThread ().interrupt ();
44- }
45- });
36+ try {
37+ // when
38+ for (int i = 0 ; i < threadCount ; i ++) {
39+ executor .submit (() -> {
40+ try {
41+ startLatch .await ();
42+ results .add (telemetry .metrics ());
43+ } catch (InterruptedException e ) {
44+ Thread .currentThread ().interrupt ();
45+ }
46+ });
47+ }
48+ startLatch .countDown ();
49+ executor .shutdown ();
50+ boolean terminated = executor .awaitTermination (5 , java .util .concurrent .TimeUnit .SECONDS );
51+ assertThat (terminated ).isTrue ();
52+
53+ // then
54+ assertThat (results ).hasSize (threadCount );
55+ Metrics expected = results .get (0 );
56+ assertThat (results ).allSatisfy (m -> assertThat (m ).isSameAs (expected ));
57+ } finally {
58+ if (!executor .isTerminated ()) {
59+ executor .shutdownNow ();
60+ }
4661 }
47- startLatch .countDown ();
48- executor .shutdown ();
49- executor .awaitTermination (5 , java .util .concurrent .TimeUnit .SECONDS );
50-
51- // then
52- assertThat (results ).hasSize (threadCount );
53- Metrics expected = results .get (0 );
54- assertThat (results ).allSatisfy (m -> assertThat (m ).isSameAs (expected ));
5562 }
5663}
0 commit comments