Search in sources :

Example 11 with SubscriptionQos

use of io.joynr.pubsub.SubscriptionQos in project joynr by bmwcarit.

the class PublicationManagerTest method persistedSubscriptionRequestsAreQueued.

@SuppressWarnings("unchecked")
@Test(timeout = 3000000)
public void persistedSubscriptionRequestsAreQueued() throws Exception {
    String persistenceFileName = "target/" + PublicationManagerTest.class.getCanonicalName() + ".test_persistenceSubscriptionRequests";
    String providerParticipantId = "providerParticipantId";
    String proxyParticipantId = "proxyParticipantId";
    int period = 200;
    int times = 5;
    int validityMs = (period * times) + period;
    long publicationTtl = validityMs;
    SubscriptionQos qos = new PeriodicSubscriptionQos().setPeriodMs(period).setValidityMs(validityMs).setPublicationTtlMs(publicationTtl);
    SubscriptionRequest subscriptionRequest = new SubscriptionRequest(SUBSCRIPTION_ID, "location", qos);
    new File(persistenceFileName).delete();
    // pre-fill the persistence file
    FileSubscriptionRequestStorage fileSubscriptionRequestStorage = new FileSubscriptionRequestStorage(persistenceFileName);
    assertEquals(0, fileSubscriptionRequestStorage.getSavedSubscriptionRequests().size());
    // no providers are currently registered
    ProviderDirectory myProviderDirectory = new ProviderDirectory();
    publicationManager = new PublicationManagerImpl(attributePollInterpreter, dispatcher, myProviderDirectory, cleanupScheduler, fileSubscriptionRequestStorage, shutdownNotifier);
    publicationManager.addSubscriptionRequest(proxyParticipantId, providerParticipantId, subscriptionRequest);
    assertEquals(1, fileSubscriptionRequestStorage.getSavedSubscriptionRequests().size());
    publicationManager.shutdown();
    // open the persistence file that should now contain one element
    fileSubscriptionRequestStorage = new FileSubscriptionRequestStorage(persistenceFileName);
    assertEquals(1, fileSubscriptionRequestStorage.getSavedSubscriptionRequests().size());
    publicationManager = new PublicationManagerImpl(attributePollInterpreter, dispatcher, myProviderDirectory, cleanupScheduler, fileSubscriptionRequestStorage, shutdownNotifier);
    // when the provider is registered, persisted subscription requests should be activated
    myProviderDirectory.add(providerParticipantId, providerContainer);
    verify(dispatcher, timeout(validityMs).atLeast(times)).sendSubscriptionPublication(any(String.class), any(Set.class), any(SubscriptionPublication.class), any(MessagingQos.class));
    ;
    Thread.sleep(validityMs + 1000);
    publicationManager.shutdown();
    // Start again with the same file, that should now be empty as all persisted subscriptions were already queued
    reset(dispatcher);
    fileSubscriptionRequestStorage = new FileSubscriptionRequestStorage(persistenceFileName);
    publicationManager = new PublicationManagerImpl(attributePollInterpreter, dispatcher, myProviderDirectory, cleanupScheduler, fileSubscriptionRequestStorage, shutdownNotifier);
    verifyNoMoreInteractions(dispatcher);
    fileSubscriptionRequestStorage = new FileSubscriptionRequestStorage(persistenceFileName);
    assertEquals(0, fileSubscriptionRequestStorage.getSavedSubscriptionRequests().size());
}
Also used : Set(java.util.Set) Matchers.anyString(org.mockito.Matchers.anyString) PeriodicSubscriptionQos(joynr.PeriodicSubscriptionQos) SubscriptionRequest(joynr.SubscriptionRequest) BroadcastSubscriptionRequest(joynr.BroadcastSubscriptionRequest) MessagingQos(io.joynr.messaging.MessagingQos) SubscriptionPublication(joynr.SubscriptionPublication) ProviderDirectory(io.joynr.dispatching.ProviderDirectory) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) PeriodicSubscriptionQos(joynr.PeriodicSubscriptionQos) SubscriptionQos(io.joynr.pubsub.SubscriptionQos) File(java.io.File) Test(org.junit.Test)

Example 12 with SubscriptionQos

use of io.joynr.pubsub.SubscriptionQos in project joynr by bmwcarit.

the class PublicationManagerTest method modifySubscriptionTypeForExistingSubscription.

