3232import org .opensearch .search .aggregations .support .ValueType ;
3333import org .opensearch .search .builder .SearchSourceBuilder ;
3434import org .opensearch .tasks .Task ;
35+ import org .opensearch .tasks .TaskResourceTrackingService ;
3536import org .opensearch .test .ClusterServiceUtils ;
3637import org .opensearch .test .OpenSearchTestCase ;
3738import 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