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