Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -19,6 +19,8 @@
import org.apache.kafka.streams.processor.RecordContext;
import org.apache.kafka.streams.processor.TopicNameExtractor;

import java.util.Objects;

/**
* Static topic name extractor
*/
Expand All @@ -38,4 +40,21 @@ public String extract(final K key, final V value, final RecordContext recordCont
public String toString() {
return "StaticTopicNameExtractor(" + topicName + ")";
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final StaticTopicNameExtractor<?, ?> that = (StaticTopicNameExtractor<?, ?>) o;
return Objects.equals(topicName, that.topicName);
}

@Override
public int hashCode() {
return Objects.hash(topicName);
}
}
29 changes: 29 additions & 0 deletions streams/src/test/java/org/apache/kafka/streams/TopologyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ public void singleSourceShouldHaveSingleSubtopology() {
Collections.singleton(expectedSourceNode)));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -409,6 +410,7 @@ public void singleSourceWithListOfTopicsShouldHaveSingleSubtopology() {
Collections.singleton(expectedSourceNode)));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -420,6 +422,7 @@ public void singleSourcePatternShouldHaveSingleSubtopology() {
Collections.singleton(expectedSourceNode)));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -440,6 +443,7 @@ public void multipleSourcesShouldHaveDistinctSubtopologies() {
Collections.singleton(expectedSourceNode3)));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -453,6 +457,7 @@ public void sourceAndProcessorShouldHaveSingleSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -468,6 +473,7 @@ public void sourceAndProcessorWithStateShouldHaveSingleSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}


Expand All @@ -484,6 +490,7 @@ public void sourceAndProcessorWithMultipleStatesShouldHaveSingleSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -499,6 +506,7 @@ public void sourceWithMultipleProcessorsShouldHaveSingleSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand All @@ -514,6 +522,7 @@ public void processorWithMultipleSourcesShouldHaveSingleSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand Down Expand Up @@ -543,6 +552,7 @@ public void multipleSourcesWithProcessorsShouldHaveDistinctSubtopologies() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(2, allNodes3));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand Down Expand Up @@ -572,6 +582,7 @@ public void multipleSourcesWithSinksShouldHaveDistinctSubtopologies() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(2, allNodes3));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand Down Expand Up @@ -603,6 +614,7 @@ public void processorsWithSameSinkShouldHaveSameSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand Down Expand Up @@ -633,19 +645,22 @@ public void processorsWithSharedStateShouldHaveSameSubtopology() {
expectedDescription.addSubtopology(new InternalTopologyBuilder.Subtopology(0, allNodes));

assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
public void shouldDescribeGlobalStoreTopology() {
addGlobalStoreToTopologyAndExpectedDescription("globalStore", "source", "globalTopic", "processor", 0);
assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
public void shouldDescribeMultipleGlobalStoreTopology() {
addGlobalStoreToTopologyAndExpectedDescription("globalStore1", "source1", "globalTopic1", "processor1", 0);
addGlobalStoreToTopologyAndExpectedDescription("globalStore2", "source2", "globalTopic2", "processor2", 1);
assertThat(topology.describe(), equalTo(expectedDescription));
assertThat(topology.describe().hashCode(), equalTo(expectedDescription.hashCode()));
}

@Test
Expand Down Expand Up @@ -1085,6 +1100,20 @@ public void kTableNamedMaterializedFilterShouldPreserveTopologyStructure() {
describe.toString());
}

@Test
public void topologyWithStaticTopicNameExtractorShouldRespectEqualHashcodeContract() {
final Topology topologyA = topologyWithStaticTopicName();
final Topology topologyB = topologyWithStaticTopicName();
assertThat(topologyA.describe(), equalTo(topologyB.describe()));
assertThat(topologyA.describe().hashCode(), equalTo(topologyB.describe().hashCode()));
}

private Topology topologyWithStaticTopicName() {
final StreamsBuilder builder = new StreamsBuilder();
builder.stream("from-topic-name").to("to-topic-name");
return builder.build();
}

private TopologyDescription.Source addSource(final String sourceName,
final String... sourceTopic) {
topology.addSource(null, sourceName, null, null, null, sourceTopic);
Expand Down