|
| 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.wlm.listeners; |
| 10 | + |
| 11 | +import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException; |
| 12 | +import org.opensearch.test.OpenSearchTestCase; |
| 13 | +import org.opensearch.threadpool.TestThreadPool; |
| 14 | +import org.opensearch.threadpool.ThreadPool; |
| 15 | +import org.opensearch.wlm.QueryGroupService; |
| 16 | +import org.opensearch.wlm.QueryGroupTask; |
| 17 | + |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +public class QueryGroupRequestRejectionOperationListenerTests extends OpenSearchTestCase { |
| 24 | + ThreadPool testThreadPool; |
| 25 | + QueryGroupService queryGroupService; |
| 26 | + QueryGroupRequestRejectionOperationListener sut; |
| 27 | + |
| 28 | + |
| 29 | + public void setUp() throws Exception { |
| 30 | + super.setUp(); |
| 31 | + testThreadPool = new TestThreadPool("RejectionTestThreadPool"); |
| 32 | + } |
| 33 | + |
| 34 | + public void tearDown() throws Exception { |
| 35 | + super.tearDown(); |
| 36 | + testThreadPool.shutdown(); |
| 37 | + } |
| 38 | + |
| 39 | + public void testRejectionCase() { |
| 40 | + queryGroupService = mock(QueryGroupService.class); |
| 41 | + sut = new QueryGroupRequestRejectionOperationListener(queryGroupService, testThreadPool); |
| 42 | + final String testQueryGroupId = "asdgasgkajgkw3141_3rt4t"; |
| 43 | + testThreadPool.getThreadContext().putHeader(QueryGroupTask.QUERY_GROUP_ID_HEADER, testQueryGroupId); |
| 44 | + when(queryGroupService.shouldRejectFor(testQueryGroupId)).thenReturn(Optional.of("Test query group is contended")); |
| 45 | + |
| 46 | + assertThrows(OpenSearchRejectedExecutionException.class, () -> sut.onRequestStart(null)); |
| 47 | + } |
| 48 | + |
| 49 | + public void testNonRejectionCase() { |
| 50 | + queryGroupService = mock(QueryGroupService.class); |
| 51 | + sut = new QueryGroupRequestRejectionOperationListener(queryGroupService, testThreadPool); |
| 52 | + final String testQueryGroupId = "asdgasgkajgkw3141_3rt4t"; |
| 53 | + testThreadPool.getThreadContext().putHeader(QueryGroupTask.QUERY_GROUP_ID_HEADER, testQueryGroupId); |
| 54 | + when(queryGroupService.shouldRejectFor(testQueryGroupId)).thenReturn(Optional.empty()); |
| 55 | + |
| 56 | + sut.onRequestStart(null); |
| 57 | + } |
| 58 | +} |
0 commit comments