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