Search in sources :

Example 36 with DiscoveryEntryWithMetaInfo

use of joynr.types.DiscoveryEntryWithMetaInfo in project joynr by bmwcarit.

the class ArbitrationTest method testLastSeenArbitrator.

@Test
public void testLastSeenArbitrator() throws InterruptedException {
    ProviderQos providerQos = new ProviderQos();
    capabilitiesList.add(new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, "wrongParticipantId", providerQos, 222L, NO_EXPIRY, publicKeyId, true));
    DiscoveryEntryWithMetaInfo expectedDiscoveryEntry = new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, expectedParticipantId, providerQos, 333L, NO_EXPIRY, publicKeyId, true);
    capabilitiesList.add(expectedDiscoveryEntry);
    capabilitiesList.add(new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, "thirdParticipantId", providerQos, 111L, NO_EXPIRY, publicKeyId, true));
    discoveryQos = new DiscoveryQos(ARBITRATION_TIMEOUT, ArbitrationStrategy.LastSeen, Long.MAX_VALUE);
    try {
        Arbitrator arbitrator = ArbitratorFactory.create(Sets.newHashSet(domain), interfaceName, interfaceVersion, discoveryQos, localDiscoveryAggregator);
        arbitrator.setArbitrationListener(arbitrationCallback);
        arbitrator.scheduleArbitration();
        assertTrue(localDiscoveryAggregatorSemaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
        ArbitrationResult expectedArbitrationResult = new ArbitrationResult(expectedDiscoveryEntry);
        verify(arbitrationCallback, times(1)).onSuccess(eq(expectedArbitrationResult));
    } catch (DiscoveryException e) {
        fail("A Joyn Arbitration Exception has been thrown");
    }
}
Also used : Version(joynr.types.Version) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) DiscoveryException(io.joynr.exceptions.DiscoveryException) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 37 with DiscoveryEntryWithMetaInfo

use of joynr.types.DiscoveryEntryWithMetaInfo in project joynr by bmwcarit.

the class ArbitrationTest method testIncompatibleVersionsReported.

@Test
public void testIncompatibleVersionsReported() throws InterruptedException {
    Version incompatibleVersion = new Version(100, 100);
    final Collection<DiscoveryEntryWithMetaInfo> discoveryEntries = Lists.newArrayList(new DiscoveryEntryWithMetaInfo(incompatibleVersion, domain, interfaceName, "first-participant", new ProviderQos(), System.currentTimeMillis(), NO_EXPIRY, "public-key-1", true));
    ArbitrationStrategyFunction arbitrationStrategyFunction = mock(ArbitrationStrategyFunction.class);
    when(arbitrationStrategyFunction.select(Mockito.<Map<String, String>>any(), Mockito.<Collection<DiscoveryEntryWithMetaInfo>>any())).thenReturn(new HashSet<DiscoveryEntryWithMetaInfo>());
    doAnswer(new Answer<Set<DiscoveryEntryWithMetaInfo>>() {

        @SuppressWarnings("unchecked")
        @Override
        public Set<DiscoveryEntryWithMetaInfo> answer(InvocationOnMock invocation) throws Throwable {
            Map<String, Set<Version>> filteredVersions = (Map<String, Set<Version>>) invocation.getArguments()[2];
            Set<DiscoveryEntryWithMetaInfo> discoveryEntries = (Set<DiscoveryEntryWithMetaInfo>) invocation.getArguments()[1];
            filteredVersions.put(domain, Sets.newHashSet(discoveryEntries.iterator().next().getProviderVersion()));
            discoveryEntries.clear();
            return new HashSet<>();
        }
    }).when(discoveryEntryVersionFilter).filter(Mockito.<Version>any(), Mockito.<Set<DiscoveryEntryWithMetaInfo>>any(), Mockito.<Map<String, Set<Version>>>any());
    DiscoveryQos discoveryQos = new DiscoveryQos(10L, arbitrationStrategyFunction, 0L);
    reset(localDiscoveryAggregator);
    doAnswer(new Answer<Object>() {

        @SuppressWarnings("unchecked")
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ((Callback<DiscoveryEntryWithMetaInfo[]>) invocation.getArguments()[0]).resolve((Object) discoveryEntries.toArray(new DiscoveryEntryWithMetaInfo[1]));
            localDiscoveryAggregatorSemaphore.release();
            return null;
        }
    }).when(localDiscoveryAggregator).lookup(Mockito.<Callback<DiscoveryEntryWithMetaInfo[]>>any(), eq(new String[] { domain }), eq(interfaceName), Mockito.<joynr.types.DiscoveryQos>any());
    Arbitrator arbitrator = ArbitratorFactory.create(Sets.newHashSet(domain), interfaceName, interfaceVersion, discoveryQos, localDiscoveryAggregator);
    arbitrator.setArbitrationListener(arbitrationCallback);
    arbitrator.scheduleArbitration();
    assertTrue(localDiscoveryAggregatorSemaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
    Set<Version> discoveredVersions = Sets.newHashSet(incompatibleVersion);
    ArgumentCaptor<NoCompatibleProviderFoundException> noCompatibleProviderFoundExceptionCaptor = ArgumentCaptor.forClass(NoCompatibleProviderFoundException.class);
    verify(arbitrationCallback).onError(noCompatibleProviderFoundExceptionCaptor.capture());
    assertEquals(discoveredVersions, noCompatibleProviderFoundExceptionCaptor.getValue().getDiscoveredVersions());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) NoCompatibleProviderFoundException(io.joynr.exceptions.NoCompatibleProviderFoundException) MultiDomainNoCompatibleProviderFoundException(io.joynr.exceptions.MultiDomainNoCompatibleProviderFoundException) Version(joynr.types.Version) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) Map(java.util.Map) HashMap(java.util.HashMap) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 38 with DiscoveryEntryWithMetaInfo

