Skip to content

Commit 720cefb

Browse files
committed
add more unit tests
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent d00b2df commit 720cefb

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

plugins/query-insights/src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.opensearch.cluster.node.DiscoveryNode;
1313
import org.opensearch.common.settings.ClusterSettings;
1414
import org.opensearch.common.util.Maps;
15+
import org.opensearch.core.tasks.resourcetracker.TaskResourceInfo;
16+
import org.opensearch.core.tasks.resourcetracker.TaskResourceUsage;
1517
import org.opensearch.core.xcontent.ToXContent;
1618
import org.opensearch.core.xcontent.XContentBuilder;
1719
import org.opensearch.plugin.insights.rules.action.top_queries.TopQueries;
@@ -80,6 +82,25 @@ public static List<SearchQueryRecord> generateQueryInsightRecords(int lower, int
8082
attributes.put(Attribute.TOTAL_SHARDS, randomIntBetween(1, 100));
8183
attributes.put(Attribute.INDICES, randomArray(1, 3, Object[]::new, () -> randomAlphaOfLengthBetween(5, 10)));
8284
attributes.put(Attribute.PHASE_LATENCY_MAP, phaseLatencyMap);
85+
attributes.put(
86+
Attribute.TASK_RESOURCE_USAGES,
87+
List.of(
88+
new TaskResourceInfo(
89+
randomAlphaOfLengthBetween(5, 10),
90+
randomLongBetween(1, 1000),
91+
randomLongBetween(1, 1000),
92+
randomAlphaOfLengthBetween(5, 10),
93+
new TaskResourceUsage(randomLongBetween(1, 1000), randomLongBetween(1, 1000))
94+
),
95+
new TaskResourceInfo(
96+
randomAlphaOfLengthBetween(5, 10),
97+
randomLongBetween(1, 1000),
98+
randomLongBetween(1, 1000),
99+
randomAlphaOfLengthBetween(5, 10),
100+
new TaskResourceUsage(randomLongBetween(1, 1000), randomLongBetween(1, 1000))
101+
)
102+
)
103+
);
83104

84105
records.add(new SearchQueryRecord(timestamp, measurements, attributes));
85106
timestamp += interval;

plugins/query-insights/src/test/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListenerTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.opensearch.search.aggregations.support.ValueType;
3333
import org.opensearch.search.builder.SearchSourceBuilder;
3434
import org.opensearch.tasks.Task;
35+
import org.opensearch.tasks.TaskResourceTrackingService;
3536
import org.opensearch.test.ClusterServiceUtils;
3637
import org.opensearch.test.OpenSearchTestCase;
3738
import org.opensearch.threadpool.TestThreadPool;
@@ -65,6 +66,7 @@ public class QueryInsightsListenerTests extends OpenSearchTestCase {
6566
private final SearchRequest searchRequest = mock(SearchRequest.class);
6667
private final QueryInsightsService queryInsightsService = mock(QueryInsightsService.class);
6768
private final TopQueriesService topQueriesService = mock(TopQueriesService.class);
69+
private final TaskResourceTrackingService taskResourceTrackingService = mock(TaskResourceTrackingService.class);
6870
private final ThreadPool threadPool = new TestThreadPool("QueryInsightsThreadPool");
6971
private ClusterService clusterService;
7072

@@ -77,6 +79,7 @@ public void setup() {
7779
ClusterState state = ClusterStateCreationUtils.stateWithActivePrimary("test", true, 1 + randomInt(3), randomInt(2));
7880
clusterService = ClusterServiceUtils.createClusterService(threadPool, state.getNodes().getLocalNode(), clusterSettings);
7981
ClusterServiceUtils.setState(clusterService, state);
82+
clusterService.setTaskResourceTrackingService(taskResourceTrackingService);
8083
when(queryInsightsService.isCollectionEnabled(MetricType.LATENCY)).thenReturn(true);
8184
when(queryInsightsService.getTopQueriesService(MetricType.LATENCY)).thenReturn(topQueriesService);
8285

@@ -139,6 +142,7 @@ public void testOnRequestEnd() throws InterruptedException {
139142
assertEquals(searchSourceBuilder.toString(), generatedRecord.getAttributes().get(Attribute.SOURCE));
140143
Map<String, String> labels = (Map<String, String>) generatedRecord.getAttributes().get(Attribute.LABELS);
141144
assertEquals("userLabel", labels.get(Task.X_OPAQUE_ID));
145+
verify(taskResourceTrackingService, times(1)).refreshResourceStats(task);
142146
}
143147

144148
public void testConcurrentOnRequestEnd() throws InterruptedException {
@@ -200,6 +204,7 @@ public void testConcurrentOnRequestEnd() throws InterruptedException {
200204
countDownLatch.await();
201205

202206
verify(queryInsightsService, times(numRequests)).addRecord(any());
207+
verify(taskResourceTrackingService, times(numRequests)).refreshResourceStats(task);
203208
}
204209

205210
public void testSetEnabled() {

0 commit comments

Comments
 (0)