@SuppressWarnings("unchecked")
@Test(timeout = 3000)
public void modifySubscriptionTypeForExistingSubscription() throws Exception {
    publicationManager = new PublicationManagerImpl(attributePollInterpreter, dispatcher, providerDirectory, cleanupScheduler, Mockito.mock(SubscriptionRequestStorage.class), shutdownNotifier);
    int period = 200;
    int testLengthMax = 3000;
    long validityMs = testLengthMax;
    long publicationTtl = testLengthMax;
    SubscriptionQos qos = new PeriodicSubscriptionQos().setPeriodMs(period).setValidityMs(validityMs).setPublicationTtlMs(publicationTtl);
    SubscriptionRequest subscriptionRequest = new SubscriptionRequest(SUBSCRIPTION_ID, "location", qos);
    when(providerDirectory.get(eq(PROVIDER_PARTICIPANT_ID))).thenReturn(providerContainer);
    when(providerDirectory.contains(eq(PROVIDER_PARTICIPANT_ID))).thenReturn(true);
    publicationManager.addSubscriptionRequest(PROXY_PARTICIPANT_ID, PROVIDER_PARTICIPANT_ID, subscriptionRequest);
    verify(dispatcher, timeout(period * 5).times(6)).sendSubscriptionPublication(eq(PROVIDER_PARTICIPANT_ID), (Set<String>) argThat(contains(PROXY_PARTICIPANT_ID)), any(SubscriptionPublication.class), any(MessagingQos.class));
    qos = new OnChangeSubscriptionQos().setMinIntervalMs(0).setValidityMs(validityMs).setPublicationTtlMs(publicationTtl);
    subscriptionRequest = new SubscriptionRequest(SUBSCRIPTION_ID, "location", qos);
    when(providerDirectory.get(eq(PROVIDER_PARTICIPANT_ID))).thenReturn(providerContainer);
    when(providerDirectory.contains(eq(PROVIDER_PARTICIPANT_ID))).thenReturn(true);
    publicationManager.addSubscriptionRequest(PROXY_PARTICIPANT_ID, PROVIDER_PARTICIPANT_ID, subscriptionRequest);
    reset(dispatcher);
    publicationManager.attributeValueChanged(SUBSCRIPTION_ID, valueToPublish);
    verify(dispatcher, timeout(testLengthMax).times(1)).sendSubscriptionPublication(eq(PROVIDER_PARTICIPANT_ID), (Set<String>) argThat(contains(PROXY_PARTICIPANT_ID)), any(SubscriptionPublication.class), any(MessagingQos.class));
}
Also used : SubscriptionRequest(joynr.SubscriptionRequest) BroadcastSubscriptionRequest(joynr.BroadcastSubscriptionRequest) MessagingQos(io.joynr.messaging.MessagingQos) SubscriptionPublication(joynr.SubscriptionPublication) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) PeriodicSubscriptionQos(joynr.PeriodicSubscriptionQos) SubscriptionQos(io.joynr.pubsub.SubscriptionQos) Matchers.anyString(org.mockito.Matchers.anyString) PeriodicSubscriptionQos(joynr.PeriodicSubscriptionQos) Test(org.junit.Test)

Example 13 with SubscriptionQos

use of io.joynr.pubsub.SubscriptionQos in project joynr by bmwcarit.

the class SubscriptionQosTest method createOnChangeSubscriptionQosWithValidity.

@Test
public void createOnChangeSubscriptionQosWithValidity() throws Exception {
    long validityMs = 100000;
    long lowerBound = System.currentTimeMillis() + validityMs;
    SubscriptionQos onChangeSubscriptionQos = new OnChangeSubscriptionQos().setValidityMs(validityMs);
    long upperBound = System.currentTimeMillis() + validityMs;
    assertTrue(onChangeSubscriptionQos.getExpiryDateMs() >= lowerBound);
    assertTrue(onChangeSubscriptionQos.getExpiryDateMs() <= upperBound);
}
Also used : SubscriptionQos(io.joynr.pubsub.SubscriptionQos) Test(org.junit.Test)

Example 14 with SubscriptionQos

use of io.joynr.pubsub.SubscriptionQos in project joynr by bmwcarit.

the class PublicationManagerImpl method handleSubscriptionRequest.

