|
41 | 41 |
|
42 | 42 | import java.io.IOException; |
43 | 43 | import java.nio.ByteBuffer; |
| 44 | +import java.util.Random; |
44 | 45 | import java.util.concurrent.ExecutorService; |
45 | 46 | import java.util.concurrent.Executors; |
| 47 | +import java.util.concurrent.Semaphore; |
46 | 48 | import java.util.concurrent.TimeUnit; |
| 49 | +import java.util.concurrent.atomic.AtomicBoolean; |
47 | 50 |
|
48 | 51 | import org.glassfish.jersey.internal.LocalizationMessages; |
49 | 52 |
|
|
52 | 55 | import static org.junit.Assert.assertNotEquals; |
53 | 56 | import static org.junit.Assert.assertNotNull; |
54 | 57 | import static org.junit.Assert.assertNull; |
| 58 | +import static org.junit.Assert.assertTrue; |
55 | 59 | import static org.junit.Assert.fail; |
56 | 60 |
|
57 | 61 | /** |
|
61 | 65 | */ |
62 | 66 | public class ByteBufferInputStreamTest { |
63 | 67 |
|
| 68 | + @Test |
| 69 | + public void testBlockingReadAByteEmptyStream() throws Exception { |
| 70 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 71 | + bbis.closeQueue(); |
| 72 | + assertEquals(-1, bbis.read()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testNonBlockingReadAByteEmptyStream() throws Exception { |
| 77 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 78 | + bbis.closeQueue(); |
| 79 | + assertEquals(-1, bbis.tryRead()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testBlockingReadByteArrayEmptyStream() throws Exception { |
| 84 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 85 | + bbis.closeQueue(); |
| 86 | + byte[] buf = new byte[1024]; |
| 87 | + assertEquals(-1, bbis.read(buf)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testNonBlockingReadByteArrayEmptyStream() throws Exception { |
| 92 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 93 | + bbis.closeQueue(); |
| 94 | + byte[] buf = new byte[1024]; |
| 95 | + assertEquals(-1, bbis.tryRead(buf)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testBlockingReadByteArrayFromFinishedExactLengthStream() throws Exception { |
| 100 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 101 | + byte[] sourceData = new byte[1024]; |
| 102 | + new Random().nextBytes(sourceData); |
| 103 | + ByteBuffer byteBuf = ByteBuffer.wrap(sourceData); |
| 104 | + bbis.put(byteBuf); |
| 105 | + bbis.closeQueue(); |
| 106 | + byte[] buf = new byte[1024]; |
| 107 | + assertEquals(1024, bbis.read(buf)); |
| 108 | + // no more data to read; so it should return -1 |
| 109 | + assertEquals(-1, bbis.read(buf)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testNonBlockingReadByteArrayFromFinishedExactLengthStream() throws Exception { |
| 114 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 115 | + byte[] sourceData = new byte[1024]; |
| 116 | + new Random().nextBytes(sourceData); |
| 117 | + ByteBuffer byteBuf = ByteBuffer.wrap(sourceData); |
| 118 | + bbis.put(byteBuf); |
| 119 | + byte[] buf = new byte[1024]; |
| 120 | + assertEquals(1024, bbis.tryRead(buf)); |
| 121 | + // the queue has not been close; so it should return 0 |
| 122 | + assertEquals(0, bbis.tryRead(buf)); |
| 123 | + bbis.closeQueue(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testBlockingReadByteArrayFromUnfinishedExactLengthStream() throws Exception { |
| 128 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 129 | + byte[] sourceData = new byte[1024]; |
| 130 | + new Random().nextBytes(sourceData); |
| 131 | + ByteBuffer byteBuf = ByteBuffer.wrap(sourceData); |
| 132 | + bbis.put(byteBuf); |
| 133 | + final byte[] buf = new byte[1024]; |
| 134 | + assertEquals(1024, bbis.read(buf)); |
| 135 | + final AtomicBoolean closed = new AtomicBoolean(false); |
| 136 | + final Semaphore s = new Semaphore(1); |
| 137 | + s.acquire(); |
| 138 | + Thread t = new Thread(new Runnable() { |
| 139 | + @Override |
| 140 | + public void run() { |
| 141 | + try { |
| 142 | + // it should return -1 since there is no more data |
| 143 | + assertEquals(-1, bbis.read(buf)); |
| 144 | + // it should only reach here if the stream has been closed |
| 145 | + assertTrue(closed.get()); |
| 146 | + } catch (IOException e) { |
| 147 | + e.printStackTrace(); |
| 148 | + } finally { |
| 149 | + s.release(); |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + t.start(); |
| 154 | + Thread.sleep(500); |
| 155 | + closed.set(true); |
| 156 | + bbis.closeQueue(); |
| 157 | + // wait until the job is done |
| 158 | + s.acquire(); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testNonBlockingReadByteArrayFromUnfinishedExactLengthStream() throws Exception { |
| 163 | + final ByteBufferInputStream bbis = new ByteBufferInputStream(); |
| 164 | + byte[] sourceData = new byte[1024]; |
| 165 | + new Random().nextBytes(sourceData); |
| 166 | + ByteBuffer byteBuf = ByteBuffer.wrap(sourceData); |
| 167 | + bbis.put(byteBuf); |
| 168 | + bbis.closeQueue(); |
| 169 | + byte[] buf = new byte[1024]; |
| 170 | + assertEquals(1024, bbis.tryRead(buf)); |
| 171 | + assertEquals(-1, bbis.tryRead(buf)); |
| 172 | + } |
| 173 | + |
64 | 174 | /** |
65 | 175 | * Test for non blocking single-byte read of the stream. |
66 | 176 | * |
|
0 commit comments