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