use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class LookupServiceTests method testLookup.
@Test
void testLookup() throws IOException, InterruptedException {
final LookupService service = getLookupService(new LocalLookupServiceImpl());
service.start();
RoboContextDescriptor descriptor = createRoboContextDescriptor();
ContextEmitter emitter = new ContextEmitter(descriptor, InetAddress.getByName(LookupServiceProvider.DEFAULT_MULTICAST_ADDRESS), LookupServiceProvider.DEFAULT_PORT, 250);
for (int i = 0; i < 10; i++) {
emitter.emit();
Thread.sleep(250);
}
Map<String, RoboContextDescriptor> discoveredContexts = service.getDiscoveredContexts();
System.out.println(discoveredContexts);
assertEquals(1, discoveredContexts.size());
RoboContext context = service.getContext(descriptor.getId());
assertNotNull(context);
ClientRemoteRoboContext remoteContext = (ClientRemoteRoboContext) context;
assertNotNull(remoteContext.getAddress());
System.out.println("Address: " + remoteContext.getAddress());
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RemoteContextTests method discoveryOfDiscoveryEnabledRoboContextTest.
@Test
void discoveryOfDiscoveryEnabledRoboContextTest() throws RoboBuilderException, IOException {
RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testDiscoverableSystem.xml"));
RoboContext ctx = builder.build();
ctx.start();
final LookupService service = LookupServiceTests.getLookupService(new LocalLookupServiceImpl());
service.start();
for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("6") == null); i++) {
SystemUtil.sleep(200);
}
assertTrue(service.getDiscoveredContexts().size() > 0);
RoboContextDescriptor descriptor = service.getDescriptor("6");
assertEquals(descriptor.getMetadata().get("name"), "Caprica");
assertEquals(descriptor.getMetadata().get("class"), "Cylon");
ctx.shutdown();
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RemoteContextTests method buildReceiverSystemStringConsumer.
/*
* Builds a system ready to receive strings.
*/
private RoboContext buildReceiverSystemStringConsumer() throws RoboBuilderException, ConfigurationException {
RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteReceiver.xml"));
Configuration configuration = new ConfigurationBuilder().addInteger("totalNumberMessages", 1).build();
StringConsumer stringConsumer = new StringConsumer(builder.getContext(), UNIT_STRING_CONSUMER);
stringConsumer.initialize(configuration);
builder.add(stringConsumer);
RoboContext receiverSystem = builder.build();
receiverSystem.start();
return receiverSystem;
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class RemoteStringProducer method sendRandomMessage.
public void sendRandomMessage() {
final String message = StringToolkit.getRandomMessage(10);
RoboContext ctx = LookupServiceProvider.getDefaultLookupService().getContext(targetContext);
ctx.getReference(target).sendMessage(message);
}
use of com.robo4j.RoboContext in project robo4j by Robo4J.
the class CounterUnitTests method test.
@Test
void test() throws RoboBuilderException, InterruptedException, ExecutionException {
// FIXME(Marcus/Aug 20, 2017): We really should get rid of the sleeps
// here and use waits with timeouts...
RoboBuilder builder = new RoboBuilder();
builder.add(IntegerConsumer.class, ID_CONSUMER);
builder.add(CounterUnit.class, getCounterConfiguration(ID_CONSUMER, 1000), ID_COUNTER);
RoboContext context = builder.build();
context.start();
assertEquals(LifecycleState.STARTED, context.getState());
RoboReference<CounterCommand> counter = context.getReference(ID_COUNTER);
RoboReference<Integer> consumer = context.getReference(ID_CONSUMER);
counter.sendMessage(CounterCommand.START);
Thread.sleep(2500);
assertTrue(consumer.getAttribute(NUMBER_OF_MESSAGES).get() > 2);
counter.sendMessage(CounterCommand.STOP);
Thread.sleep(200);
Integer count = consumer.getAttribute(NUMBER_OF_MESSAGES).get();
Thread.sleep(2500);
assertEquals(count, consumer.getAttribute(NUMBER_OF_MESSAGES).get());
ArrayList<Integer> messages = consumer.getAttribute(MESSAGES).get();
assertNotEquals(0, messages.size());
assertNotEquals(0, (int) messages.get(messages.size() - 1));
counter.sendMessage(CounterCommand.RESET);
Thread.sleep(1000);
assertEquals(0, (int) counter.getAttribute(COUNTER).get());
}
Aggregations