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