Search in sources :

Example 21 with Message

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

the class SimpleStressTest method test_naive_dispatcher_stress.

@Test
public void test_naive_dispatcher_stress() throws InterruptedException, ExecutionException {
    // Create microservices cluster member.
    Microservices provider = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).metrics(registry).build();
    // Create microservices cluster member.
    Microservices consumer = Microservices.builder().port(port.incrementAndGet()).seeds(provider.cluster().address()).metrics(registry).build();
    reporter.start(10, TimeUnit.SECONDS);
    ServiceCall greetings = consumer.dispatcher().timeout(Duration.ofSeconds(30)).create();
    // Measure
    CountDownLatch countLatch = new CountDownLatch(count);
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        CompletableFuture<Message> future = greetings.invoke(Messages.builder().request(GreetingService.class, "greetingMessage").data("1").build());
        future.whenComplete((success, error) -> {
            countLatch.countDown();
        });
    }
    System.out.println("Finished sending " + count + " messages in " + (System.currentTimeMillis() - startTime));
    countLatch.await(60, TimeUnit.SECONDS);
    reporter.stop();
    System.out.println("Finished receiving " + count + " messages in " + (System.currentTimeMillis() - startTime));
    assertTrue(countLatch.getCount() == 0);
    provider.shutdown().get();
    consumer.shutdown().get();
}
Also used : ServiceCall(io.scalecube.services.ServiceCall) Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 22 with Message

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

the class TestStreamingService method test_just_one_message.

@Test
public void test_just_one_message() throws InterruptedException {
    int batchSize = 1;
    Microservices gateway = Microservices.builder().build();
    Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
    ServiceCall service = gateway.dispatcher().create();
    final CountDownLatch latch1 = new CountDownLatch(batchSize);
    AtomicReference<Subscription> sub1 = new AtomicReference<Subscription>(null);
    Message justOne = Messages.builder().request(QuoteService.NAME, "justOne").build();
    sub1.set(service.listen(justOne).serialize().subscribe(onNext -> {
        sub1.get().unsubscribe();
        latch1.countDown();
    }));
    latch1.await(2, TimeUnit.SECONDS);
    assertTrue(latch1.getCount() == 0);
    assertTrue(sub1.get().isUnsubscribed());
    gateway.shutdown();
    node.shutdown();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Executors(java.util.concurrent.Executors) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) Messages(io.scalecube.services.Messages) CountDownLatch(java.util.concurrent.CountDownLatch) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) Schedulers(rx.schedulers.Schedulers) Message(io.scalecube.transport.Message) BaseTest(io.scalecube.testlib.BaseTest) Subscription(rx.Subscription) ServiceCall(io.scalecube.services.ServiceCall) Message(io.scalecube.transport.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 23 with Message

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

the class TestStreamingService method test_remote_node_died.

@Test
public void test_remote_node_died() throws InterruptedException {
    int batchSize = 1;
    Microservices gateway = Microservices.builder().build();
    Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
    ServiceCall service = gateway.dispatcher().create();
    final CountDownLatch latch1 = new CountDownLatch(batchSize);
    AtomicReference<Subscription> sub1 = new AtomicReference<Subscription>(null);
    Message justOne = Messages.builder().request(QuoteService.NAME, "justOne").build();
    sub1.set(service.listen(justOne).subscribe(onNext -> {
        System.out.println(onNext);
    }));
    gateway.cluster().listenMembership().filter(predicate -> predicate.isRemoved()).subscribe(onNext -> {
        latch1.countDown();
    });
    node.cluster().shutdown();
    latch1.await(20, TimeUnit.SECONDS);
    Thread.sleep(100);
    assertTrue(latch1.getCount() == 0);
    assertTrue(sub1.get().isUnsubscribed());
    gateway.shutdown();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Executors(java.util.concurrent.Executors) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) Messages(io.scalecube.services.Messages) CountDownLatch(java.util.concurrent.CountDownLatch) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) Schedulers(rx.schedulers.Schedulers) Message(io.scalecube.transport.Message) BaseTest(io.scalecube.testlib.BaseTest) Subscription(rx.Subscription) ServiceCall(io.scalecube.services.ServiceCall) Message(io.scalecube.transport.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 24 with Message

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

the class DispatchingFutureTest method test_dispatching_future_error.

@Test
public void test_dispatching_future_error() throws Exception {
    Microservices member = Microservices.builder().build();
    ServiceResponse response = ServiceResponse.correlationId(IdGenerator.generateId());
    Message request = Message.builder().correlationId(response.correlationId()).header(ServiceHeaders.SERVICE_RESPONSE, "").build();
    Field field = Message.class.getDeclaredField("sender");
    field.setAccessible(true);
    field.set(request, member.cluster().address());
    CountDownLatch latch = new CountDownLatch(1);
    response.future().whenComplete((result, error) -> {
        assertTrue(error != null);
        assertEquals(error.getMessage(), "hello");
        latch.countDown();
    });
    response.complete(Message.builder().header("exception", "true").data(new Exception("hello")).build());
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(latch.getCount() == 0);
    member.shutdown();
}
Also used : Field(java.lang.reflect.Field) Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 25 with Message

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

the class DispatchingFutureTest method test_dispatching_future_completeExceptionally.

@Test
public void test_dispatching_future_completeExceptionally() throws Exception {
    Microservices member = Microservices.builder().build();
    ServiceResponse response = ServiceResponse.correlationId(IdGenerator.generateId());
    Message request = Message.builder().correlationId(response.correlationId()).header(ServiceHeaders.SERVICE_RESPONSE, "").build();
    Field field = Message.class.getDeclaredField("sender");
    field.setAccessible(true);
    field.set(request, member.cluster().address());
    DispatchingFuture dispatcher = DispatchingFuture.from(member.sender(), request);
    CountDownLatch latch = new CountDownLatch(1);
    response.future().whenComplete((result, error) -> {
        assertTrue(error != null);
        assertEquals(error.getMessage(), "hello");
        latch.countDown();
    });
    response.completeExceptionally(new Exception("hello"));
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(latch.getCount() == 0);
    member.shutdown();
}
Also used : Field(java.lang.reflect.Field) 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