1616 */
1717package org .apache .kafka .streams ;
1818
19- import java .io .IOException ;
20- import java .nio .file .Files ;
21- import java .nio .file .Path ;
22- import java .time .Duration ;
2319import org .apache .kafka .clients .CommonClientConfigs ;
2420import org .apache .kafka .clients .consumer .ConsumerConfig ;
2521import org .apache .kafka .clients .producer .MockProducer ;
6157import org .junit .rules .TestName ;
6258
6359import java .io .File ;
60+ import java .io .IOException ;
61+ import java .nio .file .Files ;
62+ import java .nio .file .Path ;
63+ import java .time .Duration ;
6464import java .util .Collection ;
6565import java .util .Collections ;
6666import java .util .HashMap ;
@@ -215,7 +215,7 @@ public void testStateOneThreadDeadButRebalanceFinish() throws InterruptedExcepti
215215 @ Test
216216 public void shouldCleanupResourcesOnCloseWithoutPreviousStart () throws Exception {
217217 builder .globalTable ("anyTopic" );
218- final List <Node > nodes = asList (new Node (0 , "localhost" , 8121 ));
218+ final List <Node > nodes = Collections . singletonList (new Node (0 , "localhost" , 8121 ));
219219 final Cluster cluster = new Cluster ("mockClusterId" , nodes ,
220220 Collections .emptySet (), Collections .emptySet (),
221221 Collections .emptySet (), nodes .get (0 ));
@@ -326,14 +326,11 @@ public void globalThreadShouldTimeoutWhenBrokerConnectionCannotBeEstablished() {
326326
327327 // make sure we have the global state thread running too
328328 builder .globalTable ("anyTopic" );
329- final KafkaStreams streams = new KafkaStreams (builder .build (), props );
330- try {
329+ try (final KafkaStreams streams = new KafkaStreams (builder .build (), props )) {
331330 streams .start ();
332331 fail ("expected start() to time out and throw an exception." );
333332 } catch (final StreamsException expected ) {
334333 // This is a result of not being able to connect to the broker.
335- } finally {
336- streams .close ();
337334 }
338335 // There's nothing to assert... We're testing that this operation actually completes.
339336 }
@@ -349,11 +346,8 @@ public void testLocalThreadCloseWithoutConnectingToBroker() {
349346
350347 // make sure we have the global state thread running too
351348 builder .table ("anyTopic" );
352- final KafkaStreams streams = new KafkaStreams (builder .build (), props );
353- try {
349+ try (final KafkaStreams streams = new KafkaStreams (builder .build (), props )) {
354350 streams .start ();
355- } finally {
356- streams .close ();
357351 }
358352 // There's nothing to assert... We're testing that this operation actually completes.
359353 }
@@ -362,9 +356,8 @@ public void testLocalThreadCloseWithoutConnectingToBroker() {
362356 @ Test
363357 public void testInitializesAndDestroysMetricsReporters () {
364358 final int oldInitCount = MockMetricsReporter .INIT_COUNT .get ();
365- final KafkaStreams streams = new KafkaStreams (builder .build (), props );
366359
367- try {
360+ try ( final KafkaStreams streams = new KafkaStreams ( builder . build (), props )) {
368361 final int newInitCount = MockMetricsReporter .INIT_COUNT .get ();
369362 final int initDiff = newInitCount - oldInitCount ;
370363 assertTrue ("some reporters should be initialized by calling on construction" , initDiff > 0 );
@@ -373,8 +366,6 @@ public void testInitializesAndDestroysMetricsReporters() {
373366 final int oldCloseCount = MockMetricsReporter .CLOSE_COUNT .get ();
374367 streams .close ();
375368 assertEquals (oldCloseCount + initDiff , MockMetricsReporter .CLOSE_COUNT .get ());
376- } finally {
377- streams .close ();
378369 }
379370 }
380371
@@ -584,8 +575,7 @@ public void shouldCleanupOldStateDirs() throws InterruptedException {
584575
585576 builder .table (topic , Materialized .as ("store" ));
586577
587- final KafkaStreams streams = new KafkaStreams (builder .build (), props );
588- try {
578+ try (final KafkaStreams streams = new KafkaStreams (builder .build (), props )) {
589579 final CountDownLatch latch = new CountDownLatch (1 );
590580 streams .setStateListener ((newState , oldState ) -> {
591581 if (newState == KafkaStreams .State .RUNNING && oldState == KafkaStreams .State .REBALANCING ) {
@@ -601,21 +591,16 @@ public void shouldCleanupOldStateDirs() throws InterruptedException {
601591 verifyCleanupStateDir (appDir , oldTaskDir );
602592 assertTrue (oldTaskDir .mkdirs ());
603593 verifyCleanupStateDir (appDir , oldTaskDir );
604- } finally {
605- streams .close ();
606594 }
607595 }
608596
609597 @ Test
610598 public void shouldThrowOnNegativeTimeoutForClose () {
611- final KafkaStreams streams = new KafkaStreams (builder .build (), props );
612- try {
599+ try (final KafkaStreams streams = new KafkaStreams (builder .build (), props )) {
613600 streams .close (Duration .ofMillis (-1L ));
614601 fail ("should not accept negative close parameter" );
615602 } catch (final IllegalArgumentException e ) {
616603 // expected
617- } finally {
618- streams .close ();
619604 }
620605 }
621606
@@ -684,9 +669,12 @@ private Topology getStatefulTopology(final String inputTopic,
684669 final String globalStoreName ,
685670 final boolean isPersistentStore ) throws Exception {
686671 CLUSTER .createTopics (inputTopic , outputTopic , globalTopicName );
687- final StoreBuilder <KeyValueStore <String , Long >> storeBuilder = Stores .keyValueStoreBuilder (isPersistentStore ?
688- Stores .persistentKeyValueStore (storeName ) : Stores .inMemoryKeyValueStore (storeName ),
689- Serdes .String (), Serdes .Long ());
672+ final StoreBuilder <KeyValueStore <String , Long >> storeBuilder = Stores .keyValueStoreBuilder (
673+ isPersistentStore ?
674+ Stores .persistentKeyValueStore (storeName )
675+ : Stores .inMemoryKeyValueStore (storeName ),
676+ Serdes .String (),
677+ Serdes .Long ());
690678 final Topology topology = new Topology ();
691679 topology .addSource ("source" , Serdes .String ().deserializer (), Serdes .String ().deserializer (), inputTopic )
692680 .addProcessor ("process" , () -> new AbstractProcessor <String , String >() {
@@ -769,7 +757,8 @@ private void startStreamsAndCheckDirExists(final Topology topology,
769757 }
770758 }
771759
772- private void verifyCleanupStateDir (final String appDir , final File oldTaskDir ) throws InterruptedException {
760+ private void verifyCleanupStateDir (final String appDir ,
761+ final File oldTaskDir ) throws InterruptedException {
773762 final File taskDir = new File (appDir , "0_0" );
774763 TestUtils .waitForCondition (
775764 () -> !oldTaskDir .exists () && taskDir .exists (),
@@ -785,7 +774,8 @@ public static class StateListenerStub implements KafkaStreams.StateListener {
785774 public Map <KafkaStreams .State , Long > mapStates = new HashMap <>();
786775
787776 @ Override
788- public void onChange (final KafkaStreams .State newState , final KafkaStreams .State oldState ) {
777+ public void onChange (final KafkaStreams .State newState ,
778+ final KafkaStreams .State oldState ) {
789779 final long prevCount = mapStates .containsKey (newState ) ? mapStates .get (newState ) : 0 ;
790780 numChanges ++;
791781 this .oldState = oldState ;
0 commit comments