Search in sources :

Example 1 with Deferred

use of io.joynr.provider.Deferred in project joynr by bmwcarit.

the class TtlUpliftTest method setUp.

@Before
public void setUp() throws NoSuchMethodException, SecurityException {
    fromParticipantId = "sender";
    toParticipantId = "receiver";
    cleanupScheduler = new ScheduledThreadPoolExecutor(1);
    cleanupSchedulerSpy = Mockito.spy(cleanupScheduler);
    Module defaultModule = Modules.override(new JoynrPropertiesModule(new Properties())).with(new JsonMessageSerializerModule(), new AbstractModule() {

        @Override
        protected void configure() {
            bind(Long.class).annotatedWith(Names.named(ConfigurableMessagingSettings.PROPERTY_TTL_UPLIFT_MS)).toInstance(NO_TTL_UPLIFT);
            requestStaticInjection(Request.class);
            Multibinder<JoynrMessageProcessor> joynrMessageProcessorMultibinder = Multibinder.newSetBinder(binder(), new TypeLiteral<JoynrMessageProcessor>() {
            });
            joynrMessageProcessorMultibinder.addBinding().toInstance(new JoynrMessageProcessor() {

                @Override
                public MutableMessage processOutgoing(MutableMessage joynrMessage) {
                    return joynrMessage;
                }

                @Override
                public ImmutableMessage processIncoming(ImmutableMessage joynrMessage) {
                    return joynrMessage;
                }
            });
            bind(PublicationManager.class).to(PublicationManagerImpl.class);
            bind(SubscriptionRequestStorage.class).toInstance(Mockito.mock(FileSubscriptionRequestStorage.class));
            bind(AttributePollInterpreter.class).toInstance(attributePollInterpreter);
            bind(Dispatcher.class).toInstance(dispatcher);
            bind(ProviderDirectory.class).toInstance(providerDirectory);
            bind(ScheduledExecutorService.class).annotatedWith(Names.named(JoynrInjectionConstants.JOYNR_SCHEDULER_CLEANUP)).toInstance(cleanupSchedulerSpy);
        }
    });
    Injector injector = Guice.createInjector(defaultModule);
    messageFactory = injector.getInstance(MutableMessageFactory.class);
    Module ttlUpliftModule = Modules.override(defaultModule).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Long.class).annotatedWith(Names.named(ConfigurableMessagingSettings.PROPERTY_TTL_UPLIFT_MS)).toInstance(TTL_UPLIFT_MS);
        }
    });
    Injector injectorWithTtlUplift = Guice.createInjector(ttlUpliftModule);
    messageFactoryWithTtlUplift = injectorWithTtlUplift.getInstance(MutableMessageFactory.class);
    requestCaller = new RequestCallerFactory().create(provider);
    when(providerContainer.getProviderProxy()).thenReturn(requestCaller.getProxy());
    when(providerContainer.getSubscriptionPublisher()).thenReturn(subscriptionPublisher);
    Deferred<String> valueToPublishDeferred = new Deferred<String>();
    valueToPublishDeferred.resolve(valueToPublish);
    Promise<Deferred<String>> valueToPublishPromise = new Promise<Deferred<String>>(valueToPublishDeferred);
    doReturn(valueToPublishPromise).when(attributePollInterpreter).execute(any(ProviderContainer.class), any(Method.class));
    Module subcriptionUpliftModule = Modules.override(defaultModule).with(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Long.class).annotatedWith(Names.named(ConfigurableMessagingSettings.PROPERTY_TTL_UPLIFT_MS)).toInstance(SUBSCRIPTION_UPLIFT_MS);
        }
    });
    Injector injectorWithPublicationUplift = Guice.createInjector(subcriptionUpliftModule);
    publicationManager = (PublicationManagerImpl) injector.getInstance(PublicationManager.class);
    publicationManagerWithTtlUplift = (PublicationManagerImpl) injectorWithPublicationUplift.getInstance(PublicationManager.class);
    payload = "payload";
    Method method = TestProvider.class.getMethod("methodWithStrings", new Class[] { String.class });
    request = new Request(method.getName(), new String[] { payload }, method.getParameterTypes());
    messagingQos = new MessagingQos(TTL);
    expiryDate = DispatcherUtils.convertTtlToExpirationDate(messagingQos.getRoundTripTtl_ms());
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Deferred(io.joynr.provider.Deferred) JoynrMessageProcessor(io.joynr.messaging.JoynrMessageProcessor) Properties(java.util.Properties) MessagingQos(io.joynr.messaging.MessagingQos) TypeLiteral(com.google.inject.TypeLiteral) MutableMessage(joynr.MutableMessage) JoynrPropertiesModule(io.joynr.common.JoynrPropertiesModule) Injector(com.google.inject.Injector) PublicationManagerImpl(io.joynr.dispatching.subscription.PublicationManagerImpl) ProviderContainer(io.joynr.provider.ProviderContainer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Multibinder(com.google.inject.multibindings.Multibinder) JsonMessageSerializerModule(io.joynr.messaging.JsonMessageSerializerModule) SubscriptionRequest(joynr.SubscriptionRequest) BroadcastSubscriptionRequest(joynr.BroadcastSubscriptionRequest) Request(joynr.Request) Method(java.lang.reflect.Method) AbstractModule(com.google.inject.AbstractModule) Promise(io.joynr.provider.Promise) ImmutableMessage(joynr.ImmutableMessage) Matchers.anyLong(org.mockito.Matchers.anyLong) Module(com.google.inject.Module) JsonMessageSerializerModule(io.joynr.messaging.JsonMessageSerializerModule) JoynrPropertiesModule(io.joynr.common.JoynrPropertiesModule) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 2 with Deferred

use of io.joynr.provider.Deferred in project joynr by bmwcarit.

the class PublicationManagerTest method setUp.

@Before
public void setUp() {
    Deferred<String> valueToPublishDeferred = new Deferred<String>();
    valueToPublishDeferred.resolve(valueToPublish);
    Promise<Deferred<String>> valueToPublishPromise = new Promise<Deferred<String>>(valueToPublishDeferred);
    cleanupScheduler = new ScheduledThreadPoolExecutor(1);
    publicationManager = new PublicationManagerImpl(attributePollInterpreter, dispatcher, providerDirectory, cleanupScheduler, Mockito.mock(SubscriptionRequestStorage.class), shutdownNotifier);
    requestCaller = new RequestCallerFactory().create(provider);
    when(providerContainer.getProviderProxy()).thenReturn(requestCaller.getProxy());
    when(providerContainer.getSubscriptionPublisher()).thenReturn(subscriptionPublisher);
    when(providerDirectory.contains(eq(PROVIDER_PARTICIPANT_ID))).thenReturn(false);
    doReturn(valueToPublishPromise).when(attributePollInterpreter).execute(any(ProviderContainer.class), any(Method.class));
}
Also used : Promise(io.joynr.provider.Promise) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Deferred(io.joynr.provider.Deferred) Matchers.anyString(org.mockito.Matchers.anyString) Method(java.lang.reflect.Method) ProviderContainer(io.joynr.provider.ProviderContainer) RequestCallerFactory(io.joynr.dispatching.RequestCallerFactory) Before(org.junit.Before)

Example 3 with Deferred

use of io.joynr.provider.Deferred in project joynr by bmwcarit.

the class IltProvider method getAttributeWithExceptionFromGetter.

@Override
public Promise<Deferred<Boolean>> getAttributeWithExceptionFromGetter() {
    Deferred<Boolean> deferred = new Deferred<Boolean>();
    deferred.reject(new ProviderRuntimeException("Exception from getAttributeWithExceptionFromGetter"));
    return new Promise<Deferred<Boolean>>(deferred);
}
Also used : Promise(io.joynr.provider.Promise) Deferred(io.joynr.provider.Deferred) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 4 with Deferred

use of io.joynr.provider.Deferred in project joynr by bmwcarit.

the class ProviderWrapper method createAndResolveOrRejectDeferred.

@SuppressWarnings("unchecked")
private AbstractDeferred createAndResolveOrRejectDeferred(Method method, Object result, JoynrException joynrException) {
    AbstractDeferred deferred;
    if (result == null && method.getReturnType().equals(Void.class)) {
        deferred = new DeferredVoid();
        if (joynrException == null) {
            ((DeferredVoid) deferred).resolve();
        }
    } else {
        if (result instanceof MultiReturnValuesContainer) {
            deferred = new MultiValueDeferred();
            if (joynrException == null) {
                ((MultiValueDeferred) deferred).resolve(((MultiReturnValuesContainer) result).getValues());
            }
        } else {
            deferred = new Deferred<Object>();
            if (joynrException == null) {
                ((Deferred<Object>) deferred).resolve(result);
            }
        }
    }
    if (joynrException != null) {
        LOG.debug("Provider method invocation resulted in provider runtime exception - rejecting the deferred {} with {}", deferred, joynrException);
        if (joynrException instanceof ApplicationException) {
            try {
                Method rejectMethod = AbstractDeferred.class.getDeclaredMethod("reject", new Class[] { JoynrException.class });
                rejectMethod.setAccessible(true);
                rejectMethod.invoke(deferred, new Object[] { joynrException });
            } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                LOG.warn("Unable to set {} as rejection reason on {}. Wrapping in ProviderRuntimeException instead.", joynrException, deferred);
                deferred.reject(new ProviderRuntimeException(((ApplicationException) joynrException).getMessage()));
            }
        } else if (joynrException instanceof ProviderRuntimeException) {
            deferred.reject((ProviderRuntimeException) joynrException);
        }
    }
    return deferred;
}
Also used : AbstractDeferred(io.joynr.provider.AbstractDeferred) MultiValueDeferred(io.joynr.provider.MultiValueDeferred) Deferred(io.joynr.provider.Deferred) Method(java.lang.reflect.Method) AbstractDeferred(io.joynr.provider.AbstractDeferred) DeferredVoid(io.joynr.provider.DeferredVoid) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiReturnValuesContainer(io.joynr.dispatcher.rpc.MultiReturnValuesContainer) ApplicationException(joynr.exceptions.ApplicationException) DeferredVoid(io.joynr.provider.DeferredVoid) MultiValueDeferred(io.joynr.provider.MultiValueDeferred) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException)

