Search in sources :

Example 1 with ENDPOINT_REMOVED

use of io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED in project scalecube by scalecube.

the class ServiceRegistryTest method test_listen_to_discovery_events.

@ParameterizedTest
@MethodSource("metadataCodecSource")
public void test_listen_to_discovery_events(MetadataCodec metadataCodec) {
    Sinks.Many<ServiceDiscoveryEvent> processor = Sinks.many().replay().all();
    List<Microservices> cluster = new CopyOnWriteArrayList<>();
    Microservices seed = Microservices.builder().discovery(defServiceDiscovery(metadataCodec)).transport(RSocketServiceTransport::new).services(new AnnotationServiceImpl()).startAwait();
    cluster.add(seed);
    seed.listenDiscovery().subscribe(processor::tryEmitNext, processor::tryEmitError, processor::tryEmitComplete);
    Address seedAddress = seed.discovery().address();
    StepVerifier.create(processor.asFlux().onBackpressureBuffer()).then(() -> {
        Microservices ms1 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
        cluster.add(ms1);
    }).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> {
        Microservices ms2 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
        cluster.add(ms2);
    }).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> cluster.remove(2).shutdown().block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).then(() -> cluster.remove(1).shutdown().block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).thenCancel().verify(TIMEOUT);
    StepVerifier.create(seed.call().api(AnnotationService.class).serviceDiscoveryEventTypes()).assertNext(type -> assertEquals(ENDPOINT_ADDED, type)).assertNext(type -> assertEquals(ENDPOINT_ADDED, type)).assertNext(type -> assertEquals(ENDPOINT_LEAVING, type)).assertNext(type -> assertEquals(ENDPOINT_REMOVED, type)).assertNext(type -> assertEquals(ENDPOINT_LEAVING, type)).assertNext(type -> assertEquals(ENDPOINT_REMOVED, type)).thenCancel().verify(TIMEOUT);
    Mono.whenDelayError(cluster.stream().map(Microservices::shutdown).toArray(Mono[]::new)).block(TIMEOUT);
}
Also used : Sinks(reactor.core.publisher.Sinks) ENDPOINT_LEAVING(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_LEAVING) StepVerifier(reactor.test.StepVerifier) MetadataCodec(io.scalecube.cluster.metadata.MetadataCodec) JdkMetadataCodec(io.scalecube.cluster.metadata.JdkMetadataCodec) ENDPOINT_ADDED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_ADDED) Duration(java.time.Duration) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Address(io.scalecube.net.Address) MethodSource(org.junit.jupiter.params.provider.MethodSource) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ENDPOINT_REMOVED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED) JacksonMetadataCodec(io.scalecube.cluster.codec.jackson.JacksonMetadataCodec) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) AnnotationService(io.scalecube.services.sut.AnnotationService) AnnotationServiceImpl(io.scalecube.services.sut.AnnotationServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ServiceDiscoveryFactory(io.scalecube.services.discovery.api.ServiceDiscoveryFactory) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Sinks(reactor.core.publisher.Sinks) Address(io.scalecube.net.Address) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) AnnotationServiceImpl(io.scalecube.services.sut.AnnotationServiceImpl) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with ENDPOINT_REMOVED

use of io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED in project scalecube by scalecube.

the class ScalecubeServiceDiscoveryTest method testEndpointIsAddedThenRemoved.

