You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/07/23 17:40:13 UTC

[GitHub] [kafka] mjsax commented on a change in pull request #11105: KAFKA-13123: close KeyValueIterator instances in example code and tests

mjsax commented on a change in pull request #11105:
URL: https://github.com/apache/kafka/pull/11105#discussion_r675730406



##########
File path: streams/src/test/java/org/apache/kafka/streams/state/internals/RocksDBStoreTest.java
##########
@@ -872,62 +879,65 @@ public void shouldPerformRangeQueriesWithCachingDisabled() {
         context.setTime(1L);
         store.put(1, "hi");
         store.put(2, "goodbye");
-        final KeyValueIterator<Integer, String> range = store.range(1, 2);
-        assertEquals("hi", range.next().value);
-        assertEquals("goodbye", range.next().value);
-        assertFalse(range.hasNext());
+        try (final KeyValueIterator<Integer, String> range = store.range(1, 2)) {
+            assertEquals("hi", range.next().value);
+            assertEquals("goodbye", range.next().value);
+            assertFalse(range.hasNext());
+        }
     }
 
     @Test
     public void shouldPerformAllQueriesWithCachingDisabled() {
         context.setTime(1L);
         store.put(1, "hi");
         store.put(2, "goodbye");
-        final KeyValueIterator<Integer, String> range = store.all();
-        assertEquals("hi", range.next().value);
-        assertEquals("goodbye", range.next().value);
-        assertFalse(range.hasNext());
+        try (final KeyValueIterator<Integer, String> range = store.all()) {
+            assertEquals("hi", range.next().value);
+            assertEquals("goodbye", range.next().value);
+            assertFalse(range.hasNext());
+        }
     }
 
     @Test
     public void shouldCloseOpenRangeIteratorsWhenStoreClosedAndThrowInvalidStateStoreOnHasNextAndNext() {
         context.setTime(1L);
         store.put(1, "hi");
         store.put(2, "goodbye");
-        final KeyValueIterator<Integer, String> iteratorOne = store.range(1, 5);
-        final KeyValueIterator<Integer, String> iteratorTwo = store.range(1, 4);
+        try (final KeyValueIterator<Integer, String> iteratorOne = store.range(1, 5);
+             final KeyValueIterator<Integer, String> iteratorTwo = store.range(1, 4)) {
 
-        assertTrue(iteratorOne.hasNext());
-        assertTrue(iteratorTwo.hasNext());
+            assertTrue(iteratorOne.hasNext());
+            assertTrue(iteratorTwo.hasNext());
 
-        store.close();
+            store.close();
 
-        try {
-            iteratorOne.hasNext();
-            fail("should have thrown InvalidStateStoreException on closed store");
-        } catch (final InvalidStateStoreException e) {
-            // ok
-        }
+            try {
+                iteratorOne.hasNext();
+                fail("should have thrown InvalidStateStoreException on closed store");
+            } catch (final InvalidStateStoreException e) {

Review comment:
       Might be nice to rewrite this using `assertThrows` ? (Similar below.)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org