diff --git a/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java b/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java index 2acb23641667a..b2ef3215f869f 100644 --- a/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java +++ b/server/src/test/java/org/opensearch/search/backpressure/trackers/HeapUsageTrackerTests.java @@ -12,13 +12,16 @@ import org.opensearch.action.search.SearchTask; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; +import org.opensearch.monitor.jvm.JvmStats; import org.opensearch.search.backpressure.settings.SearchBackpressureSettings; import org.opensearch.search.backpressure.settings.SearchShardTaskSettings; import org.opensearch.search.backpressure.settings.SearchTaskSettings; +import org.opensearch.tasks.CancellableTask; import org.opensearch.tasks.Task; import org.opensearch.tasks.TaskCancellation; import org.opensearch.test.OpenSearchTestCase; +import java.util.List; import java.util.Optional; import static org.mockito.Mockito.spy; @@ -140,4 +143,17 @@ public void testNotEligibleForCancellation() { reason = tracker.checkAndMaybeGetCancellationReason(task); assertFalse(reason.isPresent()); } + + public void testIsHeapUsageDominatedBySearch() { + assumeTrue("Skip the test if the hardware doesn't support heap usage tracking", HeapUsageTracker.isHeapTrackingSupported()); + + // task with 1 byte of heap usage so that it does not breach the threshold + CancellableTask task = createMockTaskWithResourceStats(SearchShardTask.class, 1, 1); + assertFalse(HeapUsageTracker.isHeapUsageDominatedBySearch(List.of(task), 0.5)); + + long totalHeap = JvmStats.jvmStats().getMem().getHeapMax().getBytes(); + // task with heap usage of [totalHeap - 1] so that it breaches the threshold + task = createMockTaskWithResourceStats(SearchShardTask.class, 1, totalHeap - 1); + assertTrue(HeapUsageTracker.isHeapUsageDominatedBySearch(List.of(task), 0.5)); + } }