blob: 82deb7eedf514f13a69a47940272f8d085d9268b [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
toma7083182014-09-25 21:38:03 -070016package org.onlab.nio;
17
18import org.junit.Test;
19
20import java.io.IOException;
21import java.net.InetSocketAddress;
22import java.net.SocketAddress;
23import java.nio.channels.ServerSocketChannel;
24import java.util.concurrent.CountDownLatch;
25
26import static org.junit.Assert.assertEquals;
27import static org.onlab.junit.TestTools.delay;
28
29/**
30 * Unit tests for AcceptLoop.
31 */
32public 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}