Search in sources :

Example 1 with AsyncResponse

use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.

the class TestVertxRestTransport method testSendException.

@Test
public void testSendException() {
    boolean validAssert;
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    URIEndpointObject endpoint = Mockito.mock(URIEndpointObject.class);
    Endpoint end = Mockito.mock(Endpoint.class);
    Mockito.when(invocation.getEndpoint()).thenReturn(end);
    Mockito.when(invocation.getEndpoint().getAddress()).thenReturn(endpoint);
    try {
        validAssert = true;
        instance.send(invocation, asyncResp);
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertFalse(validAssert);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Endpoint(org.apache.servicecomb.core.Endpoint) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with AsyncResponse

use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.

the class TestConsumer method testInvocation.

@Test
public void testInvocation() {
    OperationMeta oOperationMeta = Mockito.mock(OperationMeta.class);
    SchemaMeta oSchemaMeta = Mockito.mock(SchemaMeta.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    List<Handler> oHandlerList = new ArrayList<>();
    Mockito.when(oSchemaMeta.getProviderHandlerChain()).thenReturn(oHandlerList);
    Mockito.when(oSchemaMeta.getMicroserviceName()).thenReturn("TMK");
    Mockito.when(oOperationMeta.getSchemaMeta()).thenReturn(oSchemaMeta);
    Endpoint oEndpoint = Mockito.mock(Endpoint.class);
    Transport oTransport = Mockito.mock(Transport.class);
    Mockito.when(oEndpoint.getTransport()).thenReturn(oTransport);
    Mockito.when(oOperationMeta.getOperationId()).thenReturn("TMK");
    Invocation oInvocation = new Invocation(oEndpoint, oOperationMeta, null);
    Assert.assertNotNull(oInvocation.getTransport());
    Assert.assertNotNull(oInvocation.getInvocationType());
    oInvocation.setResponseExecutor(Mockito.mock(Executor.class));
    Assert.assertNotNull(oInvocation.getResponseExecutor());
    Assert.assertNotNull(oInvocation.getSchemaMeta());
    Assert.assertNotNull(oInvocation.getOperationMeta());
    Assert.assertNull(oInvocation.getArgs());
    Assert.assertNotNull(oInvocation.getEndpoint());
    oInvocation.setEndpoint(null);
    Map<String, String> map = oInvocation.getContext();
    Assert.assertNotNull(map);
    String str = oInvocation.getSchemaId();
    Assert.assertEquals(null, str);
    String str1 = oInvocation.getMicroserviceName();
    Assert.assertEquals("TMK", str1);
    Map<String, Object> mapp = oInvocation.getHandlerContext();
    Assert.assertNotNull(mapp);
    Assert.assertEquals(true, oInvocation.getHandlerIndex() >= 0);
    oInvocation.setHandlerIndex(8);
    Assert.assertEquals("TMK", oInvocation.getOperationName());
    Assert.assertEquals("TMK", oInvocation.getMicroserviceName());
    boolean validAssert;
    try {
        validAssert = true;
        oInvocation.next(asyncResp);
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertFalse(validAssert);
}
Also used : ArrayList(java.util.ArrayList) SyncResponseExecutor(org.apache.servicecomb.core.provider.consumer.SyncResponseExecutor) Executor(java.util.concurrent.Executor) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 3 with AsyncResponse

use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.

the class TestShutdownHookHandler method testShutdownHookHandlerCount.

@Test
public void testShutdownHookHandlerCount(@Mocked Response response) throws Exception {
    Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", false);
    ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
    Assert.assertEquals(0, handler.getActiveCount());
    // no reply
    Invocation invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
        }
    }.getMockInstance();
    handler.handle(invocation, asyncResp -> {
    });
    Assert.assertEquals(1, requestCounter.get());
    Assert.assertEquals(1, handler.getActiveCount());
    // normal
    invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
            asyncResp.handle(response);
        }
    }.getMockInstance();
    handler.handle(invocation, asyncResp -> {
    });
    Assert.assertEquals(2, requestCounter.get());
    Assert.assertEquals(1, handler.getActiveCount());
    // next exception
    invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
            throw new Error();
        }
    }.getMockInstance();
    try {
        handler.handle(invocation, asyncResp -> {
        });
        Assert.assertFalse(true);
    } catch (Throwable e) {
        Assert.assertEquals(3, requestCounter.get());
        Assert.assertEquals(1, handler.getActiveCount());
    }
    AtomicLong responseCounter = Deencapsulation.getField(ShutdownHookHandler.INSTANCE, "responseCounter");
    responseCounter.incrementAndGet();
    Assert.assertEquals(0, handler.getActiveCount());
// reply exception
// TODO: should be fixed
// try {
// handler.handle(invocation, asyncResp -> {
// throw new Error();
// });
// 
// Assert.assertFalse(true);
// } catch (Throwable e) {
// Assert.assertEquals(3, requestCounter.get());
// Assert.assertEquals(1, handler.getActiveCount());
// }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Mock(mockit.Mock) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Test(org.junit.Test)

Example 4 with AsyncResponse

use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.

the class TestTransport method testEndpoint.

@Test
public void testEndpoint() throws Exception {
    Endpoint oEndpoint = new Endpoint(new Transport() {

        @Override
        public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
        }

        @Override
        public Object parseAddress(String address) {
            return "127.0.0.1";
        }

        @Override
        public boolean init() throws Exception {
            return true;
        }

        @Override
        public String getName() {
            return "test";
        }

        @Override
        public Endpoint getEndpoint() {
            return (new Endpoint(this, "testEndpoint"));
        }

        @Override
        public Endpoint getPublishEndpoint() throws Exception {
            return (new Endpoint(this, "testEndpoint"));
        }
    }, "rest://127.0.0.1:8080");
    oEndpoint.getTransport().init();
    Assert.assertEquals("rest://127.0.0.1:8080", oEndpoint.getEndpoint());
    Assert.assertEquals("127.0.0.1", oEndpoint.getAddress());
    Assert.assertEquals("test", oEndpoint.getTransport().getName());
    Assert.assertEquals("rest://127.0.0.1:8080", oEndpoint.getEndpoint().toString());
}
Also used : CacheEndpoint(org.apache.servicecomb.serviceregistry.cache.CacheEndpoint) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 5 with AsyncResponse

use of org.apache.servicecomb.swagger.invocation.AsyncResponse in project incubator-servicecomb-java-chassis by apache.

the class SwaggerProducerOperation method syncInvoke.

public void syncInvoke(SwaggerInvocation invocation, AsyncResponse asyncResp) {
    ContextUtils.setInvocationContext(invocation);
    Response response = doInvoke(invocation);
    ContextUtils.removeInvocationContext();
    asyncResp.handle(response);
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response)

Aggregations

AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)29 Invocation (org.apache.servicecomb.core.Invocation)20 Test (org.junit.Test)20 Response (org.apache.servicecomb.swagger.invocation.Response)16 MockUp (mockit.MockUp)10 ArrayList (java.util.ArrayList)9 Mock (mockit.Mock)8 List (java.util.List)7 Map (java.util.Map)6 Status (javax.ws.rs.core.Response.Status)6 Expectations (mockit.Expectations)6 Transport (org.apache.servicecomb.core.Transport)6 TransportManager (org.apache.servicecomb.core.transport.TransportManager)6 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 HashMap (java.util.HashMap)4 Holder (javax.xml.ws.Holder)4 Deencapsulation (mockit.Deencapsulation)4 Injectable (mockit.Injectable)4 Mocked (mockit.Mocked)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4