@Disabled
@ParameterizedTest
@MethodSource("metadataCodecSource")
public void testEndpointIsAddedThenRemoved(MetadataCodec metadataCodec) {
    startSeed(metadataCodec);
    AtomicInteger registeredCount = new AtomicInteger();
    AtomicInteger unregisteredCount = new AtomicInteger();
    RecordingServiceDiscovery r1 = RecordingServiceDiscovery.create(() -> newServiceDiscovery(SEED_ADDRESS, metadataCodec));
    RecordingServiceDiscovery r2 = RecordingServiceDiscovery.create(() -> newServiceDiscovery(SEED_ADDRESS, metadataCodec));
    RecordingServiceDiscovery r3 = RecordingServiceDiscovery.create(() -> newServiceDiscovery(SEED_ADDRESS, metadataCodec));
    // (1+3)x(1+3) - (1+3)/*exclude self*/ - 3/*exclude seed*/
    int expectedAddedEventsNum = 9;
    // r3 is shutdown => await by 1 event on r1 and r2
    int expectedRemovedEventsNum = 2;
    StepVerifier.create(Flux.merge(r1.nonGroupDiscoveryEvents(), r2.nonGroupDiscoveryEvents(), r3.nonGroupDiscoveryEvents())).thenConsumeWhile(event -> {
        assertEquals(ENDPOINT_ADDED, event.type());
        assertNotNull(event.serviceEndpoint());
        return registeredCount.incrementAndGet() < expectedAddedEventsNum;
    }).expectNoEvent(SHORT_TIMEOUT).then(r3::shutdown).thenConsumeWhile(event -> {
        assertEquals(ENDPOINT_REMOVED, event.type());
        assertNotNull(event.serviceEndpoint());
        return unregisteredCount.incrementAndGet() < expectedRemovedEventsNum;
    }).expectNoEvent(SHORT_TIMEOUT).thenCancel().verify();
}
Also used : Sinks(reactor.core.publisher.Sinks) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StepVerifier(reactor.test.StepVerifier) MetadataCodec(io.scalecube.cluster.metadata.MetadataCodec) JdkMetadataCodec(io.scalecube.cluster.metadata.JdkMetadataCodec) MembershipConfig(io.scalecube.cluster.membership.MembershipConfig) Disabled(org.junit.jupiter.api.Disabled) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) FailureDetectorConfig(io.scalecube.cluster.fdetector.FailureDetectorConfig) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) ENDPOINT_ADDED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_ADDED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ServiceMethodDefinition(io.scalecube.services.ServiceMethodDefinition) Address(io.scalecube.net.Address) MethodSource(org.junit.jupiter.params.provider.MethodSource) ClusterMath(io.scalecube.cluster.ClusterMath) ENDPOINT_REMOVED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED) JacksonMetadataCodec(io.scalecube.cluster.codec.jackson.JacksonMetadataCodec) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ServiceRegistration(io.scalecube.services.ServiceRegistration) Arguments(org.junit.jupiter.params.provider.Arguments) ServiceDiscovery(io.scalecube.services.discovery.api.ServiceDiscovery) Flux(reactor.core.publisher.Flux) GossipConfig(io.scalecube.cluster.gossip.GossipConfig) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Assertions(org.junit.jupiter.api.Assertions) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with ENDPOINT_REMOVED

use of io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED in project scalecube by scalecube.

the class ScalecubeServiceDiscoveryTest method testEndpointIsRestarted.

@Disabled
@ParameterizedTest
@MethodSource("metadataCodecSource")
public void testEndpointIsRestarted(MetadataCodec metadataCodec) {
    startSeed(metadataCodec);
    AtomicInteger registeredCount = new AtomicInteger();
    AtomicInteger unregisteredCount = new AtomicInteger();
    RecordingServiceDiscovery r1 = RecordingServiceDiscovery.create(() -> newServiceDiscovery(SEED_ADDRESS, metadataCodec));
    RecordingServiceDiscovery r2 = RecordingServiceDiscovery.create(() -> newServiceDiscovery(SEED_ADDRESS, metadataCodec));
    RecordingServiceDiscovery r3 = RecordingServiceDiscovery.create(() -> newServiceDiscovery(SEED_ADDRESS, metadataCodec));
    // (1+3)x(1+3) - (1+3)/*exclude self*/ - 3/*exclude seed*/
    int expectedAddedEventsNum = 9;
    // r3 is shutdown => await by 1 event on r1 and r2
    int expectedRemovedEventsNum = 2;
    StepVerifier.create(Flux.merge(r1.nonGroupDiscoveryEvents(), r2.nonGroupDiscoveryEvents(), r3.nonGroupDiscoveryEvents())).thenConsumeWhile(event -> {
        assertEquals(ENDPOINT_ADDED, event.type());
        assertNotNull(event.serviceEndpoint());
        return registeredCount.incrementAndGet() < expectedAddedEventsNum;
    }).expectNoEvent(SHORT_TIMEOUT).then(r3::shutdown).thenConsumeWhile(event -> {
        assertEquals(ENDPOINT_REMOVED, event.type());
        assertNotNull(event.serviceEndpoint());
        return unregisteredCount.incrementAndGet() < expectedRemovedEventsNum;
    }).expectNoEvent(SHORT_TIMEOUT).thenCancel().verify();
    AtomicInteger registeredCountAfterRestart = new AtomicInteger();
    // r3 is restared => await by 1 event on r1 and r2
    int expectedAddedEventsNumAfterRestart = 2;
    r1 = r1.resubscribe();
    r2 = r2.resubscribe();
    r3 = r3.recreate();
    StepVerifier.create(Flux.merge(r1.nonGroupDiscoveryEvents(), r2.nonGroupDiscoveryEvents(), r3.nonGroupDiscoveryEvents())).thenConsumeWhile(event -> {
        assertEquals(ENDPOINT_ADDED, event.type());
        assertNotNull(event.serviceEndpoint());
        return registeredCountAfterRestart.incrementAndGet() < expectedAddedEventsNumAfterRestart;
    }).expectNoEvent(SHORT_TIMEOUT).thenCancel().verify();
}
Also used : Sinks(reactor.core.publisher.Sinks) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StepVerifier(reactor.test.StepVerifier) MetadataCodec(io.scalecube.cluster.metadata.MetadataCodec) JdkMetadataCodec(io.scalecube.cluster.metadata.JdkMetadataCodec) MembershipConfig(io.scalecube.cluster.membership.MembershipConfig) Disabled(org.junit.jupiter.api.Disabled) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) FailureDetectorConfig(io.scalecube.cluster.fdetector.FailureDetectorConfig) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) ENDPOINT_ADDED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_ADDED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ServiceMethodDefinition(io.scalecube.services.ServiceMethodDefinition) Address(io.scalecube.net.Address) MethodSource(org.junit.jupiter.params.provider.MethodSource) ClusterMath(io.scalecube.cluster.ClusterMath) ENDPOINT_REMOVED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED) JacksonMetadataCodec(io.scalecube.cluster.codec.jackson.JacksonMetadataCodec) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ServiceRegistration(io.scalecube.services.ServiceRegistration) Arguments(org.junit.jupiter.params.provider.Arguments) ServiceDiscovery(io.scalecube.services.discovery.api.ServiceDiscovery) Flux(reactor.core.publisher.Flux) GossipConfig(io.scalecube.cluster.gossip.GossipConfig) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Assertions(org.junit.jupiter.api.Assertions) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with ENDPOINT_REMOVED

