Search in sources :

Example 31 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 32 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 33 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 34 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 35 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Aggregations

Message (io.scalecube.transport.Message)51 BaseTest (io.scalecube.testlib.BaseTest)31 Test (org.junit.Test)31 CountDownLatch (java.util.concurrent.CountDownLatch)28 Observable (rx.Observable)10 Member (io.scalecube.cluster.Member)8 Microservices (io.scalecube.services.Microservices)6 TimeUnit (java.util.concurrent.TimeUnit)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 ServiceCall (io.scalecube.services.ServiceCall)5 Collections (java.util.Collections)5 Executors (java.util.concurrent.Executors)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Schedulers (rx.schedulers.Schedulers)5 List (java.util.List)4 Subscription (rx.Subscription)4 PublishSubject (rx.subjects.PublishSubject)4 Subject (rx.subjects.Subject)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3