tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 1 | package org.onlab.nio; |
| 2 | |
| 3 | import org.junit.After; |
| 4 | import org.junit.Before; |
| 5 | import org.junit.Test; |
| 6 | |
| 7 | import java.io.IOException; |
| 8 | import java.nio.ByteBuffer; |
| 9 | import java.nio.channels.ByteChannel; |
| 10 | import java.nio.channels.ClosedChannelException; |
| 11 | import java.nio.channels.SelectableChannel; |
| 12 | import java.nio.channels.SelectionKey; |
| 13 | import java.nio.channels.Selector; |
| 14 | import java.nio.channels.spi.SelectorProvider; |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNull; |
| 20 | |
| 21 | /** |
| 22 | * Tests of the message message stream implementation. |
| 23 | */ |
| 24 | public class MessageStreamTest { |
| 25 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 26 | private static final int SIZE = 64; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 27 | private static final int BIG_SIZE = 32 * 1024; |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 28 | |
| 29 | private TestMessage message; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 30 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 31 | private TestIOLoop loop; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 32 | private TestByteChannel channel; |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 33 | private TestMessageStream stream; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 34 | private TestKey key; |
| 35 | |
| 36 | @Before |
| 37 | public void setUp() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 38 | loop = new TestIOLoop(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 39 | channel = new TestByteChannel(); |
| 40 | key = new TestKey(channel); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 41 | stream = loop.createStream(channel); |
| 42 | stream.setKey(key); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 43 | stream.setNonStrict(); |
| 44 | message = new TestMessage(SIZE, 0, 0, stream.padding()); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @After |
| 48 | public void tearDown() { |
| 49 | loop.shutdown(); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 50 | stream.close(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 53 | // Validates the state of the message stream |
| 54 | private void validate(boolean wp, boolean fr, int read, int written) { |
| 55 | assertEquals(wp, stream.isWritePending()); |
| 56 | assertEquals(fr, stream.isFlushRequired()); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 57 | assertEquals(read, channel.readBytes); |
| 58 | assertEquals(written, channel.writtenBytes); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void endOfStream() throws IOException { |
| 63 | channel.close(); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 64 | List<TestMessage> messages = stream.read(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 65 | assertNull(messages); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void bufferGrowth() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 70 | // Create a stream for big messages and test the growth. |
| 71 | stream = new TestMessageStream(BIG_SIZE, channel, loop); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 72 | TestMessage bigMessage = new TestMessage(BIG_SIZE, 0, 0, stream.padding()); |
| 73 | |
| 74 | stream.write(bigMessage); |
| 75 | stream.write(bigMessage); |
| 76 | stream.write(bigMessage); |
| 77 | stream.write(bigMessage); |
| 78 | stream.write(bigMessage); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void discardBeforeKey() { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 83 | // Create a stream that does not yet have the key set and discard it. |
| 84 | stream = loop.createStream(channel); |
| 85 | assertNull(stream.key()); |
| 86 | stream.close(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 87 | // There is not key, so nothing to check; we just expect no problem. |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void bufferedRead() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 92 | channel.bytesToRead = SIZE + 4; |
| 93 | List<TestMessage> messages = stream.read(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 94 | assertEquals(1, messages.size()); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 95 | validate(false, false, SIZE + 4, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 96 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 97 | channel.bytesToRead = SIZE - 4; |
| 98 | messages = stream.read(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 99 | assertEquals(1, messages.size()); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 100 | validate(false, false, SIZE * 2, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void bufferedWrite() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 105 | validate(false, false, 0, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 106 | |
| 107 | // First write is immediate... |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 108 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 109 | validate(false, false, 0, SIZE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 110 | |
| 111 | // Second and third get buffered... |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 112 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 113 | validate(false, true, 0, SIZE); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 114 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 115 | validate(false, true, 0, SIZE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 116 | |
| 117 | // Reset write, which will flush if needed; the next write is again buffered |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 118 | stream.flushIfWriteNotPending(); |
| 119 | validate(false, false, 0, SIZE * 3); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 120 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 121 | validate(false, true, 0, SIZE * 3); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 122 | |
| 123 | // Select reset, which will flush if needed; the next write is again buffered |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 124 | stream.flushIfPossible(); |
| 125 | validate(false, false, 0, SIZE * 4); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 126 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 127 | validate(false, true, 0, SIZE * 4); |
| 128 | stream.flush(); |
| 129 | validate(false, true, 0, SIZE * 4); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void bufferedWriteList() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 134 | validate(false, false, 0, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 135 | |
| 136 | // First write is immediate... |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 137 | List<TestMessage> messages = new ArrayList<>(); |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 138 | messages.add(message); |
| 139 | messages.add(message); |
| 140 | messages.add(message); |
| 141 | messages.add(message); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 142 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 143 | stream.write(messages); |
| 144 | validate(false, false, 0, SIZE * 4); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 145 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 146 | stream.write(messages); |
| 147 | validate(false, true, 0, SIZE * 4); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 148 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 149 | stream.flushIfPossible(); |
| 150 | validate(false, false, 0, SIZE * 8); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void bufferedPartialWrite() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 155 | validate(false, false, 0, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 156 | |
| 157 | // First write is immediate... |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 158 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 159 | validate(false, false, 0, SIZE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 160 | |
| 161 | // Tell test channel to accept only half. |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 162 | channel.bytesToWrite = SIZE / 2; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 163 | |
| 164 | // Second and third get buffered... |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 165 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 166 | validate(false, true, 0, SIZE); |
| 167 | stream.flushIfPossible(); |
| 168 | validate(true, true, 0, SIZE + SIZE / 2); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void bufferedPartialWrite2() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 173 | validate(false, false, 0, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 174 | |
| 175 | // First write is immediate... |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 176 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 177 | validate(false, false, 0, SIZE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 178 | |
| 179 | // Tell test channel to accept only half. |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 180 | channel.bytesToWrite = SIZE / 2; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 181 | |
| 182 | // Second and third get buffered... |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 183 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 184 | validate(false, true, 0, SIZE); |
| 185 | stream.flushIfWriteNotPending(); |
| 186 | validate(true, true, 0, SIZE + SIZE / 2); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void bufferedReadWrite() throws IOException { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 191 | channel.bytesToRead = SIZE + 4; |
| 192 | List<TestMessage> messages = stream.read(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 193 | assertEquals(1, messages.size()); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 194 | validate(false, false, SIZE + 4, 0); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 195 | |
tom | 1ae3d16 | 2014-09-26 09:38:16 -0700 | [diff] [blame^] | 196 | stream.write(message); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 197 | validate(false, false, SIZE + 4, SIZE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 198 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 199 | channel.bytesToRead = SIZE - 4; |
| 200 | messages = stream.read(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 201 | assertEquals(1, messages.size()); |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 202 | validate(false, false, SIZE * 2, SIZE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Fake IO driver loop |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 206 | private static class TestIOLoop extends IOLoop<TestMessage, TestMessageStream> { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 207 | |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 208 | public TestIOLoop() throws IOException { |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 209 | super(500); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected TestMessageStream createStream(ByteChannel channel) { |
tom | 2d6d397 | 2014-09-25 22:38:57 -0700 | [diff] [blame] | 214 | return new TestMessageStream(SIZE, channel, this); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
| 218 | protected void processMessages(List<TestMessage> messages, |
| 219 | MessageStream<TestMessage> stream) { |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | // Byte channel test fixture |
| 225 | private static class TestByteChannel extends SelectableChannel implements ByteChannel { |
| 226 | |
| 227 | private static final int BUFFER_LENGTH = 1024; |
| 228 | byte[] bytes = new byte[BUFFER_LENGTH]; |
| 229 | int bytesToWrite = BUFFER_LENGTH; |
| 230 | int bytesToRead = BUFFER_LENGTH; |
| 231 | int writtenBytes = 0; |
| 232 | int readBytes = 0; |
| 233 | |
| 234 | @Override |
| 235 | public int read(ByteBuffer dst) throws IOException { |
| 236 | int l = Math.min(dst.remaining(), bytesToRead); |
| 237 | if (bytesToRead > 0) { |
| 238 | readBytes += l; |
| 239 | dst.put(bytes, 0, l); |
| 240 | } |
| 241 | return l; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public int write(ByteBuffer src) throws IOException { |
| 246 | int l = Math.min(src.remaining(), bytesToWrite); |
| 247 | writtenBytes += l; |
| 248 | src.get(bytes, 0, l); |
| 249 | return l; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Object blockingLock() { |
| 254 | return null; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public SelectableChannel configureBlocking(boolean arg0) throws IOException { |
| 259 | return null; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public boolean isBlocking() { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public boolean isRegistered() { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public SelectionKey keyFor(Selector arg0) { |
| 274 | return null; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public SelectorProvider provider() { |
| 279 | return null; |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public SelectionKey register(Selector arg0, int arg1, Object arg2) |
| 284 | throws ClosedChannelException { |
| 285 | return null; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public int validOps() { |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | protected void implCloseChannel() throws IOException { |
| 295 | bytesToRead = -1; |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | // Selection key text fixture |
| 301 | private static class TestKey extends SelectionKey { |
| 302 | |
| 303 | private SelectableChannel channel; |
| 304 | |
| 305 | public TestKey(TestByteChannel channel) { |
| 306 | this.channel = channel; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void cancel() { |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public SelectableChannel channel() { |
| 315 | return channel; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public int interestOps() { |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public SelectionKey interestOps(int ops) { |
| 325 | return null; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public boolean isValid() { |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public int readyOps() { |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public Selector selector() { |
| 340 | return null; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | } |