Search in sources :

Example 6 with Callback

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

the class LocalDiscoveryTest method testMixedLocalCachedRemoteDiscoveryEntries.

@SuppressWarnings("unchecked")
@Test
public void testMixedLocalCachedRemoteDiscoveryEntries() {
    String testDomain = "testDomain";
    String remoteDomain = "remoteDomain";
    Set<String> testDomains = new HashSet<>();
    testDomains.add(testDomain);
    testDomains.add(remoteDomain);
    String interfaceName = testProxy.INTERFACE_NAME;
    final Collection<DiscoveryEntry> localDiscoveryEntries = new HashSet<>();
    final Collection<DiscoveryEntry> cachedDiscoveryEntries = new HashSet<>();
    final List<GlobalDiscoveryEntry> remoteDiscoveryEntries = new ArrayList<>();
    DiscoveryEntry discoveryEntry = new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantIdLocal", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId");
    DiscoveryEntry cachedDiscoveryEntry = new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantIdCached", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId");
    GlobalDiscoveryEntry remoteDiscoveryEntry = CapabilityUtils.discoveryEntry2GlobalDiscoveryEntry(new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), remoteDomain, interfaceName, "participantIdRemote", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId"), new MqttAddress());
    localDiscoveryEntries.add(discoveryEntry);
    cachedDiscoveryEntries.add(cachedDiscoveryEntry);
    remoteDiscoveryEntries.add(remoteDiscoveryEntry);
    Set<DiscoveryEntryWithMetaInfo> discoveryEntriesWithMetaInfo = CapabilityUtils.convertToDiscoveryEntryWithMetaInfoSet(true, localDiscoveryEntries);
    discoveryEntriesWithMetaInfo.add(CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, cachedDiscoveryEntry));
    discoveryEntriesWithMetaInfo.add(CapabilityUtils.convertToDiscoveryEntryWithMetaInfo(false, remoteDiscoveryEntry));
    when(localDiscoveryEntryStoreMock.lookup(any(String[].class), eq(interfaceName))).thenReturn(localDiscoveryEntries);
    when(globalDiscoveryEntryCacheMock.lookup(any(String[].class), eq(interfaceName), anyLong())).thenReturn(cachedDiscoveryEntries);
    Mockito.doAnswer(new Answer<Object>() {

        @SuppressWarnings("rawtypes")
        @Override
        public List<GlobalDiscoveryEntry> answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            assert (arguments[0] instanceof Callback);
            ((Callback) arguments[0]).resolve((Object) remoteDiscoveryEntries);
            return null;
        }
    }).when(globalCapabilitiesDirectoryClientMock).lookup(any(Callback.class), any(String[].class), eq(interfaceName), anyLong());
    ProxyBuilder<testProxy> proxyBuilder = runtime.getProxyBuilder(testDomains, testProxy.class);
    final Future<Void> future = new Future<Void>();
    ArbitrationStrategyFunction arbitrationStrategyFunction = new ArbitrationStrategyFunction() {

        @Override
        public Set<DiscoveryEntryWithMetaInfo> select(Map<String, String> parameters, Collection<DiscoveryEntryWithMetaInfo> capabilities) {
            return Sets.newHashSet(capabilities);
        }
    };
    DiscoveryQos discoveryQos = new DiscoveryQos(30000, arbitrationStrategyFunction, 0, DiscoveryScope.LOCAL_AND_GLOBAL);
    proxyBuilder.setDiscoveryQos(discoveryQos).build(new ProxyCreatedCallback<testProxy>() {

        @Override
        public void onProxyCreationFinished(testProxy result) {
            future.onSuccess(null);
        }

        @Override
        public void onProxyCreationError(JoynrRuntimeException error) {
            future.onFailure(error);
        }
    });
    try {
        future.get(5000);
        verify(joynrMessagingConnectorFactoryMock).create(anyString(), eq(discoveryEntriesWithMetaInfo), any(MessagingQos.class));
    } catch (Exception e) {
        Assert.fail("Unexpected exception from ProxyCreatedCallback: " + e);
    }
}
Also used : GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MqttAddress(joynr.system.RoutingTypes.MqttAddress) DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) joynr.tests.testProxy(joynr.tests.testProxy) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Callback(io.joynr.proxy.Callback) ProxyCreatedCallback(io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(io.joynr.proxy.Future) Collection(java.util.Collection) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) ArbitrationStrategyFunction(io.joynr.arbitration.ArbitrationStrategyFunction) Map(java.util.Map) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 7 with Callback

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

the class IltConsumerAsyncMethodTest method callMethodWithMultipleByteBufferParametersAsync.

@Test
public void callMethodWithMultipleByteBufferParametersAsync() {
    LOG.info(name.getMethodName());
    final Semaphore resultAvailable = new Semaphore(0);
    try {
        // setup input parameter
        final Byte[] byteBufferArg1 = { -5, 125 };
        final Byte[] byteBufferArg2 = { 78, 0 };
        Callback<Byte[]> callback = new Callback<Byte[]>() {

            @Override
            public void onSuccess(Byte[] byteBufferOut) {
                // check result
                if (!java.util.Objects.deepEquals(byteBufferOut, (Byte[]) ArrayUtils.addAll(byteBufferArg1, byteBufferArg2))) {
                    LOG.info(name.getMethodName() + " - invalid byteBufferOut from callback");
                    LOG.info(name.getMethodName() + " - FAILED");
                    methodWithMultipleByteBufferParametersAsyncCallbackResult = false;
                    resultAvailable.release();
                    return;
                }
                methodWithMultipleByteBufferParametersAsyncCallbackResult = true;
                resultAvailable.release();
            }

            @Override
            public void onFailure(JoynrRuntimeException error) {
                methodWithMultipleByteBufferParametersAsyncCallbackResult = false;
                if (error instanceof JoynrRuntimeException) {
                    LOG.info(name.getMethodName() + " - callback - caught exception " + ((JoynrRuntimeException) error).getMessage());
                } else {
                    LOG.info(name.getMethodName() + " - callback - caught exception");
                }
                LOG.info(name.getMethodName() + " - FAILED");
                resultAvailable.release();
            }
        };
        testInterfaceProxy.methodWithMultipleByteBufferParameters(callback, byteBufferArg1, byteBufferArg2);
        try {
            // wait for callback
            LOG.info(name.getMethodName() + " - about to wait for callback");
            Assert.assertTrue(name.getMethodName() + " - FAILED - callback not received in time", resultAvailable.tryAcquire(10, TimeUnit.SECONDS));
            // check result from callback
            LOG.info(name.getMethodName() + " - wait for callback is over");
            Assert.assertTrue(name.getMethodName() + " - FAILED - callback reported error", methodWithMultipleByteBufferParametersAsyncCallbackResult);
        } catch (InterruptedException | JoynrRuntimeException e) {
            fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
        }
    } catch (Exception e) {
        fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
    }
    LOG.info(name.getMethodName() + " - OK");
}
Also used : MethodWithMultipleStructParametersCallback(joynr.interlanguagetest.TestInterfaceAsync.MethodWithMultipleStructParametersCallback) Callback(io.joynr.proxy.Callback) Semaphore(java.util.concurrent.Semaphore) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) ApplicationException(joynr.exceptions.ApplicationException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Test(org.junit.Test)

Example 8 with Callback

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

the class CapabilitiesRegistrarImpl method registerProvider.

/*
     * (non-Javadoc)
     *
     * @see io.joynr.capabilities.CapabilitiesRegistrar# registerProvider(java.lang.String,
     * io.joynr.provider.JoynrProvider, java.lang.Class)
     */
@Override
public Future<Void> registerProvider(final String domain, Object provider, ProviderQos providerQos) {
    if (providerQos == null) {
        throw new JoynrRuntimeException("providerQos == null. It must not be null");
    }
    ProviderContainer providerContainer = providerContainerFactory.create(provider);
    String participantId = participantIdStorage.getProviderParticipantId(domain, providerContainer.getInterfaceName());
    String defaultPublicKeyId = "";
    DiscoveryEntry discoveryEntry = new DiscoveryEntry(getVersionFromAnnotation(provider.getClass()), domain, providerContainer.getInterfaceName(), participantId, providerQos, System.currentTimeMillis(), System.currentTimeMillis() + defaultExpiryTimeMs, defaultPublicKeyId);
    final boolean isGloballyVisible = (discoveryEntry.getQos().getScope() == ProviderScope.GLOBAL);
    messageRouter.addNextHop(participantId, libjoynrMessagingAddress, isGloballyVisible);
    providerDirectory.add(participantId, providerContainer);
    Callback<Void> callback = new Callback<Void>() {

        @Override
        public void onSuccess(@CheckForNull Void result) {
        }

        @Override
        public void onFailure(JoynrRuntimeException runtimeException) {
            logger.error("Unexpected Error while registering Provider:", runtimeException);
        }
    };
    return localDiscoveryAggregator.add(callback, discoveryEntry);
}
Also used : DiscoveryEntry(joynr.types.DiscoveryEntry) Callback(io.joynr.proxy.Callback) CheckForNull(javax.annotation.CheckForNull) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) ProviderContainer(io.joynr.provider.ProviderContainer)

