Search in sources :

Example 11 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class ArbitrationTest method setUp.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Before
public void setUp() throws Exception {
    initMocks(this);
    capabilitiesList = new ArrayList<DiscoveryEntryWithMetaInfo>();
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            assert (arguments[0] instanceof Callback);
            ((Callback) arguments[0]).resolve((Object) capabilitiesList.toArray(new DiscoveryEntryWithMetaInfo[0]));
            localDiscoveryAggregatorSemaphore.release();
            return null;
        }
    }).when(localDiscoveryAggregator).lookup(Mockito.<Callback>any(), eq(new String[] { domain }), eq(interfaceName), Mockito.<joynr.types.DiscoveryQos>any());
    Field discoveryEntryVersionFilterField = ArbitratorFactory.class.getDeclaredField("discoveryEntryVersionFilter");
    discoveryEntryVersionFilterField.setAccessible(true);
    discoveryEntryVersionFilterField.set(ArbitratorFactory.class, discoveryEntryVersionFilter);
    doAnswer(new Answer<Set<DiscoveryEntry>>() {

        @Override
        public Set<DiscoveryEntry> answer(InvocationOnMock invocation) throws Throwable {
            return (Set<DiscoveryEntry>) invocation.getArguments()[1];
        }
    }).when(discoveryEntryVersionFilter).filter(Mockito.<Version>any(), Mockito.<Set<DiscoveryEntryWithMetaInfo>>any(), Mockito.<Map<String, Set<Version>>>any());
    Field schedulerField = ArbitratorFactory.class.getDeclaredField("scheduler");
    schedulerField.setAccessible(true);
    String name = "TEST.joynr.scheduler.arbitration.arbitratorRunnable";
    ThreadFactory joynrThreadFactory = new JoynrThreadFactory(name, true);
    scheduler = Executors.newSingleThreadScheduledExecutor(joynrThreadFactory);
    schedulerField.set(ArbitratorFactory.class, scheduler);
    Field shutdownNotifierField = ArbitratorFactory.class.getDeclaredField("shutdownNotifier");
    shutdownNotifierField.setAccessible(true);
    shutdownNotifierField.set(ArbitratorFactory.class, shutdownNotifier);
    ArbitratorFactory.start();
    verify(shutdownNotifier).registerForShutdown(any(ShutdownListener.class));
}
Also used : JoynrThreadFactory(io.joynr.runtime.JoynrThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) DiscoveryEntry(joynr.types.DiscoveryEntry) Set(java.util.Set) HashSet(java.util.HashSet) ShutdownListener(io.joynr.runtime.ShutdownListener) Field(java.lang.reflect.Field) Callback(io.joynr.proxy.Callback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JoynrThreadFactory(io.joynr.runtime.JoynrThreadFactory) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) Before(org.junit.Before)

Example 12 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryTest method testLookup_DiscoveryEntriesWithMetaInfoContainExpectedIsLocalValue.

@Test
public void testLookup_DiscoveryEntriesWithMetaInfoContainExpectedIsLocalValue() {
    String globalDomain = "globaldomain";
    String remoteGlobalDomain = "remoteglobaldomain";
    String[] domains = new String[] { "localdomain", globalDomain, remoteGlobalDomain };
    String interfaceName = "interfaceName";
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.LOCAL_THEN_GLOBAL);
    CapabilitiesCallback capabilitiesCallback = mock(CapabilitiesCallback.class);
    // local DiscoveryEntry
    DiscoveryEntry localEntry = new DiscoveryEntry();
    localEntry.setDomain(domains[0]);
    when(localDiscoveryEntryStoreMock.lookup(eq(domains), eq(interfaceName))).thenReturn(Lists.newArrayList(localEntry));
    // cached global DiscoveryEntry
    GlobalDiscoveryEntry cachedGlobalEntry = new GlobalDiscoveryEntry();
    cachedGlobalEntry.setDomain(globalDomain);
    Collection<DiscoveryEntry> cachedEntries = Lists.newArrayList((DiscoveryEntry) cachedGlobalEntry);
    when(globalDiscoveryEntryCacheMock.lookup(eq(domains), eq(interfaceName), eq(discoveryQos.getCacheMaxAgeMs()))).thenReturn(cachedEntries);
    // remote global DiscoveryEntry
    final GlobalDiscoveryEntry remoteGlobalEntry = new GlobalDiscoveryEntry(new Version(0, 0), remoteGlobalDomain, interfaceName, "participantIdRemote", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 10000L, "publicKeyId", channelAddressSerialized);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Callback<List<GlobalDiscoveryEntry>> callback = (Callback<List<GlobalDiscoveryEntry>>) invocation.getArguments()[0];
            callback.onSuccess(Lists.newArrayList(remoteGlobalEntry));
            return null;
        }
    }).when(globalCapabilitiesClient).lookup(any(Callback.class), eq(new String[] { remoteGlobalDomain }), eq(interfaceName), anyLong());
    localCapabilitiesDirectory.lookup(domains, interfaceName, discoveryQos, capabilitiesCallback);
    verify(capabilitiesCallback).processCapabilitiesReceived(capabilitiesCaptor.capture());
    Collection<DiscoveryEntryWithMetaInfo> captured = capabilitiesCaptor.getValue();
    assertNotNull(captured);
    assertEquals(3, captured.size());
    DiscoveryEntryWithMetaInfo localEntryWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(true, localEntry);
    assertTrue(captured.contains(localEntryWithMetaInfo));
    DiscoveryEntryWithMetaInfo cachedGlobalEntryWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, cachedGlobalEntry);
    assertTrue(captured.contains(cachedGlobalEntryWithMetaInfo));
    DiscoveryEntryWithMetaInfo remoteGlobalEntryWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, remoteGlobalEntry);
    assertTrue(captured.contains(remoteGlobalEntryWithMetaInfo));
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) Matchers.anyString(org.mockito.Matchers.anyString) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) Callback(io.joynr.proxy.Callback) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) List(java.util.List) ArrayList(java.util.ArrayList) DeferredVoid(io.joynr.provider.DeferredVoid) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 13 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryTest method testLookupByParticipantId_globalEntry_DiscoveryEntryWithMetaInfoContainsExpectedIsLocalValue.

