tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 1 | package org.onlab.nio; |
| 2 | |
| 3 | import org.junit.Before; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 4 | import org.junit.Test; |
| 5 | |
| 6 | import java.net.InetAddress; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 7 | import java.util.Random; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 8 | import java.util.logging.Level; |
| 9 | import java.util.logging.Logger; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 10 | |
| 11 | import static org.onlab.junit.TestTools.delay; |
| 12 | |
| 13 | /** |
| 14 | * Integration test for the select, accept and IO loops. |
| 15 | */ |
| 16 | public class IOLoopIntegrationTest { |
| 17 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 18 | private static final int THREADS = 6; |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 19 | private static final int TIMEOUT = 60; |
| 20 | private static final int MESSAGE_LENGTH = 128; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 21 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 22 | private static final int MILLION = 1000000; |
| 23 | private static final int MSG_COUNT = 40 * MILLION; |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 24 | |
| 25 | @Before |
| 26 | public void warmUp() throws Exception { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 27 | Logger.getLogger("").setLevel(Level.SEVERE); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 28 | try { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 29 | runTest(MILLION, MESSAGE_LENGTH, 15); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 30 | } catch (Throwable e) { |
| 31 | System.err.println("Failed warmup but moving on."); |
| 32 | e.printStackTrace(); |
| 33 | } |
| 34 | } |
| 35 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 36 | |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 37 | @Test |
| 38 | public void basic() throws Exception { |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 39 | runTest(MILLION, MESSAGE_LENGTH, TIMEOUT); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 40 | } |
| 41 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 42 | public void longHaul() throws Exception { |
| 43 | runTest(MSG_COUNT, MESSAGE_LENGTH, TIMEOUT); |
| 44 | } |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 45 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 46 | private void runTest(int count, int size, int timeout) throws Exception { |
| 47 | // Use a random port to prevent conflicts. |
| 48 | int port = IOLoopTestServer.PORT + new Random().nextInt(100); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 49 | |
| 50 | InetAddress ip = InetAddress.getLoopbackAddress(); |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 51 | IOLoopTestServer server = new IOLoopTestServer(ip, THREADS, size, port); |
| 52 | IOLoopTestClient client = new IOLoopTestClient(ip, THREADS, count, size, port); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 53 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 54 | server.start(); |
| 55 | client.start(); |
| 56 | delay(100); // Pause to allow loops to get going |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 57 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 58 | client.await(timeout); |
| 59 | client.report(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 60 | |
tom | 74d4965 | 2014-09-25 23:48:46 -0700 | [diff] [blame] | 61 | server.stop(); |
| 62 | server.report(); |
tom | a708318 | 2014-09-25 21:38:03 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | } |