Search in sources :

Example 1 with ExecutionContextExecutor

use of scala.concurrent.ExecutionContextExecutor in project controller by opendaylight.

the class DataChangeListenerRegistrationProxyTest method testFailedRegistration.

@Test
public void testFailedRegistration() {
    new TestKit(getSystem()) {

        {
            ActorSystem mockActorSystem = mock(ActorSystem.class);
            ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration");
            doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
            ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
            ActorContext actorContext = mock(ActorContext.class);
            doReturn(executor).when(actorContext).getClientDispatcher();
            String shardName = "shard-1";
            final DataChangeListenerRegistrationProxy proxy = new DataChangeListenerRegistrationProxy(shardName, actorContext, mockListener);
            doReturn(mockActorSystem).when(actorContext).getActorSystem();
            doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
            doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
            doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
            doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
            proxy.init(YangInstanceIdentifier.of(TestModel.TEST_QNAME), AsyncDataBroker.DataChangeScope.ONE);
            Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
            proxy.close();
        }
    };
}
Also used : ActorSystem(akka.actor.ActorSystem) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) TestKit(akka.testkit.javadsl.TestKit) Props(akka.actor.Props) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) DoNothingActor(org.opendaylight.controller.cluster.raft.utils.DoNothingActor) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor) Test(org.junit.Test)

Example 2 with ExecutionContextExecutor

use of scala.concurrent.ExecutionContextExecutor in project controller by opendaylight.

the class DataTreeChangeListenerProxyTest method testFailedRegistration.

@Test
public void testFailedRegistration() {
    new TestKit(getSystem()) {

        {
            ActorSystem mockActorSystem = mock(ActorSystem.class);
            ActorRef mockActor = getSystem().actorOf(Props.create(DoNothingActor.class), "testFailedRegistration");
            doReturn(mockActor).when(mockActorSystem).actorOf(any(Props.class));
            ExecutionContextExecutor executor = ExecutionContexts.fromExecutor(MoreExecutors.directExecutor());
            ActorContext actorContext = mock(ActorContext.class);
            final YangInstanceIdentifier path = YangInstanceIdentifier.of(TestModel.TEST_QNAME);
            doReturn(executor).when(actorContext).getClientDispatcher();
            doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
            doReturn(mockActorSystem).when(actorContext).getActorSystem();
            String shardName = "shard-1";
            final DataTreeChangeListenerProxy<DOMDataTreeChangeListener> proxy = new DataTreeChangeListenerProxy<>(actorContext, mockListener, path);
            doReturn(duration("5 seconds")).when(actorContext).getOperationDuration();
            doReturn(Futures.successful(getRef())).when(actorContext).findLocalShardAsync(eq(shardName));
            doReturn(Futures.failed(new RuntimeException("mock"))).when(actorContext).executeOperationAsync(any(ActorRef.class), any(Object.class), any(Timeout.class));
            doReturn(mock(DatastoreContext.class)).when(actorContext).getDatastoreContext();
            proxy.init("shard-1");
            Assert.assertEquals("getListenerRegistrationActor", null, proxy.getListenerRegistrationActor());
            proxy.close();
        }
    };
}
Also used : ActorSystem(akka.actor.ActorSystem) ActorRef(akka.actor.ActorRef) Timeout(akka.util.Timeout) TestKit(akka.testkit.javadsl.TestKit) Props(akka.actor.Props) YangInstanceIdentifier(org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier) ActorContext(org.opendaylight.controller.cluster.datastore.utils.ActorContext) DoNothingActor(org.opendaylight.controller.cluster.raft.utils.DoNothingActor) DOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener) ClusteredDOMDataTreeChangeListener(org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor) Test(org.junit.Test)

Example 3 with ExecutionContextExecutor

use of scala.concurrent.ExecutionContextExecutor in project SSM by Intel-bigdata.

the class AgentUtils method repeatActionUntil.