use of io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED in project scalecube by scalecube.

the class ServiceRegistryTest method test_added_removed_registration_events.

@ParameterizedTest
@MethodSource("metadataCodecSource")
public void test_added_removed_registration_events(MetadataCodec metadataCodec) {
    Sinks.Many<ServiceDiscoveryEvent> events = Sinks.many().replay().all();
    Microservices seed = Microservices.builder().discovery(defServiceDiscovery(metadataCodec)).transport(RSocketServiceTransport::new).startAwait();
    seed.listenDiscovery().subscribe(events::tryEmitNext, events::tryEmitError, events::tryEmitComplete);
    Address seedAddress = seed.discovery().address();
    Microservices ms1 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
    Microservices ms2 = Microservices.builder().discovery(defServiceDiscovery(seedAddress, metadataCodec)).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
    StepVerifier.create(events.asFlux().onBackpressureBuffer()).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).assertNext(event -> assertEquals(ENDPOINT_ADDED, event.type())).then(() -> Mono.whenDelayError(ms1.shutdown(), ms2.shutdown()).block(TIMEOUT)).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_LEAVING, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).assertNext(event -> assertEquals(ENDPOINT_REMOVED, event.type())).then(() -> seed.shutdown().block(TIMEOUT)).thenCancel().verify(TIMEOUT);
}
Also used : Sinks(reactor.core.publisher.Sinks) ENDPOINT_LEAVING(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_LEAVING) StepVerifier(reactor.test.StepVerifier) MetadataCodec(io.scalecube.cluster.metadata.MetadataCodec) JdkMetadataCodec(io.scalecube.cluster.metadata.JdkMetadataCodec) ENDPOINT_ADDED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_ADDED) Duration(java.time.Duration) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Address(io.scalecube.net.Address) MethodSource(org.junit.jupiter.params.provider.MethodSource) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ENDPOINT_REMOVED(io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED) JacksonMetadataCodec(io.scalecube.cluster.codec.jackson.JacksonMetadataCodec) Mono(reactor.core.publisher.Mono) Arguments(org.junit.jupiter.params.provider.Arguments) AnnotationService(io.scalecube.services.sut.AnnotationService) AnnotationServiceImpl(io.scalecube.services.sut.AnnotationServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ServiceDiscoveryFactory(io.scalecube.services.discovery.api.ServiceDiscoveryFactory) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Sinks(reactor.core.publisher.Sinks) Address(io.scalecube.net.Address) ServiceDiscoveryEvent(io.scalecube.services.discovery.api.ServiceDiscoveryEvent) GreetingServiceImpl(io.scalecube.services.sut.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

JacksonMetadataCodec (io.scalecube.cluster.codec.jackson.JacksonMetadataCodec)4 JdkMetadataCodec (io.scalecube.cluster.metadata.JdkMetadataCodec)4 MetadataCodec (io.scalecube.cluster.metadata.MetadataCodec)4 Address (io.scalecube.net.Address)4 ServiceDiscoveryEvent (io.scalecube.services.discovery.api.ServiceDiscoveryEvent)4 ENDPOINT_ADDED (io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_ADDED)4 ENDPOINT_REMOVED (io.scalecube.services.discovery.api.ServiceDiscoveryEvent.Type.ENDPOINT_REMOVED)4 WebsocketTransportFactory (io.scalecube.transport.netty.websocket.WebsocketTransportFactory)4 Duration (java.time.Duration)4 List (java.util.List)4 Stream (java.util.stream.Stream)4 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Arguments (org.junit.jupiter.params.provider.Arguments)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 Mono (reactor.core.publisher.Mono)4 Sinks (reactor.core.publisher.Sinks)4 StepVerifier (reactor.test.StepVerifier)4 ClusterMath (io.scalecube.cluster.ClusterMath)2 FailureDetectorConfig (io.scalecube.cluster.fdetector.FailureDetectorConfig)2