-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
services-api/src/main/java/io/scalecube/services/annotations/Subscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.scalecube.services.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
public @interface Subscriber { | ||
|
||
/** | ||
* Event type to subscribe on. | ||
* | ||
* @return event type | ||
*/ | ||
Class<?> value() default Object.class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
services/src/test/java/io/scalecube/services/ServiceDiscoverySubscriberTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.scalecube.services; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
||
import io.scalecube.services.annotations.Subscriber; | ||
import io.scalecube.services.discovery.api.ServiceDiscoveryEvent; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.jupiter.api.Test; | ||
import org.reactivestreams.Subscription; | ||
import reactor.core.publisher.BaseSubscriber; | ||
|
||
public class ServiceDiscoverySubscriberTest extends BaseTest { | ||
|
||
@Test | ||
void testRegisterNonDiscoveryCoreSubscriber() { | ||
final NonDiscoverySubscriber1 discoverySubscriber1 = spy(new NonDiscoverySubscriber1()); | ||
final NonDiscoverySubscriber2 discoverySubscriber2 = spy(new NonDiscoverySubscriber2()); | ||
|
||
Microservices.builder().services(discoverySubscriber1, discoverySubscriber2).startAwait(); | ||
|
||
verifyNoInteractions(discoverySubscriber1, discoverySubscriber2); | ||
} | ||
|
||
@Test | ||
void testRegisterNotMatchingTypeDiscoveryCoreSubscriber() { | ||
final NotMatchingTypeDiscoverySubscriber discoverySubscriber = | ||
spy(new NotMatchingTypeDiscoverySubscriber()); | ||
|
||
Microservices.builder().services(discoverySubscriber).startAwait(); | ||
|
||
verifyNoInteractions(discoverySubscriber); | ||
} | ||
|
||
@Test | ||
void testRegisterDiscoveryCoreSubscriber() { | ||
final AtomicReference<Subscription> subscriptionReference = new AtomicReference<>(); | ||
final NormalDiscoverySubscriber normalDiscoverySubscriber = | ||
new NormalDiscoverySubscriber(subscriptionReference); | ||
|
||
Microservices.builder().services(normalDiscoverySubscriber).startAwait(); | ||
|
||
assertNotNull(subscriptionReference.get(), "subscription"); | ||
} | ||
|
||
private static class SomeType {} | ||
|
||
private static class NonDiscoverySubscriber1 extends BaseSubscriber {} | ||
|
||
private static class NonDiscoverySubscriber2 extends BaseSubscriber<SomeType> {} | ||
|
||
@Subscriber | ||
private static class NotMatchingTypeDiscoverySubscriber | ||
extends BaseSubscriber<ServiceDiscoveryEvent> {} | ||
|
||
@Subscriber(ServiceDiscoveryEvent.class) | ||
private static class NormalDiscoverySubscriber extends BaseSubscriber<ServiceDiscoveryEvent> { | ||
|
||
private final AtomicReference<Subscription> subscriptionReference; | ||
|
||
private NormalDiscoverySubscriber(AtomicReference<Subscription> subscriptionReference) { | ||
this.subscriptionReference = subscriptionReference; | ||
} | ||
|
||
@Override | ||
protected void hookOnSubscribe(Subscription subscription) { | ||
subscriptionReference.set(subscription); | ||
} | ||
} | ||
} |