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());
}
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());
}
}
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());
}
}
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());
}
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());
}
Aggregations