cadonna commented on a change in pull request #9717:
URL: https://github.com/apache/kafka/pull/9717#discussion_r545110552



##########
File path: 
streams/src/main/java/org/apache/kafka/streams/state/internals/RocksDBRangeIterator.java
##########
@@ -55,20 +56,36 @@
         }
     }
 
+    public byte[] getRawLastKey() {
+        return rawLastKey;
+    }
+
+    public boolean isForward() {
+        return forward;
+    }
+
+    public KeyValue<Bytes, byte[]> getNext() {
+        return super.makeNext();
+    }
+
+    public Comparator<byte[]> getComparator() {
+        return comparator;
+    }
+
     @Override
     public KeyValue<Bytes, byte[]> makeNext() {
-        final KeyValue<Bytes, byte[]> next = super.makeNext();
+        final KeyValue<Bytes, byte[]> next = getNext();
         if (next == null) {
             return allDone();
         } else {
-            if (forward) {
-                if (comparator.compare(next.key.get(), rawLastKey) <= 0) {
+            if (isForward()) {
+                if (getComparator().compare(next.key.get(), getRawLastKey()) 
<= 0) {
                     return next;
                 } else {
                     return allDone();
                 }
             } else {
-                if (comparator.compare(next.key.get(), rawLastKey) >= 0) {
+                if (getComparator().compare(next.key.get(), getRawLastKey()) 
>= 0) {

Review comment:
       It is not a good idea to change a class due to unit tests if it is not 
absolutely necessary. In this case it is definitely not necessary. See my 
suggestion how to change the tests. 

##########
File path: 
streams/src/test/java/org/apache/kafka/streams/state/internals/RocksDBRangeIteratorTest.java
##########
@@ -0,0 +1,295 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.streams.state.internals;
+
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.partialMockBuilder;
+import static org.easymock.EasyMock.replay;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.core.Is.is;
+
+public class RocksDBRangeIteratorTest {
+
+    private RocksDBRangeIterator mockRocksDBRangeIterator() {
+        return partialMockBuilder(RocksDBRangeIterator.class)
+                .addMockedMethod("getNext")
+                .addMockedMethod("getRawLastKey")
+                .addMockedMethod("isForward")
+                .addMockedMethod("getComparator")
+                .createMock();
+    }
+
+    @Test
+    public void shouldReturnAllKeysInTheRangeInForwardDirection() {
+        final RocksDBRangeIterator rocksDBRangeIterator = 
mockRocksDBRangeIterator();
+
+        final String key1 = "a";
+        final String key2 = "b";
+        final String key3 = "c";
+        final String key4 = "d";
+        final String value = "value";
+
+        final  Bytes key1Bytes = Bytes.wrap(key1.getBytes());
+        final  Bytes key2Bytes = Bytes.wrap(key2.getBytes());
+        final  Bytes key3Bytes = Bytes.wrap(key3.getBytes());
+        final  Bytes key4Bytes = Bytes.wrap(key4.getBytes());
+        final byte[] valueBytes = value.getBytes();
+
+        expect(rocksDBRangeIterator.getNext())
+            .andReturn(KeyValue.pair(key1Bytes, valueBytes))
+            .andReturn(KeyValue.pair(key2Bytes, valueBytes))
+            .andReturn(KeyValue.pair(key3Bytes, valueBytes))
+            .andReturn(KeyValue.pair(key4Bytes, valueBytes));
+
+        expect(rocksDBRangeIterator.getRawLastKey())
+            .andReturn(key3Bytes.get())
+            .anyTimes();
+
+        expect(rocksDBRangeIterator.isForward())
+            .andReturn(true)
+            .anyTimes();
+
+        expect(rocksDBRangeIterator.getComparator())
+            .andReturn(Bytes.BYTES_LEXICO_COMPARATOR)
+            .anyTimes();
+
+
+        replay(rocksDBRangeIterator);
+        assertThat(rocksDBRangeIterator.makeNext().key, is(key1Bytes));
+        assertThat(rocksDBRangeIterator.makeNext().key, is(key2Bytes));
+        assertThat(rocksDBRangeIterator.makeNext().key, is(key3Bytes));
+        assertThat(rocksDBRangeIterator.makeNext(), is(nullValue()));

Review comment:
       You usually do not want to mock the class under test, because you want 
to test it. Also partial mocks should only be used if absolutely necessary. A 
rule of thumb is if a partial mock is needed then most probably the design has 
a flaw.
   In this specific case, you should mock RocksDB's iterator. For the class 
under test, you should test `hasNext()`, `next()`, `peekNextKey()` and 
`close()`, because those are the one exposed (`makeNext()` should actually de 
declared as `protected`, IMO). 
   
   ```suggestion
           final String key1 = "a";
           final String key2 = "b";
           final String key3 = "c";
           final String key4 = "d";
           final String value = "value";
   
           final  Bytes key1Bytes = Bytes.wrap(key1.getBytes());
           final  Bytes key2Bytes = Bytes.wrap(key2.getBytes());
           final  Bytes key3Bytes = Bytes.wrap(key3.getBytes());
           final  Bytes key4Bytes = Bytes.wrap(key4.getBytes());
           final byte[] valueBytes = value.getBytes();
   
           final RocksIterator rocksIterator = mock(RocksIterator.class);
           rocksIterator.seek(key1Bytes.get());
           expect(rocksIterator.isValid())
               .andReturn(true)
               .andReturn(true)
               .andReturn(true)
               .andReturn(true)
               .andReturn(false);
           expect(rocksIterator.key())
               .andReturn(key1Bytes.get())
               .andReturn(key2Bytes.get())
               .andReturn(key3Bytes.get())
               .andReturn(key4Bytes.get());
           expect(rocksIterator.value()).andReturn(valueBytes).times(4);
           rocksIterator.next();
           expectLastCall().times(4);
           replay(rocksIterator);
   
           final RocksDBRangeIterator rocksDBRangeIterator = new 
RocksDBRangeIterator(
               STORE_NAME,
               rocksIterator,
               Collections.emptySet(),
               key1Bytes,
               key4Bytes,
               true
           );
           assertThat(rocksDBRangeIterator.hasNext(), is(true));
           assertThat(rocksDBRangeIterator.next().key, is(key1Bytes));
           assertThat(rocksDBRangeIterator.hasNext(), is(true));
           assertThat(rocksDBRangeIterator.next().key, is(key2Bytes));
           assertThat(rocksDBRangeIterator.hasNext(), is(true));
           assertThat(rocksDBRangeIterator.next().key, is(key3Bytes));
           assertThat(rocksDBRangeIterator.hasNext(), is(true));
           assertThat(rocksDBRangeIterator.next().key, is(key4Bytes));
           assertThat(rocksDBRangeIterator.hasNext(), is(false));
           verify(rocksIterator);
   ```




----------------------------------------------------------------
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.

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


Reply via email to