Search in sources :

Example 21 with Holder

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

the class Log4jUtilsTest method initOnMergedFileOutputDisabled.

@Test
public void initOnMergedFileOutputDisabled() {
    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("log4j.logger.outputConfig.enabled", "false");
    final ArrayList<Resource> logResList = new ArrayList<>();
    new MockUp<PropertiesLoader>() {

        @Mock
        Properties load() {
            propertiesLoaded.value = true;
            return logProperties;
        }
    };
    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.assertFalse(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)

Example 22 with Holder

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

the class TestLoadbalanceHandler method send_success.

@Test
public void send_success(@Injectable LoadBalancer loadBalancer) {
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
    ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
    LoadBalancerStats stats = new LoadBalancerStats("test");
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
            loadBalancer.getLoadBalancerStats();
            result = stats;
        }
    };
    sendResponse = Response.ok("success");
    Holder<String> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = resp.getResult();
    }, loadBalancer);
    Assert.assertEquals(1, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getActiveRequestsCount());
    Assert.assertEquals("success", result.value);
}
Also used : Expectations(mockit.Expectations) SCBEngine(org.apache.servicecomb.core.SCBEngine) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Transport(org.apache.servicecomb.core.Transport) Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryFilter(org.apache.servicecomb.registry.discovery.DiscoveryFilter) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Status(javax.ws.rs.core.Response.Status) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Before(org.junit.Before) MockUp(mockit.MockUp) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) Invocation(org.apache.servicecomb.core.Invocation) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Rule(org.junit.Rule) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) Assert(org.junit.Assert) Collections(java.util.Collections) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Holder(org.apache.servicecomb.foundation.common.Holder) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 23 with Holder

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

the class TestLoadbalanceHandler method send_failed2.

@Test
public void send_failed2(@Injectable LoadBalancer loadBalancer) {
    MicroserviceInstance instance1 = new MicroserviceInstance();
    instance1.setInstanceId("1234");
    CacheEndpoint cacheEndpoint = new CacheEndpoint("rest://localhost:8080", instance1);
    ServiceCombServer server = new ServiceCombServer(null, restTransport, cacheEndpoint);
    LoadBalancerStats stats = new LoadBalancerStats("test");
    new Expectations(loadBalancer) {

        {
            loadBalancer.chooseServer(invocation);
            result = server;
            loadBalancer.getLoadBalancerStats();
            result = stats;
        }
    };
    sendResponse = Response.create(Status.BAD_REQUEST, "send failed");
    Holder<Throwable> result = new Holder<>();
    Deencapsulation.invoke(handler, "send", invocation, (AsyncResponse) resp -> {
        result.value = (Throwable) resp.getResult();
    }, loadBalancer);
    // InvocationException is not taken as a failure
    Assert.assertEquals(0, loadBalancer.getLoadBalancerStats().getSingleServerStat(server).getSuccessiveConnectionFailureCount());
    Assert.assertEquals("InvocationException: code=400;msg=send failed", result.value.getMessage());
}
Also used : Expectations(mockit.Expectations) SCBEngine(org.apache.servicecomb.core.SCBEngine) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Transport(org.apache.servicecomb.core.Transport) Expectations(mockit.Expectations) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryFilter(org.apache.servicecomb.registry.discovery.DiscoveryFilter) SPIServiceUtils(org.apache.servicecomb.foundation.common.utils.SPIServiceUtils) ArchaiusUtils(org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) ArrayList(java.util.ArrayList) SocketException(java.net.SocketException) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) SCBBootstrap(org.apache.servicecomb.core.bootstrap.SCBBootstrap) Status(javax.ws.rs.core.Response.Status) Response(org.apache.servicecomb.swagger.invocation.Response) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Before(org.junit.Before) MockUp(mockit.MockUp) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ConfigUtil(org.apache.servicecomb.config.ConfigUtil) Matchers(org.hamcrest.Matchers) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test) Deencapsulation(mockit.Deencapsulation) Invocation(org.apache.servicecomb.core.Invocation) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Rule(org.junit.Rule) TransportManager(org.apache.servicecomb.core.transport.TransportManager) Injectable(mockit.Injectable) Assert(org.junit.Assert) Collections(java.util.Collections) Mocked(mockit.Mocked) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Holder(org.apache.servicecomb.foundation.common.Holder) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Test(org.junit.Test)

