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.Before; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 19 | import org.junit.Ignore; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.net.InetAddress; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 23 | import java.util.Random; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 24 | import java.util.logging.Level; |
| 25 | import java.util.logging.Logger; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 26 | |
| 27 | import static org.onlab.junit.TestTools.delay; |
| 28 | |
| 29 | /** |
| 30 | * Integration test for the select, accept and IO loops. |
| 31 | */ |
| 32 | public class IOLoopIntegrationTest { |
| 33 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 34 | private static final int THREADS = 6; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 35 | private static final int TIMEOUT = 60; |
| 36 | private static final int MESSAGE_LENGTH = 128; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 37 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 38 | private static final int MILLION = 1000000; |
| 39 | private static final int MSG_COUNT = 40 * MILLION; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 40 | |
| 41 | @Before |
| 42 | public void warmUp() throws Exception { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 43 | Logger.getLogger("").setLevel(Level.SEVERE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 44 | try { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 45 | runTest(MILLION, MESSAGE_LENGTH, 15); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 46 | } catch (Throwable e) { |
| 47 | System.err.println("Failed warmup but moving on."); |
| 48 | e.printStackTrace(); |
| 49 | } |
| 50 | } |
| 51 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 52 | // TODO: this test can not pass in some environments, need to be improved |
| 53 | @Ignore |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 54 | @Test |
| 55 | public void basic() throws Exception { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 56 | runTest(MILLION, MESSAGE_LENGTH, TIMEOUT); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 59 | public void longHaul() throws Exception { |
| 60 | runTest(MSG_COUNT, MESSAGE_LENGTH, TIMEOUT); |
| 61 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 62 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 63 | private void runTest(int count, int size, int timeout) throws Exception { |
| 64 | // Use a random port to prevent conflicts. |
| 65 | int port = IOLoopTestServer.PORT + new Random().nextInt(100); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 66 | |
| 67 | InetAddress ip = InetAddress.getLoopbackAddress(); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 68 | IOLoopTestServer server = new IOLoopTestServer(ip, THREADS, size, port); |
| 69 | IOLoopTestClient client = new IOLoopTestClient(ip, THREADS, count, size, port); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 70 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 71 | server.start(); |
| 72 | client.start(); |
| 73 | delay(100); // Pause to allow loops to get going |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 74 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 75 | client.await(timeout); |
| 76 | client.report(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 77 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 78 | server.stop(); |
| 79 | server.report(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | } |