Search in sources :

Example 1 with ArbitrationCallback

use of io.joynr.arbitration.ArbitrationCallback in project joynr by bmwcarit.

the class ProxyBuilderDefaultImpl method createProxyInvocationHandler.

// Method called by both synchronous and asynchronous build() to create a ProxyInvocationHandler
private ProxyInvocationHandler createProxyInvocationHandler(final ProxyCreatedCallback<T> callback) {
    if (buildCalled) {
        throw new JoynrIllegalStateException("Proxy builder was already used to build a proxy. Please create a new proxy builder for each proxy.");
    }
    buildCalled = true;
    final ProxyInvocationHandler proxyInvocationHandler = proxyInvocationHandlerFactory.create(domains, interfaceName, proxyParticipantId, discoveryQos, messagingQos);
    // This order is necessary because the Arbitrator might return early
    // But if the listener is set after the ProxyInvocationHandler the
    // Arbitrator cannot return early
    arbitrator.setArbitrationListener(new ArbitrationCallback() {

        @Override
        public void onSuccess(ArbitrationResult arbitrationResult) {
            logger.debug("DISCOVERY proxy created for:{}", arbitrationResult.getDiscoveryEntries());
            proxyInvocationHandler.createConnector(arbitrationResult);
            callback.onProxyCreationFinished(proxy);
        }

        @Override
        public void onError(Throwable throwable) {
            JoynrRuntimeException reason;
            if (throwable instanceof JoynrRuntimeException) {
                reason = (JoynrRuntimeException) throwable;
            } else {
                reason = new JoynrRuntimeException(throwable);
            }
            proxyInvocationHandler.abort(reason);
            callback.onProxyCreationError(reason);
        }
    });
    return proxyInvocationHandler;
}
Also used : ArbitrationResult(io.joynr.arbitration.ArbitrationResult) ArbitrationCallback(io.joynr.arbitration.ArbitrationCallback) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) JoynrIllegalStateException(io.joynr.exceptions.JoynrIllegalStateException)

Example 2 with ArbitrationCallback

use of io.joynr.arbitration.ArbitrationCallback in project joynr by bmwcarit.

the class ProxyBuilderDefaultImplTest method testMultiDomainNoCompatibleProviderFoundSetOnInvocationHandler.

@Test
public void testMultiDomainNoCompatibleProviderFoundSetOnInvocationHandler() throws Exception {
    final Set<String> domains = Sets.newHashSet("domain-1", "domain-2");
    setup(domains);
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            executor.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Thread.sleep(10L);
                    verify(arbitrator).setArbitrationListener(arbitrationCallbackCaptor.capture());
                    ArbitrationCallback callback = arbitrationCallbackCaptor.getValue();
                    Map<String, Set<Version>> versionsByDomain = new HashMap<>();
                    HashSet<Version> discoveredVersions = Sets.newHashSet(new Version(100, 100));
                    Map<String, NoCompatibleProviderFoundException> exceptionsByDomain = Maps.newHashMap();
                    for (String domain : domains) {
                        versionsByDomain.put(domain, discoveredVersions);
                        exceptionsByDomain.put(domain, new NoCompatibleProviderFoundException("interfaceName", new Version(1, 1), domain, discoveredVersions));
                    }
                    callback.onError(new MultiDomainNoCompatibleProviderFoundException(exceptionsByDomain));
                    return null;
                }
            });
            return null;
        }
    }).when(arbitrator).scheduleArbitration();
    subject.build(proxyCreatedCallback);
    executor.shutdown();
    executor.awaitTermination(100L, TimeUnit.MILLISECONDS);
    verify(proxyCreatedCallback).onProxyCreationError(exceptionCaptor.capture());
    JoynrRuntimeException capturedException = exceptionCaptor.getValue();
    assertTrue(capturedException instanceof MultiDomainNoCompatibleProviderFoundException);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MultiDomainNoCompatibleProviderFoundException(io.joynr.exceptions.MultiDomainNoCompatibleProviderFoundException) NoCompatibleProviderFoundException(io.joynr.exceptions.NoCompatibleProviderFoundException) ArbitrationCallback(io.joynr.arbitration.ArbitrationCallback) MultiDomainNoCompatibleProviderFoundException(io.joynr.exceptions.MultiDomainNoCompatibleProviderFoundException) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Callable(java.util.concurrent.Callable) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 3 with ArbitrationCallback

use of io.joynr.arbitration.ArbitrationCallback in project joynr by bmwcarit.

the class ProxyBuilderDefaultImplTest method testNoCompatibleProviderPassedToOnError.

@Test
public void testNoCompatibleProviderPassedToOnError() throws Exception {
    final String domain = "domain1";
    final Set<String> domains = Sets.newHashSet(domain);
    setup(domains);
    final ExecutorService executor = Executors.newSingleThreadExecutor();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            executor.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    Thread.sleep(10L);
                    verify(arbitrator).setArbitrationListener(arbitrationCallbackCaptor.capture());
                    ArbitrationCallback callback = arbitrationCallbackCaptor.getValue();
                    Set<Version> discoveredVersions = Sets.newHashSet(new Version(100, 100));
                    callback.onError(new NoCompatibleProviderFoundException(TestInterface.INTERFACE_NAME, new Version(1, 1), domain, discoveredVersions));
                    return null;
                }
            });
            return null;
        }
    }).when(arbitrator).scheduleArbitration();
    subject.build(proxyCreatedCallback);
    executor.shutdown();
    executor.awaitTermination(100L, TimeUnit.MILLISECONDS);
    verify(proxyCreatedCallback).onProxyCreationError(exceptionCaptor.capture());
    JoynrRuntimeException capturedException = exceptionCaptor.getValue();
    assertTrue(capturedException instanceof NoCompatibleProviderFoundException);
}
Also used : MultiDomainNoCompatibleProviderFoundException(io.joynr.exceptions.MultiDomainNoCompatibleProviderFoundException) NoCompatibleProviderFoundException(io.joynr.exceptions.NoCompatibleProviderFoundException) ArbitrationCallback(io.joynr.arbitration.ArbitrationCallback) JoynrRuntimeException(io.joynr.exceptions.JoynrRuntimeException) Callable(java.util.concurrent.Callable) JoynrVersion(io.joynr.JoynrVersion) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

ArbitrationCallback (io.joynr.arbitration.ArbitrationCallback)3 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)3 JoynrVersion (io.joynr.JoynrVersion)2 MultiDomainNoCompatibleProviderFoundException (io.joynr.exceptions.MultiDomainNoCompatibleProviderFoundException)2 NoCompatibleProviderFoundException (io.joynr.exceptions.NoCompatibleProviderFoundException)2 Callable (java.util.concurrent.Callable)2 ExecutorService (java.util.concurrent.ExecutorService)2 Version (joynr.types.Version)2 Test (org.junit.Test)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 ArbitrationResult (io.joynr.arbitration.ArbitrationResult)1 JoynrIllegalStateException (io.joynr.exceptions.JoynrIllegalStateException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1