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.Test; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.net.InetSocketAddress; |
| 22 | import java.net.SocketAddress; |
| 23 | import java.nio.channels.ServerSocketChannel; |
| 24 | import java.util.concurrent.CountDownLatch; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.onlab.junit.TestTools.delay; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for AcceptLoop. |
| 31 | */ |
| 32 | public class AcceptorLoopTest extends AbstractLoopTest { |
| 33 | |
| 34 | private static final int PORT = 9876; |
| 35 | |
| 36 | private static final SocketAddress SOCK_ADDR = new InetSocketAddress("127.0.0.1", PORT); |
| 37 | |
| 38 | private static class MyAcceptLoop extends AcceptorLoop { |
| 39 | private final CountDownLatch loopStarted = new CountDownLatch(1); |
| 40 | private final CountDownLatch loopFinished = new CountDownLatch(1); |
| 41 | private final CountDownLatch runDone = new CountDownLatch(1); |
| 42 | private final CountDownLatch ceaseLatch = new CountDownLatch(1); |
| 43 | |
| 44 | private int acceptCount = 0; |
| 45 | |
| 46 | MyAcceptLoop() throws IOException { |
| 47 | super(500, SOCK_ADDR); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected void acceptConnection(ServerSocketChannel ssc) throws IOException { |
| 52 | acceptCount++; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void loop() throws IOException { |
| 57 | loopStarted.countDown(); |
| 58 | super.loop(); |
| 59 | loopFinished.countDown(); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void run() { |
| 64 | super.run(); |
| 65 | runDone.countDown(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void shutdown() { |
| 70 | super.shutdown(); |
| 71 | ceaseLatch.countDown(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | // @Ignore("Doesn't shut down the socket") |
| 77 | public void basic() throws IOException { |
| 78 | MyAcceptLoop myAccLoop = new MyAcceptLoop(); |
| 79 | AcceptorLoop accLoop = myAccLoop; |
| 80 | exec.execute(accLoop); |
| 81 | waitForLatch(myAccLoop.loopStarted, "loopStarted"); |
| 82 | delay(200); // take a quick nap |
| 83 | accLoop.shutdown(); |
| 84 | waitForLatch(myAccLoop.loopFinished, "loopFinished"); |
| 85 | waitForLatch(myAccLoop.runDone, "runDone"); |
| 86 | assertEquals(0, myAccLoop.acceptCount); |
| 87 | } |
| 88 | } |