Search in sources :

Example 1 with MicroserviceInstanceChangedEvent

use of org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceVersions method onMicroserviceInstanceChangedMatch.

@Test
public void onMicroserviceInstanceChangedMatch() {
    MicroserviceKey key = new MicroserviceKey();
    key.setAppId(appId);
    key.setServiceName(microserviceName);
    MicroserviceInstanceChangedEvent event = new MicroserviceInstanceChangedEvent();
    event.setKey(key);
    eventBus.register(new Object() {

        @Subscribe
        public void onEvent(PullMicroserviceVersionsInstancesEvent pullEvent) {
            pendingPullCount.incrementAndGet();
        }
    });
    eventBus.post(event);
    Assert.assertEquals(2, pendingPullCount.get());
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.serviceregistry.api.MicroserviceKey) Subscribe(com.google.common.eventbus.Subscribe) PullMicroserviceVersionsInstancesEvent(org.apache.servicecomb.serviceregistry.task.event.PullMicroserviceVersionsInstancesEvent) Test(org.junit.Test)

Example 2 with MicroserviceInstanceChangedEvent

use of org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceVersions method checkIsEventAccept.

public void checkIsEventAccept(MicroserviceKey key, boolean expected) {
    MicroserviceInstanceChangedEvent changeEvent = new MicroserviceInstanceChangedEvent();
    changeEvent.setKey(key);
    boolean isEventAccept = microserviceVersions.isEventAccept(changeEvent);
    Assert.assertEquals(expected, isEventAccept);
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)

Example 3 with MicroserviceInstanceChangedEvent

use of org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceVersions method onMicroserviceInstanceChangedAppNotMatch.

@Test
public void onMicroserviceInstanceChangedAppNotMatch() {
    MicroserviceKey key = new MicroserviceKey();
    key.setAppId("otherAppId");
    MicroserviceInstanceChangedEvent event = new MicroserviceInstanceChangedEvent();
    event.setKey(key);
    microserviceVersions.onMicroserviceInstanceChanged(event);
    Assert.assertEquals(0, pendingPullCount.get());
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.serviceregistry.api.MicroserviceKey) Test(org.junit.Test)

Example 4 with MicroserviceInstanceChangedEvent

use of org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceWatchTask method testWatchInstanceChanged.

@Test
public void testWatchInstanceChanged(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    initWatch(serviceRegistryConfig, srClient, microservice);
    MicroserviceInstanceChangedEvent changedEvent = new MicroserviceInstanceChangedEvent();
    MicroserviceKey key = new MicroserviceKey();
    key.setAppId(microservice.getAppId());
    key.setVersion(microservice.getVersion());
    key.setServiceName(microservice.getServiceName());
    changedEvent.setKey(key);
    changedEvent.setInstance(microservice.getInstance());
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            callback.success(changedEvent);
        }
    };
    Holder<MicroserviceInstanceChangedEvent> holder = new Holder<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onException(MicroserviceInstanceChangedEvent event) {
            holder.value = event;
        }
    });
    changedEvent.setAction(WatchAction.CREATE);
    microserviceWatchTask.run();
    Assert.assertEquals(WatchAction.CREATE, holder.value.getAction());
    changedEvent.setAction(WatchAction.DELETE);
    microserviceWatchTask.run();
    Assert.assertEquals(WatchAction.DELETE, holder.value.getAction());
    changedEvent.setAction(WatchAction.UPDATE);
    microserviceWatchTask.run();
    Assert.assertEquals(WatchAction.UPDATE, holder.value.getAction());
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.serviceregistry.api.MicroserviceKey) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 5 with MicroserviceInstanceChangedEvent

use of org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent in project incubator-servicecomb-java-chassis by apache.

the class MicroserviceWatchTask method doRun.

@Override
public void doRun() {
    // will always run watch when it is ready
    if (!needToWatch()) {
        return;
    }
    srClient.watch(microservice.getServiceId(), (event) -> {
        if (event.failed()) {
            eventBus.post(new ExceptionEvent(event.cause()));
            return;
        }
        MicroserviceInstanceChangedEvent changedEvent = event.result();
        if (isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isWatch()) {
            return;
        }
        if (!isProviderInstancesChanged(changedEvent) && !serviceRegistryConfig.isRegistryAutoDiscovery()) {
            return;
        }
        onMicroserviceInstanceChanged(changedEvent);
    }, open -> {
        eventBus.post(new RecoveryEvent());
    }, close -> {
    });
}
Also used : ExceptionEvent(org.apache.servicecomb.serviceregistry.task.event.ExceptionEvent) MicroserviceInstanceChangedEvent(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) RecoveryEvent(org.apache.servicecomb.serviceregistry.task.event.RecoveryEvent)

Aggregations

MicroserviceInstanceChangedEvent (org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)7 MicroserviceKey (org.apache.servicecomb.serviceregistry.api.MicroserviceKey)4 Test (org.junit.Test)4 Subscribe (com.google.common.eventbus.Subscribe)2 Holder (javax.xml.ws.Holder)1 MockUp (mockit.MockUp)1 IpPort (org.apache.servicecomb.foundation.common.net.IpPort)1 AsyncResultCallback (org.apache.servicecomb.foundation.vertx.AsyncResultCallback)1 ClientException (org.apache.servicecomb.serviceregistry.client.ClientException)1 ExceptionEvent (org.apache.servicecomb.serviceregistry.task.event.ExceptionEvent)1 PullMicroserviceVersionsInstancesEvent (org.apache.servicecomb.serviceregistry.task.event.PullMicroserviceVersionsInstancesEvent)1 RecoveryEvent (org.apache.servicecomb.serviceregistry.task.event.RecoveryEvent)1