use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionNo1NotSendNo2SuccessReuseChannelAsHttp.
@Test(timeout = 5000)
public void testInitiatorInteractionNo1NotSendNo2SuccessReuseChannelAsHttp() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
assertEquals(0, allActiveAllocationsCount(allocator));
try {
final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.<HttpObject>error(new RuntimeException("test error")), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
getresp.subscribe(subscriber);
subscriber.awaitTerminalEvent();
subscriber.assertError(RuntimeException.class);
subscriber.assertNoValues();
}
}).transport();
assertEquals(0, allActiveAllocationsCount(allocator));
final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
assertEquals(0, allActiveAllocationsCount(allocator));
assertSame(ch1, ch2);
} finally {
client.close();
server.unsubscribe();
}
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionSendPartRequestThenFailedAsHttps.
@Test(timeout = 5000)
public void testInitiatorInteractionSendPartRequestThenFailedAsHttps() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, enableSSL4ServerWithSelfSigned(), Feature.ENABLE_LOGGING_OVER_SSL);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), enableSSL4Client(), Feature.ENABLE_LOGGING_OVER_SSL);
assertEquals(0, allActiveAllocationsCount(allocator));
try {
final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
req.headers().set(HttpHeaderNames.CONTENT_LENGTH, 100);
final ConnectableObservable<HttpObject> errorOfEnd = Observable.<HttpObject>error(new RuntimeException("test error")).publish();
final Channel ch1 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.concat(Observable.<HttpObject>just(req), errorOfEnd), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
getresp.subscribe(subscriber);
// server side recv req
final HttpTrade trade = trades.take();
assertTrue(trade.isActive());
// fire error
errorOfEnd.connect();
subscriber.awaitTerminalEvent();
subscriber.assertError(RuntimeException.class);
subscriber.assertNoValues();
TerminateAware.Util.awaitTerminated(trade);
assertTrue(!trade.isActive());
}
}, new Action1<WriteCtrl>() {
@Override
public void call(final WriteCtrl writeCtrl) {
writeCtrl.setFlushPerWrite(true);
}
}).transport();
assertEquals(0, allActiveAllocationsCount(allocator));
final Channel ch2 = (Channel) startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), standardInteraction(allocator, trades)).transport();
assertEquals(0, allActiveAllocationsCount(allocator));
assertNotSame(ch1, ch2);
} finally {
client.close();
server.unsubscribe();
}
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionDisconnectFromServerAfterConnectedAsHttp.
@Test(timeout = 5000)
public void testInitiatorInteractionDisconnectFromServerAfterConnectedAsHttp() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
assertEquals(0, allActiveAllocationsCount(allocator));
try {
startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(fullHttpRequest()), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final TestSubscriber<DisposableWrapper<FullHttpResponse>> subscriber = new TestSubscriber<>();
getresp.subscribe(subscriber);
// server side recv req
final HttpTrade trade = trades.take();
// recv request from client side
trade.inbound().toCompletable().await();
// disconnect
trade.close();
subscriber.awaitTerminalEvent();
subscriber.assertError(TransportException.class);
subscriber.assertNotCompleted();
subscriber.assertNoValues();
}
});
assertEquals(0, allActiveAllocationsCount(allocator));
} finally {
client.close();
server.unsubscribe();
}
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionSuccessAsHttp10ConnectionClose.
@Test(timeout = 5000)
public void testInitiatorInteractionSuccessAsHttp10ConnectionClose() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
try {
final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
cached.subscribe();
// server side recv req
final HttpTrade trade = trades.take();
// recv all request
trade.inbound().doOnNext(DISPOSE_EACH).toCompletable().await();
assertEquals(0, allActiveAllocationsCount(allocator));
final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
assertEquals(1, allActiveAllocationsCount(allocator));
// for HTTP 1.0 Connection: Close response behavior
final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
// missing Content-Length
// response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
trade.outbound(Observable.just(fullrespfromsvr));
// wait for recv all resp at client side
cached.toCompletable().await();
svrRespContent.release();
// holder create clientside resp's content
assertEquals(1, allActiveAllocationsCount(allocator));
assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
}
});
} finally {
assertEquals(0, allActiveAllocationsCount(allocator));
client.close();
server.unsubscribe();
}
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionStillActiveAsHttp10ConnectionClose.
// (timeout=5000)
@Test
public void testInitiatorInteractionStillActiveAsHttp10ConnectionClose() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
final String addr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(addr, trades, Feature.ENABLE_LOGGING);
final DefaultHttpClient client = new DefaultHttpClient(new TestChannelCreator(), Feature.ENABLE_LOGGING);
try {
final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/");
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
startInteraction(client.initiator().remoteAddress(new LocalAddress(addr)), Observable.just(request), new Interaction() {
@Override
public void interact(final HttpInitiator initiator, final Observable<DisposableWrapper<FullHttpResponse>> getresp) throws Exception {
final Observable<DisposableWrapper<FullHttpResponse>> cached = getresp.cache();
cached.subscribe();
// server side recv req
final HttpTrade trade = trades.take();
// recv all request
trade.inbound().toCompletable().await();
final ByteBuf svrRespContent = allocator.buffer(CONTENT.length).writeBytes(CONTENT);
// for HTTP 1.0 Connection: Close response behavior
final FullHttpResponse fullrespfromsvr = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK, svrRespContent);
fullrespfromsvr.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
// missing Content-Length
// response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
fullrespfromsvr.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
trade.outbound(Observable.just(fullrespfromsvr));
// wait for recv all resp at client side
cached.toCompletable().await();
svrRespContent.release();
TerminateAware.Util.awaitTerminated(trade);
final Channel channel = (Channel) initiator.transport();
assertTrue(!channel.isActive());
assertTrue(initiator.isActive());
assertTrue(Arrays.equals(dumpResponseContentAsBytes(cached), CONTENT));
}
});
} finally {
assertEquals(0, allActiveAllocationsCount(allocator));
client.close();
server.unsubscribe();
}
}
Aggregations