@Test
public void testLookupByParticipantId_globalEntry_DiscoveryEntryWithMetaInfoContainsExpectedIsLocalValue() {
    String participantId = "participantId";
    String interfaceName = "interfaceName";
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.LOCAL_THEN_GLOBAL);
    // remote global DiscoveryEntry
    String remoteGlobalDomain = "remoteglobaldomain";
    final GlobalDiscoveryEntry remoteGlobalEntry = new GlobalDiscoveryEntry(new Version(0, 0), remoteGlobalDomain, interfaceName, participantId, new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 10000L, "publicKeyId", channelAddressSerialized);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Callback<GlobalDiscoveryEntry> callback = (Callback<GlobalDiscoveryEntry>) invocation.getArguments()[0];
            callback.onSuccess(remoteGlobalEntry);
            return null;
        }
    }).when(globalCapabilitiesClient).lookup(any(Callback.class), eq(participantId), anyLong());
    DiscoveryEntryWithMetaInfo capturedRemoteGlobalEntry = localCapabilitiesDirectory.lookup(participantId, discoveryQos);
    DiscoveryEntryWithMetaInfo remoteGlobalEntryWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, remoteGlobalEntry);
    assertEquals(remoteGlobalEntryWithMetaInfo, capturedRemoteGlobalEntry);
}
Also used : Callback(io.joynr.proxy.Callback) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) Matchers.anyString(org.mockito.Matchers.anyString) DeferredVoid(io.joynr.provider.DeferredVoid) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 14 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryTest method createAddAnswerWithError.

private Answer<Future<Void>> createAddAnswerWithError() {
    return new Answer<Future<Void>>() {

        @SuppressWarnings("unchecked")
        @Override
        public Future<Void> answer(InvocationOnMock invocation) throws Throwable {
            Future<Void> result = new Future<Void>();
            Object[] args = invocation.getArguments();
            ((Callback<Void>) args[0]).onFailure(new JoynrRuntimeException("Simulating a JoynrRuntimeException on callback"));
            result.onSuccess(null);
            return result;
        }
    };
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) Callback(io.joynr.proxy.Callback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(io.joynr.proxy.Future) DeferredVoid(io.joynr.provider.DeferredVoid) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException)

Example 15 with Callback

use of io.joynr.proxy.Callback in project joynr by bmwcarit.

the class LocalCapabilitiesDirectoryImpl method shutdown.

@Override
public void shutdown(boolean unregisterAllRegisteredCapabilities) {
    freshnessUpdateScheduler.shutdownNow();
    if (unregisterAllRegisteredCapabilities) {
        Set<DiscoveryEntry> allDiscoveryEntries = localDiscoveryEntryStore.getAllDiscoveryEntries();
        List<DiscoveryEntry> discoveryEntries = new ArrayList<>(allDiscoveryEntries.size());
        for (DiscoveryEntry capabilityEntry : allDiscoveryEntries) {
            if (capabilityEntry.getQos().getScope() == ProviderScope.GLOBAL) {
                discoveryEntries.add(capabilityEntry);
            }
        }
        if (discoveryEntries.size() > 0) {
            try {
                Function<? super DiscoveryEntry, String> transfomerFct = new Function<DiscoveryEntry, String>() {

                    @Override
                    public String apply(DiscoveryEntry input) {
                        return input != null ? input.getParticipantId() : null;
                    }
                };
                Callback<Void> callback = new Callback<Void>() {

                    @Override
                    public void onFailure(JoynrRuntimeException error) {
                    }

                    @Override
                    public void onSuccess(Void result) {
                    }
                };
                globalCapabilitiesDirectoryClient.remove(callback, Lists.newArrayList(Collections2.transform(discoveryEntries, transfomerFct)));
            } catch (DiscoveryException e) {
                logger.debug("error removing discovery entries", e);
            }
        }
    }
}
Also used : Function(com.google.common.base.Function) DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) Callback(io.joynr.proxy.Callback) ArrayList(java.util.ArrayList) DeferredVoid(io.joynr.provider.DeferredVoid) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) DiscoveryException(io.joynr.exceptions.DiscoveryException)

Aggregations

Callback (io.joynr.proxy.Callback)15 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)9 DiscoveryEntry (joynr.types.DiscoveryEntry)9 Test (org.junit.Test)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 GlobalDiscoveryEntry (joynr.types.GlobalDiscoveryEntry)8 DeferredVoid (io.joynr.provider.DeferredVoid)6 ArrayList (java.util.ArrayList)6 DiscoveryEntryWithMetaInfo (joynr.types.DiscoveryEntryWithMetaInfo)6 ProviderQos (joynr.types.ProviderQos)6 Matchers.anyString (org.mockito.Matchers.anyString)6 DiscoveryQos (io.joynr.arbitration.DiscoveryQos)5 Future (io.joynr.proxy.Future)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Version (joynr.types.Version)4 MessagingQos (io.joynr.messaging.MessagingQos)2 ProxyCreatedCallback (io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback)2 Field (java.lang.reflect.Field)2 Semaphore (java.util.concurrent.Semaphore)2