use of joynr.types.DiscoveryEntryWithMetaInfo in project joynr by bmwcarit.

the class ArbitrationTest method testPriorityArbitratorWithOnlyNegativePriorities.

@Test
public void testPriorityArbitratorWithOnlyNegativePriorities() throws InterruptedException {
    ProviderQos providerQos = new ProviderQos();
    providerQos.setPriority(Long.MIN_VALUE);
    expectedEndpointAddress = new ChannelAddress("http://testUrl", "testChannelId");
    ArrayList<Address> expectedEndpointAddresses = new ArrayList<Address>();
    expectedEndpointAddresses.add(expectedEndpointAddress);
    capabilitiesList.add(new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, expectedParticipantId, providerQos, System.currentTimeMillis(), NO_EXPIRY, publicKeyId, true));
    ProviderQos providerQos2 = new ProviderQos();
    providerQos2.setPriority(Long.MIN_VALUE);
    capabilitiesList.add(new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, "wrongParticipantId", providerQos2, System.currentTimeMillis(), NO_EXPIRY, publicKeyId, true));
    long negativePriority = Long.MIN_VALUE;
    ProviderQos providerQos3 = new ProviderQos();
    providerQos3.setPriority(negativePriority);
    capabilitiesList.add(new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, "thirdParticipantId", providerQos3, System.currentTimeMillis(), NO_EXPIRY, publicKeyId, true));
    discoveryQos = new DiscoveryQos(ARBITRATION_TIMEOUT, ArbitrationStrategy.HighestPriority, Long.MAX_VALUE);
    try {
        Arbitrator arbitrator = ArbitratorFactory.create(Sets.newHashSet(domain), interfaceName, interfaceVersion, discoveryQos, localDiscoveryAggregator);
        arbitrator.setArbitrationListener(arbitrationCallback);
        arbitrator.scheduleArbitration();
        assertTrue(localDiscoveryAggregatorSemaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
        verify(arbitrationCallback, times(1)).onError(any(Throwable.class));
        verify(arbitrationCallback, never()).onSuccess(any(ArbitrationResult.class));
    } catch (DiscoveryException e) {
        fail("A Joyn Arbitration Exception has been thrown");
    }
}
Also used : Address(joynr.system.RoutingTypes.Address) ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) ArrayList(java.util.ArrayList) ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) Version(joynr.types.Version) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) DiscoveryException(io.joynr.exceptions.DiscoveryException) ProviderQos(joynr.types.ProviderQos) Test(org.junit.Test)

