use of java.io.PipedOutputStream in project activemq-artemis by apache.
the class LargeMessageBufferTest method testStreamData.
@Test
public void testStreamData() throws Exception {
final LargeMessageControllerImpl outBuffer = new LargeMessageControllerImpl(new FakeConsumerInternal(), 1024 * 11 + 123, 1000);
final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);
final AtomicInteger errors = new AtomicInteger(0);
// Done reading 3 elements
final CountDownLatch done1 = new CountDownLatch(1);
// Done with the thread
final CountDownLatch done2 = new CountDownLatch(1);
final AtomicInteger count = new AtomicInteger(0);
final AtomicInteger totalBytes = new AtomicInteger(0);
Thread treader = new Thread("treader") {
@Override
public void run() {
try {
byte[] line = new byte[1024];
int dataRead = 0;
while (dataRead >= 0) {
dataRead = input.read(line);
if (dataRead > 0) {
totalBytes.addAndGet(dataRead);
if (count.incrementAndGet() == 3) {
done1.countDown();
}
}
}
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
} finally {
done1.countDown();
done2.countDown();
}
}
};
treader.setDaemon(true);
treader.start();
for (int i = 0; i < 3; i++) {
outBuffer.addPacket(new byte[1024], 1, true);
}
outBuffer.setOutputStream(output);
final CountDownLatch waiting = new CountDownLatch(1);
Thread twaiter = new Thread("twaiter") {
@Override
public void run() {
try {
outBuffer.waitCompletion(0);
waiting.countDown();
} catch (Exception e) {
e.printStackTrace();
errors.incrementAndGet();
}
}
};
twaiter.setDaemon(true);
twaiter.start();
Assert.assertTrue(done1.await(10, TimeUnit.SECONDS));
Assert.assertEquals(3, count.get());
Assert.assertEquals(1024 * 3, totalBytes.get());
for (int i = 0; i < 8; i++) {
outBuffer.addPacket(new byte[1024], 1, true);
}
Assert.assertEquals(1, waiting.getCount());
outBuffer.addPacket(new byte[123], 1, false);
Assert.assertTrue(done2.await(10, TimeUnit.SECONDS));
Assert.assertTrue(waiting.await(10, TimeUnit.SECONDS));
Assert.assertEquals(12, count.get());
Assert.assertEquals(1024 * 11 + 123, totalBytes.get());
treader.join();
twaiter.join();
Assert.assertEquals(0, errors.get());
input.close();
}
use of java.io.PipedOutputStream in project new-cloud by xie-summer.
the class ProtocolTest method buildACommand.
@Test
public void buildACommand() throws IOException {
PipedInputStream pis = new PipedInputStream();
BufferedInputStream bis = new BufferedInputStream(pis);
PipedOutputStream pos = new PipedOutputStream(pis);
RedisOutputStream ros = new RedisOutputStream(pos);
Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
ros.flush();
pos.close();
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
int b;
StringBuilder sb = new StringBuilder();
while ((b = bis.read()) != -1) {
sb.append((char) b);
}
assertEquals(expectedCommand, sb.toString());
}
use of java.io.PipedOutputStream in project cachecloud by sohutv.
the class ProtocolTest method buildACommand.
@Test
public void buildACommand() throws IOException {
PipedInputStream pis = new PipedInputStream();
BufferedInputStream bis = new BufferedInputStream(pis);
PipedOutputStream pos = new PipedOutputStream(pis);
RedisOutputStream ros = new RedisOutputStream(pos);
Protocol.sendCommand(ros, Protocol.Command.GET, "SOMEKEY".getBytes(Protocol.CHARSET));
ros.flush();
pos.close();
String expectedCommand = "*2\r\n$3\r\nGET\r\n$7\r\nSOMEKEY\r\n";
int b;
StringBuilder sb = new StringBuilder();
while ((b = bis.read()) != -1) {
sb.append((char) b);
}
assertEquals(expectedCommand, sb.toString());
}
use of java.io.PipedOutputStream in project java-chassis by ServiceComb.
the class DownloadSchema method slowInputStream.
@ApiResponses({ @ApiResponse(code = 200, response = File.class, message = "") })
@GetMapping(path = "/slowInputStream")
public ResponseEntity<InputStream> slowInputStream() throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
slowInputStreamThread = new Thread(() -> {
Thread.currentThread().setName("download thread");
byte[] bytes = "1".getBytes();
for (; ; ) {
try {
out.write(bytes);
out.flush();
Thread.sleep(1000);
} catch (Throwable e) {
break;
}
}
try {
out.close();
} catch (final IOException ioe) {
// ignore
}
});
slowInputStreamThread.start();
ResponseEntity<InputStream> responseEntity = ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=slowInputStream.txt").body(in);
return responseEntity;
}
use of java.io.PipedOutputStream in project nanohttpd by NanoHttpd.
the class HttpKeepAliveTest method testManyRequests.
/**
* Issue the given request many times to check whether an error occurs. For
* this test, a small stack size is used, since a stack overflow is among
* the possible errors.
*
* @param request
* The request to issue
* @param expected
* The expected response
*/
public void testManyRequests(final String request, final String[] expected) throws Exception {
Runnable r = new Runnable() {
@Override
public void run() {
try {
PipedOutputStream requestStream = new PipedOutputStream();
PipedInputStream inputStream = new PipedInputStream(requestStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DefaultTempFileManager tempFileManager = new DefaultTempFileManager();
try {
HTTPSession session = HttpKeepAliveTest.this.testServer.createSession(tempFileManager, inputStream, outputStream);
for (int i = 0; i < 2048; i++) {
requestStream.write(request.getBytes());
requestStream.flush();
outputStream.reset();
session.execute();
assertResponse(outputStream, expected);
}
// Finally, try "Connection: Close"
String closeReq = request.replaceAll("HTTP/1.1", "HTTP/1.1\r\nConnection: Close");
expected[3] = "Connection: close";
requestStream.write(closeReq.getBytes());
outputStream.reset();
requestStream.flush();
// SocketException:
try {
session.execute();
} catch (java.net.SocketException se) {
junit.framework.Assert.assertEquals(se.getMessage(), "NanoHttpd Shutdown");
}
assertResponse(outputStream, expected);
} finally {
tempFileManager.clear();
}
} catch (Throwable t) {
HttpKeepAliveTest.this.error = t;
}
}
};
Thread t = new Thread(null, r, "Request Thread", 1 << 17);
t.start();
t.join();
if (this.error != null) {
fail("" + this.error);
this.error.printStackTrace();
}
}
Aggregations