use of org.opensearch.jobscheduler.spi.utils.LockService in project anomaly-detection by opensearch-project.
the class AnomalyDetectorJobRunnerTests method setup.
@SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
super.setUp();
super.setUpLog4jForJUnit(AnomalyDetectorJobRunner.class);
MockitoAnnotations.initMocks(this);
ThreadFactory threadFactory = OpenSearchExecutors.daemonThreadFactory(OpenSearchExecutors.threadName("node1", "test-ad"));
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
executorService = OpenSearchExecutors.newFixed("test-ad", 4, 100, threadFactory, threadContext);
Mockito.doReturn(executorService).when(mockedThreadPool).executor(anyString());
Mockito.doReturn(mockedThreadPool).when(client).threadPool();
Mockito.doReturn(threadContext).when(mockedThreadPool).getThreadContext();
runner.setThreadPool(mockedThreadPool);
runner.setClient(client);
runner.setAnomalyResultHandler(anomalyResultHandler);
runner.setAdTaskManager(adTaskManager);
Settings settings = Settings.builder().put("plugins.anomaly_detection.max_retry_for_backoff", 2).put("plugins.anomaly_detection.backoff_initial_delay", TimeValue.timeValueMillis(1)).put("plugins.anomaly_detection.max_retry_for_end_run_exception", 3).build();
setUpJobParameter();
runner.setSettings(settings);
AnomalyDetectionIndices anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
IndexNameExpressionResolver indexNameResolver = mock(IndexNameExpressionResolver.class);
IndexUtils indexUtils = new IndexUtils(client, clientUtil, clusterService, indexNameResolver);
NodeStateManager stateManager = mock(NodeStateManager.class);
runner.setAnomalyDetectionIndices(indexUtil);
lockService = new LockService(client, clusterService);
doReturn(lockService).when(context).getLockService();
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
if (request.index().equals(ANOMALY_DETECTOR_JOB_INDEX)) {
AnomalyDetectorJob job = TestHelpers.randomAnomalyDetectorJob(true);
listener.onResponse(TestHelpers.createGetResponse(job, randomAlphaOfLength(5), ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
assertTrue(String.format("The size of args is %d. Its content is %s", args.length, Arrays.toString(args)), args.length >= 2);
IndexRequest request = null;
ActionListener<IndexResponse> listener = null;
if (args[0] instanceof IndexRequest) {
request = (IndexRequest) args[0];
}
if (args[1] instanceof ActionListener) {
listener = (ActionListener<IndexResponse>) args[1];
}
assertTrue(request != null && listener != null);
ShardId shardId = new ShardId(new Index(ANOMALY_DETECTOR_JOB_INDEX, randomAlphaOfLength(10)), 0);
listener.onResponse(new IndexResponse(shardId, randomAlphaOfLength(10), request.id(), 1, 1, 1, true));
return null;
}).when(client).index(any(), any());
}
Aggregations