Search in sources :

Example 1 with SameThreadExecutorService

use of org.apache.lucene.util.SameThreadExecutorService in project crate by crate.

the class BlobStoreTestUtil method mockClusterService.

private static ClusterService mockClusterService(ClusterState initialState) {
    final ThreadPool threadPool = mock(ThreadPool.class);
    when(threadPool.executor(ThreadPool.Names.SNAPSHOT)).thenReturn(new SameThreadExecutorService());
    when(threadPool.generic()).thenReturn(new SameThreadExecutorService());
    final ClusterService clusterService = mock(ClusterService.class);
    final ClusterApplierService clusterApplierService = mock(ClusterApplierService.class);
    when(clusterService.getClusterApplierService()).thenReturn(clusterApplierService);
    final AtomicReference<ClusterState> currentState = new AtomicReference<>(initialState);
    when(clusterService.state()).then(invocationOnMock -> currentState.get());
    final List<ClusterStateApplier> appliers = new CopyOnWriteArrayList<>();
    doAnswer(invocation -> {
        final ClusterStateUpdateTask task = ((ClusterStateUpdateTask) invocation.getArguments()[1]);
        final ClusterState current = currentState.get();
        final ClusterState next = task.execute(current);
        currentState.set(next);
        appliers.forEach(applier -> applier.applyClusterState(new ClusterChangedEvent((String) invocation.getArguments()[0], next, current)));
        task.clusterStateProcessed((String) invocation.getArguments()[0], current, next);
        return null;
    }).when(clusterService).submitStateUpdateTask(anyString(), any(ClusterStateUpdateTask.class));
    doAnswer(invocation -> {
        appliers.add((ClusterStateApplier) invocation.getArguments()[0]);
        return null;
    }).when(clusterService).addStateApplier(any(ClusterStateApplier.class));
    when(clusterApplierService.threadPool()).thenReturn(threadPool);
    return clusterService;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) SameThreadExecutorService(org.apache.lucene.util.SameThreadExecutorService) ClusterService(org.elasticsearch.cluster.service.ClusterService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ClusterStateUpdateTask(org.elasticsearch.cluster.ClusterStateUpdateTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) ClusterApplierService(org.elasticsearch.cluster.service.ClusterApplierService) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SameThreadExecutorService (org.apache.lucene.util.SameThreadExecutorService)1 ClusterChangedEvent (org.elasticsearch.cluster.ClusterChangedEvent)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 ClusterStateApplier (org.elasticsearch.cluster.ClusterStateApplier)1 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)1 ClusterApplierService (org.elasticsearch.cluster.service.ClusterApplierService)1 ClusterService (org.elasticsearch.cluster.service.ClusterService)1 ThreadPool (org.elasticsearch.threadpool.ThreadPool)1