@@ -288,33 +288,104 @@ private void setupMockedWorkloadGroupsFromClusterState() {
288288 // Tests for applyWorkloadGroupSearchSettings
289289
290290 public void testApplySearchSettings_NullWorkloadGroupId () {
291+ mockSearchRequest .setBatchedReduceSize (256 );
292+
291293 // No workload group ID in thread context
292294 sut .onRequestStart (mockSearchRequestContext );
293295
294- // Request should remain unchanged - source is null, no timeout set
295- assertNull ( mockSearchRequest .source ());
296+ // Request should remain unchanged
297+ assertEquals ( 256 , mockSearchRequest .getBatchedReduceSize ());
296298 }
297299
298300 public void testApplySearchSettings_WorkloadGroupNotFound () {
301+ mockSearchRequest .setBatchedReduceSize (256 );
302+
299303 testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , "non-existent-id" );
300304 when (workloadGroupService .getWorkloadGroupById ("non-existent-id" )).thenReturn (null );
301305
302306 sut .onRequestStart (mockSearchRequestContext );
303307
304- assertNull ( mockSearchRequest .source ());
308+ assertEquals ( 256 , mockSearchRequest .getBatchedReduceSize ());
305309 }
306310
307- public void testApplySearchSettings_EmptySearchSettings () {
308- mockSearchRequest .source (new SearchSourceBuilder ());
311+ public void testApplySearchSettings_BatchedReduceSize_WlmAppliedWhenDefault () {
312+ // Request uses default value (512)
313+ assertEquals (SearchRequest .DEFAULT_BATCHED_REDUCE_SIZE , mockSearchRequest .getBatchedReduceSize ());
309314
310315 String wgId = "test-wg" ;
311- WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ());
316+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ("batched_reduce_size" , "100" ));
312317 when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
313318 testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
314319
315320 sut .onRequestStart (mockSearchRequestContext );
316321
317- assertNull (mockSearchRequest .source ().timeout ()); // No settings applied
322+ assertEquals (100 , mockSearchRequest .getBatchedReduceSize ()); // WLM applied
323+ }
324+
325+ public void testApplySearchSettings_BatchedReduceSize_RequestAlreadySet () {
326+ mockSearchRequest .setBatchedReduceSize (50 ); // explicitly set by user
327+
328+ String wgId = "test-wg" ;
329+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ("batched_reduce_size" , "100" ));
330+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
331+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
332+
333+ sut .onRequestStart (mockSearchRequestContext );
334+
335+ assertEquals (50 , mockSearchRequest .getBatchedReduceSize ()); // Request value preserved
336+ }
337+
338+ public void testApplySearchSettings_MaxConcurrentShardRequests_WlmAppliedWhenDefault () {
339+ // Request has default value (not explicitly set), raw field is 0
340+ assertEquals (0 , mockSearchRequest .getMaxConcurrentShardRequestsRaw ());
341+
342+ String wgId = "test-wg" ;
343+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ("max_concurrent_shard_requests" , "10" ));
344+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
345+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
346+
347+ sut .onRequestStart (mockSearchRequestContext );
348+
349+ assertEquals (10 , mockSearchRequest .getMaxConcurrentShardRequests ()); // WLM applied
350+ }
351+
352+ public void testApplySearchSettings_MaxConcurrentShardRequests_RequestAlreadySet () {
353+ mockSearchRequest .setMaxConcurrentShardRequests (20 ); // explicitly set by user
354+
355+ String wgId = "test-wg" ;
356+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ("max_concurrent_shard_requests" , "5" ));
357+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
358+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
359+
360+ sut .onRequestStart (mockSearchRequestContext );
361+
362+ assertEquals (20 , mockSearchRequest .getMaxConcurrentShardRequests ()); // Request value preserved
363+ }
364+
365+ public void testApplySearchSettings_CancelAfterTimeInterval_WlmAppliedWhenNull () {
366+ assertNull (mockSearchRequest .getCancelAfterTimeInterval ());
367+
368+ String wgId = "test-wg" ;
369+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ("cancel_after_time_interval" , "30s" ));
370+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
371+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
372+
373+ sut .onRequestStart (mockSearchRequestContext );
374+
375+ assertEquals (TimeValue .timeValueSeconds (30 ), mockSearchRequest .getCancelAfterTimeInterval ());
376+ }
377+
378+ public void testApplySearchSettings_CancelAfterTimeInterval_RequestAlreadySet () {
379+ mockSearchRequest .setCancelAfterTimeInterval (TimeValue .timeValueSeconds (10 ));
380+
381+ String wgId = "test-wg" ;
382+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ("cancel_after_time_interval" , "30s" ));
383+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
384+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
385+
386+ sut .onRequestStart (mockSearchRequestContext );
387+
388+ assertEquals (TimeValue .timeValueSeconds (10 ), mockSearchRequest .getCancelAfterTimeInterval ()); // Request value preserved
318389 }
319390
320391 public void testApplySearchSettings_Timeout_WlmAppliedWhenNull () {
@@ -357,6 +428,37 @@ public void testApplySearchSettings_Timeout_NullSource() {
357428 assertNull (mockSearchRequest .source ()); // Should not throw, source remains null
358429 }
359430
431+ public void testApplySearchSettings_EmptySearchSettings () {
432+ mockSearchRequest .source (new SearchSourceBuilder ());
433+
434+ String wgId = "test-wg" ;
435+ WorkloadGroup wg = createWorkloadGroup (wgId , Map .of ());
436+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
437+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
438+
439+ sut .onRequestStart (mockSearchRequestContext );
440+
441+ assertNull (mockSearchRequest .source ().timeout ()); // No settings applied
442+ }
443+
444+ public void testApplySearchSettings_MultipleSettings () {
445+ mockSearchRequest .source (new SearchSourceBuilder ());
446+
447+ String wgId = "test-wg" ;
448+ WorkloadGroup wg = createWorkloadGroup (
449+ wgId ,
450+ Map .of ("batched_reduce_size" , "100" , "max_concurrent_shard_requests" , "5" , "timeout" , "30s" )
451+ );
452+ when (workloadGroupService .getWorkloadGroupById (wgId )).thenReturn (wg );
453+ testThreadPool .getThreadContext ().putHeader (WorkloadGroupTask .WORKLOAD_GROUP_ID_HEADER , wgId );
454+
455+ sut .onRequestStart (mockSearchRequestContext );
456+
457+ assertEquals (100 , mockSearchRequest .getBatchedReduceSize ());
458+ assertEquals (5 , mockSearchRequest .getMaxConcurrentShardRequests ());
459+ assertEquals (TimeValue .timeValueSeconds (30 ), mockSearchRequest .source ().timeout ());
460+ }
461+
360462 private WorkloadGroup createWorkloadGroup (String id , Map <String , String > searchSettings ) {
361463 return new WorkloadGroup (
362464 "test-name" ,
0 commit comments