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