Search in sources :

Example 1 with AsyncResultCallback

use of org.apache.servicecomb.foundation.vertx.AsyncResultCallback 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 2 with AsyncResultCallback

use of org.apache.servicecomb.foundation.vertx.AsyncResultCallback in project incubator-servicecomb-java-chassis by apache.

the class TestMicroserviceWatchTask method testNeedToWatch.

@Test
public void testNeedToWatch(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    EventBus eventBus = new EventBus();
    MicroserviceWatchTask microserviceWatchTask = new MicroserviceWatchTask(eventBus, serviceRegistryConfig, srClient, microservice);
    microserviceWatchTask.taskStatus = TaskStatus.READY;
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            throw new Error("called watch");
        }
    };
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = false;
        }
    };
    // no watch
    try {
        microserviceWatchTask.run();
    } catch (Throwable e) {
        Assert.fail("must do not watch");
    }
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = true;
        }
    };
    // no watch
    try {
        microserviceWatchTask.run();
    } catch (Throwable e) {
        // ready state, service id can not be null , will always watch
        Assert.assertEquals("called watch", e.getMessage());
    }
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = false;
        }
    };
    // no watch
    try {
        microserviceWatchTask.run();
    } catch (Throwable e) {
        // ready state, service id can not be null , will always watch
        Assert.assertEquals("called watch", e.getMessage());
    }
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = true;
            microservice.getServiceId();
            result = "serviceId";
        }
    };
    // watch
    try {
        microserviceWatchTask.run();
        Assert.fail("must watch");
    } catch (Throwable e) {
        Assert.assertEquals("called watch", e.getMessage());
    }
}
Also used : Expectations(mockit.Expectations) MockUp(mockit.MockUp) EventBus(com.google.common.eventbus.EventBus) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 3 with AsyncResultCallback

use of org.apache.servicecomb.foundation.vertx.AsyncResultCallback in project java-chassis by ServiceComb.

the class TestMicroserviceWatchTask method testNeedToWatch.

@Test
public void testNeedToWatch(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    EventBus eventBus = new EventBus();
    MicroserviceWatchTask microserviceWatchTask = new MicroserviceWatchTask(eventBus, serviceRegistryConfig, srClient, microservice);
    microserviceWatchTask.taskStatus = TaskStatus.READY;
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            throw new RuntimeExceptionWithoutStackTrace("called watch");
        }
    };
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = false;
        }
    };
    // no watch
    try {
        microserviceWatchTask.run();
    } catch (Throwable e) {
        Assert.fail("must do not watch");
    }
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = true;
        }
    };
    // no watch
    try {
        microserviceWatchTask.run();
    } catch (Throwable e) {
        // ready state, service id can not be null , will always watch
        Assert.assertEquals("called watch", e.getMessage());
    }
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = false;
        }
    };
    // no watch
    try {
        microserviceWatchTask.run();
    } catch (Throwable e) {
        // ready state, service id can not be null , will always watch
        Assert.assertEquals("called watch", e.getMessage());
    }
    new Expectations() {

        {
            serviceRegistryConfig.isWatch();
            result = true;
            microservice.getServiceId();
            result = "serviceId";
        }
    };
    // watch
    try {
        microserviceWatchTask.run();
        Assert.fail("must watch");
    } catch (Throwable e) {
        Assert.assertEquals("called watch", e.getMessage());
    }
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) Expectations(mockit.Expectations) MockUp(mockit.MockUp) EventBus(com.google.common.eventbus.EventBus) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 4 with AsyncResultCallback

use of org.apache.servicecomb.foundation.vertx.AsyncResultCallback in project java-chassis by ServiceComb.

the class TestMicroserviceWatchTask method testWatchFailed.

@Test
public void testWatchFailed(@Mocked ServiceRegistryConfig serviceRegistryConfig, @Mocked ServiceRegistryClient srClient, @Mocked Microservice microservice) {
    initWatch(serviceRegistryConfig, srClient, microservice);
    new MockUp<ServiceRegistryClient>(srClient) {

        @Mock
        void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
            callback.fail(new RuntimeExceptionWithoutStackTrace("test failed"));
        }
    };
    Holder<Throwable> holder = new Holder<>();
    eventBus.register(new Object() {

        @Subscribe
        public void onException(ExceptionEvent event) {
            holder.value = event.getThrowable();
        }
    });
    Assert.assertNull(holder.value);
    microserviceWatchTask.run();
    Assert.assertEquals("test failed", holder.value.getMessage());
}
Also used : RuntimeExceptionWithoutStackTrace(org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace) ExceptionEvent(org.apache.servicecomb.serviceregistry.event.ExceptionEvent) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 5 with AsyncResultCallback

use of org.apache.servicecomb.foundation.vertx.AsyncResultCallback in project java-chassis by ServiceComb.

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.registry.api.event.MicroserviceInstanceChangedEvent) MicroserviceKey(org.apache.servicecomb.registry.api.MicroserviceKey) Holder(org.apache.servicecomb.foundation.common.Holder) MockUp(mockit.MockUp) Subscribe(com.google.common.eventbus.Subscribe) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Aggregations

MockUp (mockit.MockUp)5 AsyncResultCallback (org.apache.servicecomb.foundation.vertx.AsyncResultCallback)5 Test (org.junit.Test)5 Subscribe (com.google.common.eventbus.Subscribe)3 EventBus (com.google.common.eventbus.EventBus)2 Expectations (mockit.Expectations)2 Holder (org.apache.servicecomb.foundation.common.Holder)2 RuntimeExceptionWithoutStackTrace (org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace)2 Holder (javax.xml.ws.Holder)1 MicroserviceKey (org.apache.servicecomb.registry.api.MicroserviceKey)1 MicroserviceInstanceChangedEvent (org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent)1 MicroserviceKey (org.apache.servicecomb.serviceregistry.api.MicroserviceKey)1 MicroserviceInstanceChangedEvent (org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent)1 ExceptionEvent (org.apache.servicecomb.serviceregistry.event.ExceptionEvent)1