blob: 0ee83bdc5d595a221c5426de34876edb0d74848c [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 */
toma7083182014-09-25 21:38:03 -070019package org.onlab.nio;
20
21import org.junit.Test;
22
23import java.io.IOException;
24import java.net.InetSocketAddress;
25import java.net.SocketAddress;
26import java.nio.channels.ServerSocketChannel;
27import java.util.concurrent.CountDownLatch;
28
29import static org.junit.Assert.assertEquals;
30import static org.onlab.junit.TestTools.delay;
31
32/**
33 * Unit tests for AcceptLoop.
34 */
35public 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}