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));
}
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));
}
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);
}
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;
}
};
}
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);
}
}
}
}
Aggregations