Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

[TEX] Part 3: TrackingKeyValueService: tracking iterator utils #6336

Merged
merged 10 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,84 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import com.google.common.collect.ImmutableList;
import com.palantir.common.base.ClosableIterator;
import com.palantir.common.base.ClosableIterators;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.ToLongFunction;
import one.util.streamex.StreamEx;
import org.junit.Test;

public final class TrackingClosableIteratorTest {
private static final ClosableIterator<String> ONE_ELEMENT_ITERATOR = ClosableIterators.wrapWithEmptyClose(
List.of(TrackingIteratorTestUtils.STRING).iterator());
private static final String STRING = "test";
private static final ClosableIterator<String> ONE_ELEMENT_ITERATOR =
ClosableIterators.wrapWithEmptyClose(List.of(STRING).iterator());
private static final ImmutableList<String> STRINGS = ImmutableList.of(
"test4", "test4", "test200", "composite", "", "t", "twentyElementString1", "tt", "twentyElementString2");

// these have to be anonymous inner classes rather than lambdas in order to spy
private static final Consumer<Long> NO_OP = new Consumer<>() {
@Override
public void accept(Long _unused) {}
};
private static final ToLongFunction<String> STRING_LENGTH_MEASURER = new ToLongFunction<>() {
@Override
public long applyAsLong(String value) {
return value.length();
}
};

@Test
public void oneElementTrackingClosableIteratorIsWiredCorrectly() {
Consumer<Long> tracker = spy(TrackingIteratorTestUtils.noOp());
ToLongFunction<String> measurer = spy(TrackingIteratorTestUtils.STRING_MEASURER);
Consumer<Long> tracker = spy(NO_OP);
ToLongFunction<String> measurer = spy(STRING_LENGTH_MEASURER);
TrackingClosableIterator<String> trackingIterator =
new TrackingClosableIterator<>(ONE_ELEMENT_ITERATOR, tracker, measurer);

assertThat(trackingIterator).toIterable().containsExactlyElementsOf(List.of(TrackingIteratorTestUtils.STRING));
verify(measurer, times(1)).applyAsLong(TrackingIteratorTestUtils.STRING);
verify(tracker, times(1))
.accept(TrackingIteratorTestUtils.STRING_MEASURER.applyAsLong(TrackingIteratorTestUtils.STRING));
verifyNoMoreInteractions(tracker);
verifyNoMoreInteractions(measurer);
assertThat(trackingIterator).toIterable().containsExactlyElementsOf(List.of(STRING));
verify(measurer).applyAsLong(STRING);
verify(tracker).accept(STRING_LENGTH_MEASURER.applyAsLong(STRING));
verifyNoMoreInteractions(tracker, measurer);
}

@Test
public void multiElementTrackingClosableIteratorIsWiredCorrectly() {
ArrayList<Long> consumed = new ArrayList<>();
TrackingClosableIterator<String> trackingIterator = new TrackingClosableIterator<>(
createClosableStringIterator(), consumed::add, TrackingIteratorTestUtils.STRING_MEASURER);
TrackingClosableIterator<String> trackingIterator =
new TrackingClosableIterator<>(createClosableStringIterator(), consumed::add, STRING_LENGTH_MEASURER);

assertThat(trackingIterator)
.toIterable()
.containsExactlyElementsOf(ImmutableList.copyOf(createClosableStringIterator()));

assertThat(consumed)
.containsExactlyElementsOf(StreamEx.of(createClosableStringIterator())
.mapToLong(TrackingIteratorTestUtils.STRING_MEASURER)
.mapToLong(STRING_LENGTH_MEASURER)
.boxed()
.toList());
}

@Test
public void trackingClosableStringIteratorDelegatesClose() {
ClosableIterator<String> iterator = spy(createClosableStringIterator());
TrackingClosableIterator<String> trackingIterator = new TrackingClosableIterator<>(
iterator, TrackingIteratorTestUtils.noOp(), TrackingIteratorTestUtils.STRING_MEASURER);
TrackingClosableIterator<String> trackingIterator =
new TrackingClosableIterator<>(iterator, NO_OP, STRING_LENGTH_MEASURER);
trackingIterator.close();
verify(iterator, times(1)).close();
verify(iterator).close();
verifyNoMoreInteractions(iterator);
}

private static ClosableIterator<String> createClosableStringIterator() {
return ClosableIterators.wrapWithEmptyClose(TrackingIteratorTestUtils.createStringIterator());
return ClosableIterators.wrapWithEmptyClose(createStringIterator());
}

private static Iterator<String> createStringIterator() {
return STRINGS.stream().iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

Expand All @@ -32,40 +31,54 @@
import org.junit.Test;

public final class TrackingIteratorTest {
private static final Iterator<String> ONE_ELEMENT_ITERATOR =
List.of(TrackingIteratorTestUtils.STRING).iterator();
private static final String STRING = "test";
private static final Iterator<String> ONE_ELEMENT_ITERATOR = List.of(STRING).iterator();
private static final ImmutableList<String> STRINGS = ImmutableList.of(
"test4", "test4", "test200", "composite", "", "t", "twentyElementString1", "tt", "twentyElementString2");

// these have to be anonymous inner classes rather than lambdas in order to spy
private static final Consumer<Long> NO_OP = new Consumer<>() {
@Override
public void accept(Long _unused) {}
};
private static final ToLongFunction<String> STRING_LENGTH_MEASURER = new ToLongFunction<>() {
@Override
public long applyAsLong(String value) {
return value.length();
}
};

@Test
public void oneElementTrackingIteratorIsWiredCorrectly() {
Consumer<Long> tracker = spy(TrackingIteratorTestUtils.noOp());
ToLongFunction<String> measurer = spy(TrackingIteratorTestUtils.STRING_MEASURER);
Consumer<Long> tracker = spy(NO_OP);
ToLongFunction<String> measurer = spy(STRING_LENGTH_MEASURER);
TrackingIterator<String, Iterator<String>> trackingIterator =
new TrackingIterator<>(ONE_ELEMENT_ITERATOR, tracker, measurer);

assertThat(trackingIterator).toIterable().containsExactlyElementsOf(List.of(TrackingIteratorTestUtils.STRING));
verify(measurer, times(1)).applyAsLong(TrackingIteratorTestUtils.STRING);
verify(tracker, times(1))
.accept(TrackingIteratorTestUtils.STRING_MEASURER.applyAsLong(TrackingIteratorTestUtils.STRING));
verifyNoMoreInteractions(tracker);
verifyNoMoreInteractions(measurer);
assertThat(trackingIterator).toIterable().containsExactlyElementsOf(List.of(STRING));
verify(measurer).applyAsLong(STRING);
verify(tracker).accept(STRING_LENGTH_MEASURER.applyAsLong(STRING));
verifyNoMoreInteractions(tracker, measurer);
}

@Test
public void multiElementTrackingIteratorIsWiredCorrectly() {
ArrayList<Long> consumed = new ArrayList<>();
TrackingIterator<String, Iterator<String>> trackingIterator = new TrackingIterator<>(
TrackingIteratorTestUtils.createStringIterator(),
consumed::add,
TrackingIteratorTestUtils.STRING_MEASURER);
TrackingIterator<String, Iterator<String>> trackingIterator =
new TrackingIterator<>(createStringIterator(), consumed::add, STRING_LENGTH_MEASURER);

assertThat(trackingIterator)
.toIterable()
.containsExactlyElementsOf(ImmutableList.copyOf(TrackingIteratorTestUtils.createStringIterator()));
.containsExactlyElementsOf(ImmutableList.copyOf(createStringIterator()));

assertThat(consumed)
.containsExactlyElementsOf(StreamEx.of(TrackingIteratorTestUtils.createStringIterator())
.mapToLong(TrackingIteratorTestUtils.STRING_MEASURER)
.containsExactlyElementsOf(StreamEx.of(createStringIterator())
.mapToLong(STRING_LENGTH_MEASURER)
.boxed()
.toList());
}

public static Iterator<String> createStringIterator() {
return STRINGS.stream().iterator();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

Expand Down Expand Up @@ -47,7 +46,11 @@ public final class TrackingRowColumnRangeIteratorTest {
createCell(20), createValue(20),
createCell(30), createValue(30));

// this has to be an anonymous inner class rather than lambda in order to spy
// these have to be anonymous inner classes rather than lambdas in order to spy
private static final Consumer<Long> NO_OP = new Consumer<>() {
@Override
public void accept(Long _unused) {}
};
private static final ToLongFunction<Entry<Cell, Value>> ENTRY_MEASURER = new ToLongFunction<>() {
@Override
public long applyAsLong(Entry<Cell, Value> value) {
Expand All @@ -57,16 +60,15 @@ public long applyAsLong(Entry<Cell, Value> value) {

@Test
public void oneElementTrackingRowColumnRangeIteratorIsWiredCorrectly() {
Consumer<Long> tracker = spy(TrackingIteratorTestUtils.noOp());
Consumer<Long> tracker = spy(NO_OP);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit / non actionable: I don't normally like using constants if there is only one thing, though I'm fine either way

ToLongFunction<Entry<Cell, Value>> measurer = spy(ENTRY_MEASURER);
TrackingRowColumnRangeIterator trackingIterator =
new TrackingRowColumnRangeIterator(ONE_ELEMENT_ITERATOR, tracker, measurer);

assertThat(trackingIterator).toIterable().containsExactlyElementsOf(List.of(ENTRY));
verify(measurer, times(1)).applyAsLong(ENTRY);
verify(tracker, times(1)).accept(ENTRY_MEASURER.applyAsLong(ENTRY));
verifyNoMoreInteractions(tracker);
verifyNoMoreInteractions(measurer);
verify(measurer).applyAsLong(ENTRY);
verify(tracker).accept(ENTRY_MEASURER.applyAsLong(ENTRY));
verifyNoMoreInteractions(tracker, measurer);
}

@Test
Expand Down