Skip to content

Commit a3eeb6a

Browse files
committed
add unit test
Signed-off-by: xuxiong1 <[email protected]>
1 parent abe8c97 commit a3eeb6a

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

server/src/main/java/org/opensearch/index/engine/Engine.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,9 @@ public SegmentsStats segmentsStats(boolean includeSegmentFileSizes, boolean incl
947947
return stats;
948948
}
949949

950+
/**
951+
* @return Stats for pull-based ingestion.
952+
*/
950953
public PollingIngestStats pollingIngestStats() {
951954
return null;
952955
}

server/src/main/java/org/opensearch/indices/pollingingest/PollingIngestStats.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public int hashCode() {
7979
return Objects.hash(messageProcessorStats, consumerStats);
8080
}
8181

82+
/**
83+
* Stats for message processor
84+
*/
8285
@ExperimentalApi
8386
public static class MessageProcessorStats {
8487
private final long totalProcessedCount;
@@ -105,6 +108,9 @@ public int hashCode() {
105108
}
106109
}
107110

111+
/**
112+
* Stats for consumer (poller)
113+
*/
108114
@ExperimentalApi
109115
public static class ConsumerStats {
110116
private final long totalPolledCount;
@@ -131,6 +137,9 @@ public int hashCode() {
131137
}
132138
}
133139

140+
/**
141+
* Builder for {@link PollingIngestStats}
142+
*/
134143
@ExperimentalApi
135144
public static class Builder {
136145
private long totalProcessedCount;
@@ -155,6 +164,11 @@ public PollingIngestStats build() {
155164
}
156165
}
157166

167+
/**
168+
* Returns a new builder for creating a {@link PollingIngestStats} instance.
169+
*
170+
* @return a new {@code Builder} instance
171+
*/
158172
public static Builder builder() {
159173
return new Builder();
160174
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.indices.pollingingest;
10+
11+
import org.opensearch.common.io.stream.BytesStreamOutput;
12+
import org.opensearch.core.common.io.stream.StreamInput;
13+
import org.opensearch.core.xcontent.MediaTypeRegistry;
14+
import org.opensearch.core.xcontent.ToXContent;
15+
import org.opensearch.core.xcontent.XContentBuilder;
16+
import org.opensearch.test.OpenSearchTestCase;
17+
18+
import java.io.IOException;
19+
20+
public class PollingIngestStatsTests extends OpenSearchTestCase {
21+
22+
public void testToXContent() throws IOException {
23+
PollingIngestStats stats = createTestInstance();
24+
25+
XContentBuilder builder = MediaTypeRegistry.contentBuilder(MediaTypeRegistry.JSON);
26+
builder.startObject();
27+
stats.toXContent(builder, ToXContent.EMPTY_PARAMS);
28+
builder.endObject();
29+
30+
String expected = "{\"polling_ingest_stats\":{\"message_processor_stats\":{\"total_processed_count\":"
31+
+ stats.getMessageProcessorStats().getTotalProcessedCount()
32+
+ "},\"consumer_stats\":{\"total_polled_count\":"
33+
+ stats.getConsumerStats().getTotalPolledCount()
34+
+ "}}}";
35+
36+
assertEquals(expected, builder.toString());
37+
}
38+
39+
public void testSerialization() throws IOException {
40+
PollingIngestStats original = createTestInstance();
41+
42+
try (BytesStreamOutput output = new BytesStreamOutput()) {
43+
original.writeTo(output);
44+
45+
try (StreamInput input = output.bytes().streamInput()) {
46+
PollingIngestStats deserialized = new PollingIngestStats(input);
47+
assertEquals(original, deserialized);
48+
}
49+
}
50+
}
51+
52+
private PollingIngestStats createTestInstance() {
53+
return PollingIngestStats.builder()
54+
.setTotalProcessedCount(randomNonNegativeLong())
55+
.setTotalPolledCount(randomNonNegativeLong())
56+
.build();
57+
}
58+
}

0 commit comments

Comments
 (0)