Example 9 with Callback

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

the class IltConsumerAsyncMethodTest method callMethodWithSingleByteBufferParameterAsync.

@Test
public void callMethodWithSingleByteBufferParameterAsync() {
    LOG.info(name.getMethodName());
    final Semaphore resultAvailable = new Semaphore(0);
    try {
        // setup input parameter
        final Byte[] byteBufferArg = { -128, 0, 127 };
        Callback<Byte[]> callback = new Callback<Byte[]>() {

            @Override
            public void onSuccess(Byte[] byteBufferOut) {
                // check result
                if (!java.util.Objects.deepEquals(byteBufferOut, byteBufferArg)) {
                    LOG.info(name.getMethodName() + " - invalid byteBufferOut from callback");
                    LOG.info(name.getMethodName() + " - FAILED");
                    methodWithSingleByteBufferParameterAsyncCallbackResult = false;
                    resultAvailable.release();
                    return;
                }
                methodWithSingleByteBufferParameterAsyncCallbackResult = true;
                resultAvailable.release();
            }

            @Override
            public void onFailure(JoynrRuntimeException error) {
                methodWithSingleByteBufferParameterAsyncCallbackResult = false;
                if (error instanceof JoynrRuntimeException) {
                    LOG.info(name.getMethodName() + " - callback - caught exception " + ((JoynrRuntimeException) error).getMessage());
                } else {
                    LOG.info(name.getMethodName() + " - callback - caught exception");
                }
                LOG.info(name.getMethodName() + " - FAILED");
                resultAvailable.release();
            }
        };
        testInterfaceProxy.methodWithSingleByteBufferParameter(callback, byteBufferArg);
        try {
            // wait for callback
            LOG.info(name.getMethodName() + " - about to wait for callback");
            Assert.assertTrue(name.getMethodName() + " - FAILED - callback not received in time", resultAvailable.tryAcquire(10, TimeUnit.SECONDS));
            // check result from callback
            LOG.info(name.getMethodName() + " - wait for callback is over");
            Assert.assertTrue(name.getMethodName() + " - FAILED - callback reported error", methodWithSingleByteBufferParameterAsyncCallbackResult);
        } catch (InterruptedException | JoynrRuntimeException e) {
            fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
        }
    } catch (Exception e) {
        fail(name.getMethodName() + " - FAILED - caught unexpected exception: " + e.getMessage());
    }
    LOG.info(name.getMethodName() + " - OK");
}
Also used : MethodWithMultipleStructParametersCallback(joynr.interlanguagetest.TestInterfaceAsync.MethodWithMultipleStructParametersCallback) Callback(io.joynr.proxy.Callback) Semaphore(java.util.concurrent.Semaphore) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) ApplicationException(joynr.exceptions.ApplicationException) ProviderRuntimeException(joynr.exceptions.ProviderRuntimeException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Test(org.junit.Test)

Example 10 with Callback

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

the class LocalDiscoveryTest method testRemoteGlobalDiscoveryEntries.

@SuppressWarnings("unchecked")
@Test
public void testRemoteGlobalDiscoveryEntries() {
    String testDomain = "testDomain";
    String interfaceName = testProxy.INTERFACE_NAME;
    final Collection<DiscoveryEntry> discoveryEntries = new HashSet<>();
    final List<GlobalDiscoveryEntry> globalDiscoveryEntries = new ArrayList<>();
    DiscoveryEntry discoveryEntry = new DiscoveryEntry(VersionUtil.getVersionFromAnnotation(testProxy.class), testDomain, interfaceName, "participantId", new ProviderQos(), System.currentTimeMillis(), System.currentTimeMillis() + 100000, "publicKeyId");
    discoveryEntries.add(discoveryEntry);
    globalDiscoveryEntries.add(CapabilityUtils.discoveryEntry2GlobalDiscoveryEntry(discoveryEntry, new MqttAddress()));
    when(globalDiscoveryEntryCacheMock.lookup(any(String[].class), eq(interfaceName), anyLong())).thenReturn(new HashSet<DiscoveryEntry>());
    Mockito.doAnswer(new Answer<Object>() {

        @SuppressWarnings("rawtypes")
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            assert (arguments[0] instanceof Callback);
            ((Callback) arguments[0]).resolve((Object) globalDiscoveryEntries);
            return null;
        }
    }).when(globalCapabilitiesDirectoryClientMock).lookup(any(Callback.class), any(String[].class), eq(interfaceName), anyLong());
    ProxyBuilder<testProxy> proxyBuilder = runtime.getProxyBuilder(testDomain, testProxy.class);
    final Future<Void> future = new Future<Void>();
    DiscoveryQos discoveryQos = new DiscoveryQos();
    discoveryQos.setDiscoveryScope(DiscoveryScope.GLOBAL_ONLY);
    proxyBuilder.setDiscoveryQos(discoveryQos).build(new ProxyCreatedCallback<testProxy>() {

        @Override
        public void onProxyCreationFinished(testProxy result) {
            future.onSuccess(null);
        }

        @Override
        public void onProxyCreationError(JoynrRuntimeException error) {
            future.onFailure(error);
        }
    });
    try {
        future.get(5000);
        verify(joynrMessagingConnectorFactoryMock).create(anyString(), discoveryEntryWithMetaInfoArgumentCaptor.capture(), any(MessagingQos.class));
        assertDiscoveryEntryEqualsCaptured(discoveryEntry);
    } catch (Exception e) {
        Assert.fail("Unexpected exception from ProxyCreatedCallback: " + e);
    }
}
Also used : GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) MessagingQos(io.joynr.messaging.MessagingQos) HashSet(java.util.HashSet) MqttAddress(joynr.system.RoutingTypes.MqttAddress) DiscoveryEntry(joynr.types.DiscoveryEntry) GlobalDiscoveryEntry(joynr.types.GlobalDiscoveryEntry) joynr.tests.testProxy(joynr.tests.testProxy) DiscoveryQos(io.joynr.arbitration.DiscoveryQos) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Callback(io.joynr.proxy.Callback) ProxyCreatedCallback(io.joynr.proxy.ProxyBuilder.ProxyCreatedCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(io.joynr.proxy.Future) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

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