use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionStillActiveAsHttps10ConnectionClose.
@Test(timeout = 5000)
public void testInitiatorInteractionStillActiveAsHttps10ConnectionClose() 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);
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();
}
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForFullRequestSourceAndMultiInboundThenDumpFullRequestContent.
@Test
public final void testTradeForFullRequestSourceAndMultiInboundThenDumpFullRequestContent() throws IOException {
final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Nettys4Test.buildByteBuf(REQ_CONTENT));
final EmbeddedChannel channel = new EmbeddedChannel();
final HttpTrade trade = new DefaultHttpTrade(channel);
final AtomicReference<DefaultFullHttpRequest> ref1 = new AtomicReference<DefaultFullHttpRequest>();
trade.inbound().subscribe(new Action1<DisposableWrapper<HttpObject>>() {
@Override
public void call(DisposableWrapper<HttpObject> dwh) {
ref1.set((DefaultFullHttpRequest) dwh.unwrap());
}
});
final AtomicReference<DefaultFullHttpRequest> ref2 = new AtomicReference<DefaultFullHttpRequest>();
trade.inbound().subscribe(new Action1<DisposableWrapper<HttpObject>>() {
@Override
public void call(DisposableWrapper<HttpObject> dwh) {
ref2.set((DefaultFullHttpRequest) dwh.unwrap());
}
});
writeToInboundAndFlush(channel, request);
final byte[] firstReadBytes = Nettys.dumpByteBufAsBytes(ref1.get().content());
assertTrue(Arrays.equals(REQ_CONTENT.getBytes(Charsets.UTF_8), firstReadBytes));
final byte[] secondReadBytes = Nettys.dumpByteBufAsBytes(ref2.get().content());
assertTrue(Arrays.equals(REQ_CONTENT.getBytes(Charsets.UTF_8), secondReadBytes));
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForCallAbortAfterPartRequestThenPushError.
@Test
public final void testTradeForCallAbortAfterPartRequestThenPushError() {
final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
final EmbeddedChannel channel = new EmbeddedChannel();
final HttpTrade trade = new DefaultHttpTrade(channel);
// trade.inboundHolder().setMaxBlockSize(-1);
final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
trade.inbound().subscribe(reqSubscriber);
writeToInboundAndFlush(channel, request);
writeToInboundAndFlush(channel, req_contents[0]);
trade.close();
assertFalse(trade.isActive());
/* TODO, fix no terminal event
reqSubscriber.assertTerminalEvent();
reqSubscriber.assertError(Exception.class);
*/
reqSubscriber.assertValueCount(2);
reqSubscriber.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpTradeTestCase method testTradeForRequestPartError.
@Test
public final void testTradeForRequestPartError() {
final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
final List<DisposableWrapper<HttpObject>> part_req = new ArrayList<DisposableWrapper<HttpObject>>() {
private static final long serialVersionUID = 1L;
{
this.add(RxNettys.<HttpObject>wrap4release(request));
for (int idx = 0; idx < 5; idx++) {
this.add(RxNettys.<HttpObject>wrap4release(req_contents[idx]));
}
}
};
final EmbeddedChannel channel = new EmbeddedChannel();
final HttpTrade trade = new DefaultHttpTrade(channel);
// trade.inboundHolder().setMaxBlockSize(-1);
final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
trade.inbound().subscribe(reqSubscriber);
Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>just(req_contents[0]), Observable.<HttpObject>just(req_contents[1]), Observable.<HttpObject>just(req_contents[2]), Observable.<HttpObject>just(req_contents[3]), Observable.<HttpObject>just(req_contents[4])).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
channel.disconnect().syncUninterruptibly();
assertTrue(!trade.isActive());
// TODO, fix assert terminal event
// reqSubscriber.assertTerminalEvent();
// java.lang.AssertionError: Exceptions differ; expected: java.lang.RuntimeException: RequestPartError,
// actual: java.lang.RuntimeException: trade unactived
// reqSubscriber.assertError(Exception.class);
reqSubscriber.assertNotCompleted();
reqSubscriber.assertValues(part_req.toArray(new DisposableWrapper[0]));
}
use of org.jocean.idiom.DisposableWrapper in project jocean-http by isdom.
the class DefaultHttpClientTestCase method testInitiatorInteractionUnsubscribedAlreadyAsHttp.
@Test(timeout = 5000)
public void testInitiatorInteractionUnsubscribedAlreadyAsHttp() throws Exception {
// 配置 池化分配器 为 取消缓存,使用 Heap
configDefaultAllocator();
final PooledByteBufAllocator allocator = defaultAllocator();
final String addr = UUID.randomUUID().toString();
final BlockingQueue<HttpTrade> trades = new ArrayBlockingQueue<>(1);
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<>();
subscriber.unsubscribe();
final Subscription subscription = getresp.subscribe(subscriber);
assertTrue(subscription.isUnsubscribed());
subscriber.assertNoTerminalEvent();
subscriber.assertNoValues();
}
});
assertEquals(0, allActiveAllocationsCount(allocator));
} finally {
client.close();
server.unsubscribe();
}
}
Aggregations