Example 24 with Holder

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

the class TestFaultInjectHandler method testFaultInjectHandlerConfigChangeEvent4.

/**
 * Tests the fault injection handler functionality with configuration change event for service level config.
 */
@Test
public void testFaultInjectHandlerConfigChangeEvent4() throws Exception {
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.fixedDelay", "1");
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.percent", "100");
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.percent", "100");
    System.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.httpStatus", "421");
    Mockito.when(invocation.getMicroserviceQualifiedName()).thenReturn("MicroserviceQualifiedName10");
    Mockito.when(invocation.getTransport()).thenReturn(transport);
    Mockito.when(transport.getName()).thenReturn("rest");
    Mockito.when(invocation.getOperationName()).thenReturn("sayBye4");
    Mockito.when(invocation.getSchemaId()).thenReturn("testSchema4");
    Mockito.when(invocation.getMicroserviceName()).thenReturn("carts4");
    boolean validAssert;
    long timeOld = System.currentTimeMillis();
    List<Fault> faultInjectionFeatureList = Arrays.asList(delayFault, abortFault);
    handler.setFaultFeature(faultInjectionFeatureList);
    try {
        validAssert = true;
        handler.handle(invocation, ar);
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.fixedDelay", 500);
    Holder<Boolean> isAsserted = new Holder<>(false);
    handler.handle(invocation, ar -> {
        // check whether error code return,
        isAsserted.value = true;
        assertEquals(421, response.getStatusCode());
        assertTrue(response.isFailed());
        long timeNow = System.currentTimeMillis();
        // if really time delay is added it should be greater than 5s.
        Assert.assertTrue((timeNow - timeOld) >= 500);
    });
    Assert.assertTrue(isAsserted.value);
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.fixedDelay");
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.delay.percent");
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.percent");
    System.getProperties().remove("servicecomb.governance.Consumer.carts4.policy.fault.protocols.rest.abort.httpStatus");
    AtomicLong count = FaultInjectionUtil.getOperMetTotalReq("restMicroserviceQualifiedName10");
    assertEquals(3, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Holder(org.apache.servicecomb.foundation.common.Holder) Test(org.junit.Test)

Example 25 with Holder

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

the class DelayFaultTest method injectFaultNoPercentageConfig.

@Test
public void injectFaultNoPercentageConfig() {
    ArchaiusUtils.setProperty("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.percent", null);
    assertNull(DynamicProperty.getInstance("servicecomb.governance.Consumer._global.policy.fault.protocols.rest.delay.percent").getString());
    DelayFault delayFault = new DelayFault();
    FaultParam faultParam = new FaultParam(1);
    Vertx vertx = VertxUtils.getOrCreateVertxByName("faultinjectionTest", null);
    faultParam.setVertx(vertx);
    Holder<String> resultHolder = new Holder<>();
    delayFault.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)

Aggregations

Holder (org.apache.servicecomb.foundation.common.Holder)108 Test (org.junit.Test)88 MockUp (mockit.MockUp)36 AtomicLong (java.util.concurrent.atomic.AtomicLong)28 Invocation (org.apache.servicecomb.core.Invocation)24 Expectations (mockit.Expectations)22 Vertx (io.vertx.core.Vertx)20 List (java.util.List)18 Response (org.apache.servicecomb.swagger.invocation.Response)18 Mock (mockit.Mock)16 HttpServerFilterBaseForTest (org.apache.servicecomb.common.rest.filter.HttpServerFilterBaseForTest)16 ArrayList (java.util.ArrayList)14 Map (java.util.Map)14 SCBEngine (org.apache.servicecomb.core.SCBEngine)14 ExpectedException (org.junit.rules.ExpectedException)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 Status (javax.ws.rs.core.Response.Status)12 Deencapsulation (mockit.Deencapsulation)12 Mocked (mockit.Mocked)12 ConfigUtil (org.apache.servicecomb.config.ConfigUtil)12