Search in sources :

Example 36 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceCallTest method test_service_tags.

@Ignore("https://api.travis-ci.org/v3/job/346827972/log.txt")
@Test
public void test_service_tags() throws InterruptedException, ExecutionException {
    Microservices gateway = Microservices.builder().port(port.incrementAndGet()).build();
    Microservices services1 = Microservices.builder().port(port.incrementAndGet()).seeds(gateway.cluster().address()).services().service(new GreetingServiceImplA()).tag("Weight", "0.3").add().build().build();
    Microservices services2 = Microservices.builder().port(port.incrementAndGet()).seeds(gateway.cluster().address()).services().service(new GreetingServiceImplB()).tag("Weight", "0.7").add().build().build();
    System.out.println(gateway.cluster().members());
    sleep(1000);
    ServiceCall service = gateway.dispatcher().router(CanaryTestingRouter.class).create();
    AtomicInteger count = new AtomicInteger(0);
    AtomicInteger responses = new AtomicInteger(0);
    CountDownLatch timeLatch = new CountDownLatch(1);
    for (int i = 0; i < 100; i++) {
        // call the service.
        CompletableFuture<Message> future = service.invoke(Messages.builder().request(CanaryService.class, "greeting").data("joe").build());
        future.whenComplete((success, error) -> {
            responses.incrementAndGet();
            if (success.data().toString().startsWith("B")) {
                count.incrementAndGet();
                if ((responses.get() == 100) && (60 < count.get() && count.get() < 80)) {
                    timeLatch.countDown();
                }
            }
        });
    }
    await(timeLatch, 5, TimeUnit.SECONDS);
    System.out.println("responses: " + responses.get());
    System.out.println("count: " + count.get());
    System.out.println("Service B was called: " + count.get() + " times.");
    assertTrue((responses.get() == 100) && (60 < count.get() && count.get() < 80));
    services1.shutdown().get();
    services2.shutdown().get();
    gateway.shutdown().get();
}
Also used : Message(io.scalecube.transport.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CanaryTestingRouter(io.scalecube.services.a.b.testing.CanaryTestingRouter) CanaryService(io.scalecube.services.a.b.testing.CanaryService) CountDownLatch(java.util.concurrent.CountDownLatch) GreetingServiceImplA(io.scalecube.services.a.b.testing.GreetingServiceImplA) GreetingServiceImplB(io.scalecube.services.a.b.testing.GreetingServiceImplB) Ignore(org.junit.Ignore) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 37 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceCallTest method test_remote_void_greeting.

@Test
public void test_remote_void_greeting() throws InterruptedException, ExecutionException {
    // Create microservices instance.
    Microservices gateway = Microservices.builder().port(port.incrementAndGet()).build();
    Microservices node1 = Microservices.builder().seeds(gateway.cluster().address()).services(new GreetingServiceImpl()).build();
    ServiceCall service = gateway.dispatcher().create();
    // call the service.
    CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingVoid").data(new GreetingRequest("joe")).build());
    CountDownLatch timeLatch = new CountDownLatch(1);
    future.whenComplete((success, error) -> {
        if (error == null) {
            System.out.println("void return: " + success);
            assertTrue(success.data() == null);
            timeLatch.countDown();
        }
    });
    // send and forget so we have no way to know what happen
    // but at least we didn't get exception :)
    System.out.println("test_remote_void_greeting done.");
    await(timeLatch, 1, TimeUnit.SECONDS);
    gateway.shutdown().get();
    node1.shutdown().get();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 38 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceCallTest method test_remote_async_greeting_return_string.

@Test
public void test_remote_async_greeting_return_string() 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().create();
    // call the service.
    CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greeting").data("joe").build());
    CountDownLatch timeLatch = new CountDownLatch(1);
    future.whenComplete((result, ex) -> {
        if (ex == null) {
            // print the greeting.
            System.out.println("4. remote_async_greeting_return_string :" + result);
            assertTrue(result.data().equals(" hello to: joe"));
        } else {
            // print the greeting.
            System.out.println(ex);
        }
        timeLatch.countDown();
    });
    await(timeLatch, 1, TimeUnit.SECONDS);
    provider.shutdown().get();
    consumer.shutdown().get();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 39 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceCallTest method test_remote_async_greeting_no_params.

@Test
public void test_remote_async_greeting_no_params() 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().create();
    // call the service.
    CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingNoParams").build());
    CountDownLatch timeLatch = new CountDownLatch(1);
    future.whenComplete((message, ex) -> {
        if (ex == null) {
            System.out.println(message);
            String result = message.data();
            assertTrue(result.equals("hello unknown"));
            // print the greeting.
            System.out.println("test_local_async_no_params :" + result);
        } else {
            // print the greeting.
            System.out.println(ex);
        }
        timeLatch.countDown();
    });
    await(timeLatch, 1, TimeUnit.SECONDS);
    provider.shutdown().get();
    consumer.shutdown().get();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 40 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceCallTest method test_local_void_greeting.

@Test
public void test_local_void_greeting() throws InterruptedException, ExecutionException {
    // Create microservices instance.
    Microservices node = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
    ServiceCall service = node.dispatcher().create();
    // call the service.
    CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingVoid").data(new GreetingRequest("joe")).build());
    CountDownLatch timeLatch = new CountDownLatch(1);
    future.whenComplete((success, error) -> {
        if (error == null) {
            System.out.println("void return: " + success);
            assertTrue(success.data() == null);
            timeLatch.countDown();
        }
    });
    // send and forget so we have no way to know what happen
    // but at least we didn't get exception :)
    System.out.println("test_local_void_greeting done.");
    await(timeLatch, 1, TimeUnit.SECONDS);
    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