private void handleSubscriptionRequest(PublicationInformation publicationInformation, SubscriptionRequest subscriptionRequest, ProviderContainer providerContainer) {
    final String subscriptionId = subscriptionRequest.getSubscriptionId();
    SubscriptionQos subscriptionQos = subscriptionRequest.getQos();
    MessagingQos messagingQos = createMessagingQos(subscriptionQos);
    try {
        Method method = findGetterForAttributeName(providerContainer.getProviderProxy().getClass(), subscriptionRequest.getSubscribedToName());
        triggerPublication(publicationInformation, providerContainer, method);
        boolean hasSubscriptionHeartBeat = subscriptionQos instanceof HeartbeatSubscriptionInformation;
        boolean isOnChangeSubscription = subscriptionQos instanceof OnChangeSubscriptionQos;
        if (hasSubscriptionHeartBeat || isOnChangeSubscription) {
            // TODO: send error subscription reply is periodMs < MIN_PERIOD_MS or periodMs > MAX_PERIOD_MS?
            final PublicationTimer timer = new PublicationTimer(publicationInformation, method, providerContainer, this, attributePollInterpreter);
            timer.startTimer();
            publicationTimers.put(subscriptionId, timer);
        }
        if (subscriptionQos instanceof OnChangeSubscriptionQos) {
            handleOnChangeSubscription(subscriptionRequest, providerContainer, subscriptionId);
        }
        dispatcher.sendSubscriptionReply(publicationInformation.providerParticipantId, publicationInformation.proxyParticipantId, new SubscriptionReply(subscriptionId), messagingQos);
    } catch (NoSuchMethodException e) {
        cancelPublicationCreation(subscriptionId);
        logger.error("Error subscribing: {}. The provider does not have the requested attribute", subscriptionRequest);
        sendSubscriptionReplyWithError(publicationInformation, subscriptionId, e, messagingQos);
    }
}
Also used : SubscriptionReply(joynr.SubscriptionReply) MessagingQos(io.joynr.messaging.MessagingQos) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) UnicastSubscriptionQos(joynr.UnicastSubscriptionQos) SubscriptionQos(io.joynr.pubsub.SubscriptionQos) Method(java.lang.reflect.Method) HeartbeatSubscriptionInformation(io.joynr.pubsub.HeartbeatSubscriptionInformation)

Example 15 with SubscriptionQos

use of io.joynr.pubsub.SubscriptionQos in project joynr by bmwcarit.

the class PublicationManagerImpl method sendSubscriptionReplyWithError.

private void sendSubscriptionReplyWithError(SubscriptionException e, PublicationInformation publicationInformation, SubscriptionRequest subscriptionRequest) {
    SubscriptionQos subscriptionQos = subscriptionRequest.getQos();
    MessagingQos messagingQos = new MessagingQos();
    if (subscriptionQos.getExpiryDateMs() == SubscriptionQos.NO_EXPIRY_DATE) {
        messagingQos.setTtl_ms(SubscriptionQos.INFINITE_SUBSCRIPTION);
    } else {
        // TTL uplift will be done in JoynrMessageFactory
        messagingQos.setTtl_ms(subscriptionQos.getExpiryDateMs() - System.currentTimeMillis());
    }
    SubscriptionReply subscriptionReply = new SubscriptionReply(publicationInformation.getSubscriptionId(), e);
    dispatcher.sendSubscriptionReply(publicationInformation.providerParticipantId, publicationInformation.proxyParticipantId, subscriptionReply, messagingQos);
}
Also used : SubscriptionReply(joynr.SubscriptionReply) MessagingQos(io.joynr.messaging.MessagingQos) OnChangeSubscriptionQos(joynr.OnChangeSubscriptionQos) UnicastSubscriptionQos(joynr.UnicastSubscriptionQos) SubscriptionQos(io.joynr.pubsub.SubscriptionQos)

Aggregations

SubscriptionQos (io.joynr.pubsub.SubscriptionQos)25 OnChangeSubscriptionQos (joynr.OnChangeSubscriptionQos)18 Test (org.junit.Test)17 MessagingQos (io.joynr.messaging.MessagingQos)10 PeriodicSubscriptionQos (joynr.PeriodicSubscriptionQos)10 SubscriptionRequest (joynr.SubscriptionRequest)8 BroadcastSubscriptionRequest (joynr.BroadcastSubscriptionRequest)7 Matchers.anyString (org.mockito.Matchers.anyString)7 OnChangeWithKeepAliveSubscriptionQos (joynr.OnChangeWithKeepAliveSubscriptionQos)5 UnicastSubscriptionQos (joynr.UnicastSubscriptionQos)5 Method (java.lang.reflect.Method)4 SubscriptionPublication (joynr.SubscriptionPublication)4 SubscriptionException (io.joynr.exceptions.SubscriptionException)3 AttributeSubscribeInvocation (io.joynr.proxy.invocation.AttributeSubscribeInvocation)3 MulticastSubscriptionQos (joynr.MulticastSubscriptionQos)3 SubscriptionReply (joynr.SubscriptionReply)3 PersistedSubscriptionRequest (io.joynr.dispatching.subscription.PersistedSubscriptionRequest)2 MulticastSubscribeInvocation (io.joynr.proxy.invocation.MulticastSubscribeInvocation)2 AttributeSubscriptionAdapter (io.joynr.pubsub.subscription.AttributeSubscriptionAdapter)2 AttributeSubscriptionListener (io.joynr.pubsub.subscription.AttributeSubscriptionListener)2