use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceCallTest method test_local_async_greeting_return_Message.
@Test
public void test_local_async_greeting_return_Message() throws InterruptedException, ExecutionException {
// Create microservices cluster.
Microservices microservices = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
ServiceCall service = microservices.dispatcher().timeout(Duration.ofSeconds(1)).create();
// call the service.
CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingMessage").data(Message.builder().data("joe").build()).build(), Duration.ofMillis(1));
CountDownLatch timeLatch = new CountDownLatch(1);
future.whenComplete((result, ex) -> {
if (ex == null) {
assertTrue(result.data().equals(" hello to: joe"));
// print the greeting.
System.out.println("9. local_async_greeting_return_Message :" + result.data());
} else {
// print the greeting.
System.out.println(ex);
}
timeLatch.countDown();
});
await(timeLatch, 1, TimeUnit.SECONDS);
microservices.shutdown().get();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceCallTest method test_dispatcher_local_greeting_request_completes_before_timeout.
@Test
public void test_dispatcher_local_greeting_request_completes_before_timeout() throws InterruptedException, ExecutionException {
Microservices gateway = Microservices.builder().services(new GreetingServiceImpl()).build();
ServiceCall service = gateway.dispatcher().timeout(Duration.ofSeconds(3)).create();
CompletableFuture<Message> result = service.invoke(Messages.builder().request("io.scalecube.services.GreetingService", "greetingRequest").data(new GreetingRequest("joe")).build());
CountDownLatch timeLatch = new CountDownLatch(1);
result.whenComplete((success, error) -> {
if (error == null) {
System.out.println(success);
GreetingResponse greetings = success.data();
// print the greeting.
System.out.println("1. greeting_request_completes_before_timeout : " + greetings.getResult());
assertTrue(greetings.getResult().equals(" hello to: joe"));
timeLatch.countDown();
} else {
System.out.println("1. FAILED! - greeting_request_completes_before_timeout reached timeout: " + error);
assertTrue(error.toString(), false);
timeLatch.countDown();
}
});
await(timeLatch, 10, TimeUnit.SECONDS);
assertTrue(timeLatch.getCount() == 0);
gateway.shutdown().get();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceCallTest method test_remote_greeting_request_timeout_expires.
@Test
public void test_remote_greeting_request_timeout_expires() throws InterruptedException, ExecutionException {
// Create microservices cluster.
Microservices provider = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
// Create microservices cluster.
Microservices consumer = Microservices.builder().port(port.incrementAndGet()).seeds(provider.cluster().address()).build();
ServiceCall service = consumer.dispatcher().timeout(Duration.ofSeconds(1)).create();
// call the service.
CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingRequestTimeout").data(new GreetingRequest("joe", Duration.ofSeconds(4))).build());
CountDownLatch timeLatch = new CountDownLatch(1);
future.whenComplete((success, error) -> {
if (error != null) {
// print the greeting.
System.out.println("8. remote_greeting_request_timeout_expires : " + error);
assertTrue(error instanceof TimeoutException);
timeLatch.countDown();
}
});
try {
await(timeLatch, 10, TimeUnit.SECONDS);
} catch (Exception ex) {
fail();
}
provider.shutdown().get();
consumer.shutdown().get();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceCallTest method test_round_robin_selection_logic.
@Test
public void test_round_robin_selection_logic() throws InterruptedException, ExecutionException {
Microservices gateway = createSeed();
// Create microservices instance cluster.
Microservices provider1 = Microservices.builder().seeds(gateway.cluster().address()).port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
// Create microservices instance cluster.
Microservices provider2 = Microservices.builder().seeds(gateway.cluster().address()).port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
ServiceCall service = gateway.dispatcher().create();
// call the service.
CompletableFuture<Message> result1 = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingMessage").data(Message.builder().data("joe").build()).build());
CompletableFuture<Message> result2 = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingMessage").data(Message.builder().data("joe").build()).build());
CompletableFuture<Void> combined = CompletableFuture.allOf(result1, result2);
CountDownLatch timeLatch = new CountDownLatch(1);
combined.whenComplete((v, x) -> {
try {
// print the greeting.
System.out.println("11. round_robin_selection_logic :" + result1.get());
System.out.println("11. round_robin_selection_logic :" + result2.get());
boolean success = !result1.get().sender().equals(result2.get().sender());
assertTrue(success);
} catch (Throwable e) {
assertTrue(false);
}
timeLatch.countDown();
});
await(timeLatch, 2, TimeUnit.SECONDS);
provider2.shutdown().get();
provider1.shutdown().get();
gateway.shutdown().get();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceCallTest method test_dispatcher_remote_greeting_request_completes_before_timeout.
@Test
public void test_dispatcher_remote_greeting_request_completes_before_timeout() throws InterruptedException, ExecutionException {
// Create microservices instance.
Microservices gateway = Microservices.builder().port(port.incrementAndGet()).build();
Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new GreetingServiceImpl()).build();
ServiceCall service = gateway.dispatcher().timeout(Duration.ofSeconds(3)).create();
CompletableFuture<Message> result = service.invoke(Messages.builder().request("io.scalecube.services.GreetingService", "greetingRequest").data(new GreetingRequest("joe")).build());
CountDownLatch timeLatch = new CountDownLatch(1);
result.whenComplete((success, error) -> {
if (error == null) {
System.out.println(success);
GreetingResponse greetings = success.data();
// print the greeting.
System.out.println("1. greeting_request_completes_before_timeout : " + greetings.getResult());
assertTrue(greetings.getResult().equals(" hello to: joe"));
timeLatch.countDown();
} else {
System.out.println("1. FAILED! - greeting_request_completes_before_timeout reached timeout: " + error);
assertTrue(error.toString(), false);
timeLatch.countDown();
}
});
await(timeLatch, 10, TimeUnit.SECONDS);
assertTrue(timeLatch.getCount() == 0);
gateway.shutdown().get();
node.shutdown().get();
}
Aggregations