Example 5 with Deferred

use of io.joynr.provider.Deferred in project joynr by bmwcarit.

the class RpcStubbingTest method setUp.

@Before
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_NULL_PARAM_DEREF", justification = "NPE in test would fail test")
public void setUp() throws JoynrCommunicationException, JoynrSendBufferFullException, JsonGenerationException, JsonMappingException, IOException, JoynrMessageNotSentException {
    Deferred<GpsLocation> deferredGpsLocation = new Deferred<GpsLocation>();
    deferredGpsLocation.resolve(gpsValue);
    when(testMock.returnsGpsLocation()).thenReturn(new Promise<Deferred<GpsLocation>>(deferredGpsLocation));
    Deferred<List<GpsLocation>> deferredGpsLocationList = new Deferred<List<GpsLocation>>();
    deferredGpsLocationList.resolve(gpsList);
    when(testMock.returnsGpsLocationList()).thenReturn(new Promise<Deferred<List<GpsLocation>>>(deferredGpsLocationList));
    DeferredVoid deferredVoid = new DeferredVoid();
    deferredVoid.resolve();
    when(testMock.noParamsNoReturnValue()).thenReturn(new Promise<DeferredVoid>(deferredVoid));
    when(testMock.takesTwoSimpleParams(any(Integer.class), any(String.class))).thenReturn(new Promise<DeferredVoid>(deferredVoid));
    fromParticipantId = UUID.randomUUID().toString();
    toParticipantId = UUID.randomUUID().toString();
    toDiscoveryEntry = new DiscoveryEntryWithMetaInfo();
    toDiscoveryEntry.setParticipantId(toParticipantId);
    // required to inject static members of JoynMessagingConnectorFactory
    injector = Guice.createInjector(new JoynrPropertiesModule(PropertyLoader.loadProperties(MessagingPropertyKeys.DEFAULT_MESSAGING_PROPERTIES_FILE)), new JsonMessageSerializerModule(), new AbstractModule() {

        @Override
        protected void configure() {
            requestStaticInjection(RpcUtils.class);
            install(new JoynrMessageScopeModule());
            MapBinder<Class<? extends Address>, AbstractMiddlewareMessagingStubFactory<? extends IMessagingStub, ? extends Address>> messagingStubFactory;
            messagingStubFactory = MapBinder.newMapBinder(binder(), new TypeLiteral<Class<? extends Address>>() {
            }, new TypeLiteral<AbstractMiddlewareMessagingStubFactory<? extends IMessagingStub, ? extends Address>>() {
            }, Names.named(MessagingStubFactory.MIDDLEWARE_MESSAGING_STUB_FACTORIES));
            messagingStubFactory.addBinding(InProcessAddress.class).to(InProcessMessagingStubFactory.class);
        }
    });
    final RequestInterpreter requestInterpreter = injector.getInstance(RequestInterpreter.class);
    final RequestCallerFactory requestCallerFactory = injector.getInstance(RequestCallerFactory.class);
    when(requestReplyManager.sendSyncRequest(eq(fromParticipantId), eq(toDiscoveryEntry), any(Request.class), any(SynchronizedReplyCaller.class), eq(messagingQos))).thenAnswer(new Answer<Reply>() {

        @Override
        public Reply answer(InvocationOnMock invocation) throws Throwable {
            RequestCaller requestCaller = requestCallerFactory.create(testMock);
            Object[] args = invocation.getArguments();
            Request request = null;
            for (Object arg : args) {
                if (arg instanceof Request) {
                    request = (Request) arg;
                    break;
                }
            }
            final Future<Reply> future = new Future<Reply>();
            ProviderCallback<Reply> callback = new ProviderCallback<Reply>() {

                @Override
                public void onSuccess(Reply result) {
                    future.onSuccess(result);
                }

                @Override
                public void onFailure(JoynrException error) {
                    future.onFailure(error);
                }
            };
            requestInterpreter.execute(callback, requestCaller, request);
            return future.get();
        }
    });
    JoynrMessagingConnectorFactory joynrMessagingConnectorFactory = new JoynrMessagingConnectorFactory(requestReplyManager, replyCallerDirectory, subscriptionManager);
    connector = joynrMessagingConnectorFactory.create(fromParticipantId, Sets.newHashSet(toDiscoveryEntry), messagingQos);
}
Also used : InProcessAddress(io.joynr.messaging.inprocess.InProcessAddress) Address(joynr.system.RoutingTypes.Address) InProcessAddress(io.joynr.messaging.inprocess.InProcessAddress) Deferred(io.joynr.provider.Deferred) DeferredVoid(io.joynr.provider.DeferredVoid) TypeLiteral(com.google.inject.TypeLiteral) JoynrPropertiesModule(io.joynr.common.JoynrPropertiesModule) List(java.util.List) ProviderCallback(io.joynr.provider.ProviderCallback) JsonMessageSerializerModule(io.joynr.messaging.JsonMessageSerializerModule) Request(joynr.Request) GpsLocation(joynr.types.Localisation.GpsLocation) JoynrException(io.joynr.exceptions.JoynrException) AbstractModule(com.google.inject.AbstractModule) JoynrMessageScopeModule(io.joynr.context.JoynrMessageScopeModule) RequestCaller(io.joynr.dispatching.RequestCaller) AbstractMiddlewareMessagingStubFactory(io.joynr.messaging.AbstractMiddlewareMessagingStubFactory) RequestInterpreter(io.joynr.dispatching.rpc.RequestInterpreter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Reply(joynr.Reply) IMessagingStub(io.joynr.messaging.IMessagingStub) SynchronizedReplyCaller(io.joynr.dispatching.rpc.SynchronizedReplyCaller) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) RequestCallerFactory(io.joynr.dispatching.RequestCallerFactory) Before(org.junit.Before)

Aggregations

Deferred (io.joynr.provider.Deferred)6 Promise (io.joynr.provider.Promise)4 Method (java.lang.reflect.Method)3 ProviderRuntimeException (joynr.exceptions.ProviderRuntimeException)3 Before (org.junit.Before)3 AbstractModule (com.google.inject.AbstractModule)2 TypeLiteral (com.google.inject.TypeLiteral)2 JoynrPropertiesModule (io.joynr.common.JoynrPropertiesModule)2 RequestCallerFactory (io.joynr.dispatching.RequestCallerFactory)2 JsonMessageSerializerModule (io.joynr.messaging.JsonMessageSerializerModule)2 DeferredVoid (io.joynr.provider.DeferredVoid)2 ProviderContainer (io.joynr.provider.ProviderContainer)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)2 Request (joynr.Request)2 Injector (com.google.inject.Injector)1 Module (com.google.inject.Module)1 Multibinder (com.google.inject.multibindings.Multibinder)1 JoynrMessageScopeModule (io.joynr.context.JoynrMessageScopeModule)1 MultiReturnValuesContainer (io.joynr.dispatcher.rpc.MultiReturnValuesContainer)1 RequestCaller (io.joynr.dispatching.RequestCaller)1