public static Cancellable repeatActionUntil(ActorSystem system, FiniteDuration initialDelay, FiniteDuration interval, FiniteDuration timeout, Runnable action, Runnable onTimeout) {
    final Scheduler scheduler = system.scheduler();
    final ExecutionContextExecutor dispatcher = system.dispatcher();
    final Cancellable run = scheduler.schedule(initialDelay, interval, action, dispatcher);
    final Cancellable cancelRun = scheduler.scheduleOnce(timeout, new Runnable() {

        @Override
        public void run() {
            run.cancel();
        }
    }, dispatcher);
    final Cancellable fail = scheduler.scheduleOnce(timeout, onTimeout, dispatcher);
    return new Cancellable() {

        @Override
        public boolean cancel() {
            return run.cancel() && cancelRun.cancel() && fail.cancel();
        }

        @Override
        public boolean isCancelled() {
            return run.isCancelled() && cancelRun.isCancelled() && fail.isCancelled();
        }
    };
}
Also used : Scheduler(akka.actor.Scheduler) Cancellable(akka.actor.Cancellable) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor)

Example 4 with ExecutionContextExecutor

use of scala.concurrent.ExecutionContextExecutor in project flink by apache.

the class MetricFetcherTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    // ========= setup TaskManager =================================================================================
    JobID jobID = new JobID();
    InstanceID tmID = new InstanceID();
    ResourceID tmRID = new ResourceID(tmID.toString());
    TaskManagerGateway taskManagerGateway = mock(TaskManagerGateway.class);
    when(taskManagerGateway.getAddress()).thenReturn("/tm/address");
    Instance taskManager = mock(Instance.class);
    when(taskManager.getTaskManagerGateway()).thenReturn(taskManagerGateway);
    when(taskManager.getId()).thenReturn(tmID);
    when(taskManager.getTaskManagerID()).thenReturn(tmRID);
    // ========= setup JobManager ==================================================================================
    JobDetails details = mock(JobDetails.class);
    when(details.getJobId()).thenReturn(jobID);
    ActorGateway jobManagerGateway = mock(ActorGateway.class);
    Object registeredTaskManagersAnswer = new JobManagerMessages.RegisteredTaskManagers(JavaConverters.collectionAsScalaIterableConverter(Collections.singletonList(taskManager)).asScala());
    when(jobManagerGateway.ask(isA(RequestJobDetails.class), any(FiniteDuration.class))).thenReturn(Future$.MODULE$.successful((Object) new MultipleJobsDetails(new JobDetails[0], new JobDetails[0])));
    when(jobManagerGateway.ask(isA(JobManagerMessages.RequestRegisteredTaskManagers$.class), any(FiniteDuration.class))).thenReturn(Future$.MODULE$.successful(registeredTaskManagersAnswer));
    when(jobManagerGateway.path()).thenReturn("/jm/address");
    JobManagerRetriever retriever = mock(JobManagerRetriever.class);
    when(retriever.getJobManagerGatewayAndWebPort()).thenReturn(Option.apply(new scala.Tuple2<ActorGateway, Integer>(jobManagerGateway, 0)));
    // ========= setup QueryServices ================================================================================
    Object requestMetricsAnswer = createRequestDumpAnswer(tmID, jobID);
    final ActorRef jmQueryService = mock(ActorRef.class);
    final ActorRef tmQueryService = mock(ActorRef.class);
    ActorSystem actorSystem = mock(ActorSystem.class);
    when(actorSystem.actorFor(eq("/jm/" + METRIC_QUERY_SERVICE_NAME))).thenReturn(jmQueryService);
    when(actorSystem.actorFor(eq("/tm/" + METRIC_QUERY_SERVICE_NAME + "_" + tmRID.getResourceIdString()))).thenReturn(tmQueryService);
    MetricFetcher.BasicGateway jmQueryServiceGateway = mock(MetricFetcher.BasicGateway.class);
    when(jmQueryServiceGateway.ask(any(MetricQueryService.getCreateDump().getClass()), any(FiniteDuration.class))).thenReturn(Future$.MODULE$.successful((Object) new MetricDumpSerialization.MetricSerializationResult(new byte[0], 0, 0, 0, 0)));
    MetricFetcher.BasicGateway tmQueryServiceGateway = mock(MetricFetcher.BasicGateway.class);
    when(tmQueryServiceGateway.ask(any(MetricQueryService.getCreateDump().getClass()), any(FiniteDuration.class))).thenReturn(Future$.MODULE$.successful(requestMetricsAnswer));
    whenNew(MetricFetcher.BasicGateway.class).withArguments(eq(new Object() {

        @Override
        public boolean equals(Object o) {
            return o == jmQueryService;
        }
    })).thenReturn(jmQueryServiceGateway);
    whenNew(MetricFetcher.BasicGateway.class).withArguments(eq(new Object() {

        @Override
        public boolean equals(Object o) {
            return o == tmQueryService;
        }
    })).thenReturn(tmQueryServiceGateway);
    // ========= start MetricFetcher testing =======================================================================
    ExecutionContextExecutor context = ExecutionContext$.MODULE$.fromExecutor(new CurrentThreadExecutor());
    MetricFetcher fetcher = new MetricFetcher(actorSystem, retriever, context);
    // verify that update fetches metrics and updates the store
    fetcher.update();
    MetricStore store = fetcher.getMetricStore();
    synchronized (store) {
        assertEquals("7", store.jobManager.metrics.get("abc.hist_min"));
        assertEquals("6", store.jobManager.metrics.get("abc.hist_max"));
        assertEquals("4.0", store.jobManager.metrics.get("abc.hist_mean"));
        assertEquals("0.5", store.jobManager.metrics.get("abc.hist_median"));
        assertEquals("5.0", store.jobManager.metrics.get("abc.hist_stddev"));
        assertEquals("0.75", store.jobManager.metrics.get("abc.hist_p75"));
        assertEquals("0.9", store.jobManager.metrics.get("abc.hist_p90"));
        assertEquals("0.95", store.jobManager.metrics.get("abc.hist_p95"));
        assertEquals("0.98", store.jobManager.metrics.get("abc.hist_p98"));
        assertEquals("0.99", store.jobManager.metrics.get("abc.hist_p99"));
        assertEquals("0.999", store.jobManager.metrics.get("abc.hist_p999"));
        assertEquals("x", store.getTaskManagerMetricStore(tmID.toString()).metrics.get("abc.gauge"));
        assertEquals("5.0", store.getJobMetricStore(jobID.toString()).metrics.get("abc.jc"));
        assertEquals("2", store.getTaskMetricStore(jobID.toString(), "taskid").metrics.get("2.abc.tc"));
        assertEquals("1", store.getTaskMetricStore(jobID.toString(), "taskid").metrics.get("2.opname.abc.oc"));
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) InstanceID(org.apache.flink.runtime.instance.InstanceID) Instance(org.apache.flink.runtime.instance.Instance) ActorRef(akka.actor.ActorRef) TaskManagerGateway(org.apache.flink.runtime.jobmanager.slots.TaskManagerGateway) MultipleJobsDetails(org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails) RequestJobDetails(org.apache.flink.runtime.messages.webmonitor.RequestJobDetails) JobDetails(org.apache.flink.runtime.messages.webmonitor.JobDetails) MetricDumpSerialization(org.apache.flink.runtime.metrics.dump.MetricDumpSerialization) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) ActorGateway(org.apache.flink.runtime.instance.ActorGateway) ExecutionContextExecutor(scala.concurrent.ExecutionContextExecutor) FiniteDuration(scala.concurrent.duration.FiniteDuration) RequestJobDetails(org.apache.flink.runtime.messages.webmonitor.RequestJobDetails) Tuple2(org.apache.flink.api.java.tuple.Tuple2) JobManagerRetriever(org.apache.flink.runtime.webmonitor.JobManagerRetriever) JobID(org.apache.flink.api.common.JobID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ExecutionContextExecutor (scala.concurrent.ExecutionContextExecutor)4 ActorRef (akka.actor.ActorRef)3 ActorSystem (akka.actor.ActorSystem)3 Test (org.junit.Test)3 Props (akka.actor.Props)2 TestKit (akka.testkit.javadsl.TestKit)2 Timeout (akka.util.Timeout)2 ActorContext (org.opendaylight.controller.cluster.datastore.utils.ActorContext)2 DoNothingActor (org.opendaylight.controller.cluster.raft.utils.DoNothingActor)2 Cancellable (akka.actor.Cancellable)1 Scheduler (akka.actor.Scheduler)1 JobID (org.apache.flink.api.common.JobID)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 ResourceID (org.apache.flink.runtime.clusterframework.types.ResourceID)1 ActorGateway (org.apache.flink.runtime.instance.ActorGateway)1 Instance (org.apache.flink.runtime.instance.Instance)1 InstanceID (org.apache.flink.runtime.instance.InstanceID)1 TaskManagerGateway (org.apache.flink.runtime.jobmanager.slots.TaskManagerGateway)1 JobDetails (org.apache.flink.runtime.messages.webmonitor.JobDetails)1 MultipleJobsDetails (org.apache.flink.runtime.messages.webmonitor.MultipleJobsDetails)1