Example 39 with DiscoveryEntryWithMetaInfo

use of joynr.types.DiscoveryEntryWithMetaInfo 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 40 with DiscoveryEntryWithMetaInfo

use of joynr.types.DiscoveryEntryWithMetaInfo in project joynr by bmwcarit.

the class ArbitrationTest method testCustomArbitrationFunctionMultipleMatches.

@SuppressWarnings("unchecked")
@Test
public void testCustomArbitrationFunctionMultipleMatches() throws InterruptedException {
    // Expected provider supports onChangeSubscriptions
    ProviderQos providerQos = new ProviderQos();
    String publicKeyId = "publicKeyId";
    expectedEndpointAddress = new ChannelAddress("http://testUrl", "testChannelId");
    String[] participantIds = new String[] { "first-participant", "second-participant" };
    for (int index = 0; index < participantIds.length; index++) {
        DiscoveryEntryWithMetaInfo discoveryEntry = new DiscoveryEntryWithMetaInfo(new Version(47, 11), domain, TestInterface.INTERFACE_NAME, participantIds[index], providerQos, System.currentTimeMillis(), NO_EXPIRY, publicKeyId, true);
        capabilitiesList.add(discoveryEntry);
    }
    ArbitrationStrategyFunction arbitrationStrategyFunction = mock(ArbitrationStrategyFunction.class);
    when(arbitrationStrategyFunction.select(any(Map.class), any(Collection.class))).thenReturn(new HashSet<DiscoveryEntryWithMetaInfo>(capabilitiesList));
    discoveryQos = new DiscoveryQos(ARBITRATION_TIMEOUT, arbitrationStrategyFunction, Long.MAX_VALUE);
    Arbitrator arbitrator = ArbitratorFactory.create(Sets.newHashSet(domain), interfaceName, interfaceVersion, discoveryQos, localDiscoveryAggregator);
    arbitrator.setArbitrationListener(arbitrationCallback);
    arbitrator.scheduleArbitration();
    assertTrue(localDiscoveryAggregatorSemaphore.tryAcquire(1000, TimeUnit.MILLISECONDS));
    verify(arbitrationStrategyFunction, times(1)).select(eq(discoveryQos.getCustomParameters()), eq(new HashSet<DiscoveryEntryWithMetaInfo>(capabilitiesList)));
    DiscoveryEntryWithMetaInfo[] expectedDiscoveryEntries = new DiscoveryEntryWithMetaInfo[capabilitiesList.size()];
    ArbitrationResult expectedArbitrationResult = new ArbitrationResult(capabilitiesList.toArray(expectedDiscoveryEntries));
    verify(arbitrationCallback, times(1)).onSuccess(eq(expectedArbitrationResult));
}
Also used : ChannelAddress(joynr.system.RoutingTypes.ChannelAddress) Version(joynr.types.Version) Collection(java.util.Collection) DiscoveryEntryWithMetaInfo(joynr.types.DiscoveryEntryWithMetaInfo) Map(java.util.Map) HashMap(java.util.HashMap) ProviderQos(joynr.types.ProviderQos) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DiscoveryEntryWithMetaInfo (joynr.types.DiscoveryEntryWithMetaInfo)66 Test (org.junit.Test)32 Version (joynr.types.Version)29 ProviderQos (joynr.types.ProviderQos)24 DiscoveryEntry (joynr.types.DiscoveryEntry)16 HashSet (java.util.HashSet)14 GlobalDiscoveryEntry (joynr.types.GlobalDiscoveryEntry)13 DiscoveryQos (io.joynr.arbitration.DiscoveryQos)11 Matchers.anyString (org.mockito.Matchers.anyString)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 ChannelAddress (joynr.system.RoutingTypes.ChannelAddress)9 Before (org.junit.Before)9 DiscoveryException (io.joynr.exceptions.DiscoveryException)7 MessagingQos (io.joynr.messaging.MessagingQos)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Set (java.util.Set)7 Callback (io.joynr.proxy.Callback)6 JoynrRuntimeException (io.joynr.exceptions.JoynrRuntimeException)5 Collection (java.util.Collection)5