Search in sources :

Example 51 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class AbortFaultTest method injectFaultNoPercentageConfig.

@Test
public void injectFaultNoPercentageConfig() {
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent", null);
    assertNull(DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.abort.percent").getString());
    AbortFault abortFault = new AbortFault();
    FaultParam faultParam = new FaultParam(1);
    Vertx vertx = VertxUtils.getOrCreateVertxByName("faultinjectionTest", null);
    faultParam.setVertx(vertx);
    Holder<String> resultHolder = new Holder<>();
    abortFault.injectFault(invocation, faultParam, response -> resultHolder.value = response.getResult());
    AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName12");
    assertEquals(1, count.get());
    assertEquals("success", resultHolder.value);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Holder(org.apache.servicecomb.foundation.common.Holder) Vertx(io.vertx.core.Vertx) Test(org.junit.Test)

Example 52 with Holder

use of org.apache.servicecomb.foundation.common.Holder 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 53 with Holder

use of org.apache.servicecomb.foundation.common.Holder 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)

Example 54 with Holder

use of org.apache.servicecomb.foundation.common.Holder in project java-chassis by ServiceComb.

the class Log4jUtilsTest method init.

@Test
public void init() {
    Holder<Boolean> propertiesLoaded = new Holder<>(false);
    Holder<Boolean> logConfigured = new Holder<>(false);
    Holder<Boolean> mergedFileWritten = new Holder<>(false);
    final Properties logProperties = new Properties();
    logProperties.setProperty("paas.logs.file", "cse.log");
    final ArrayList<Resource> logResList = new ArrayList<>();
    new MockUp<PropertiesLoader>() {

        @Mock
        Properties load() {
            propertiesLoaded.value = true;
            return logProperties;
        }

        @Mock
        List<Resource> getFoundResList() {
            return logResList;
        }
    };
    new MockUp<PropertyConfigurator>() {

        @Mock
        void configure(Properties properties) {
            logConfigured.value = true;
            Assert.assertSame(properties, logProperties);
        }
    };
    new MockUp<Log4jUtils>() {

        @Mock
        void outputFile(List<Resource> resList, Properties properties) {
            mergedFileWritten.value = true;
            Assert.assertSame(logResList, resList);
            Assert.assertSame(logProperties, properties);
        }
    };
    Assert.assertFalse(Deencapsulation.getField(Log4jUtils.class, "inited"));
    try {
        Log4jUtils.init();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    Assert.assertTrue(Deencapsulation.getField(Log4jUtils.class, "inited"));
    Assert.assertTrue(propertiesLoaded.value);
    Assert.assertTrue(logConfigured.value);
    Assert.assertTrue(mergedFileWritten.value);
}
Also used : Holder(org.apache.servicecomb.foundation.common.Holder) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) MockUp(mockit.MockUp) ArrayList(java.util.ArrayList) List(java.util.List) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

Holder (org.apache.servicecomb.foundation.common.Holder)54 Test (org.junit.Test)44 MockUp (mockit.MockUp)18 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Invocation (org.apache.servicecomb.core.Invocation)12 Expectations (mockit.Expectations)11 Vertx (io.vertx.core.Vertx)10 List (java.util.List)9 Response (org.apache.servicecomb.swagger.invocation.Response)9 Mock (mockit.Mock)8 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 SCBEngine (org.apache.servicecomb.core.SCBEngine)7 ExpectedException (org.junit.rules.ExpectedException)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Status (javax.ws.rs.core.Response.Status)6 Deencapsulation (mockit.Deencapsulation)6 Mocked (mockit.Mocked)6 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)6