blob: 70f9cd5faf19cd2dfb45685b1f9374d3ab0e6b32 [file] [log] [blame]
toma7083182014-09-25 21:38:03 -07001package org.onlab.nio;
2
3import org.junit.Test;
4
5import java.io.IOException;
6import java.net.InetSocketAddress;
7import java.net.SocketAddress;
8import java.nio.channels.ServerSocketChannel;
9import java.util.concurrent.CountDownLatch;
10
11import static org.junit.Assert.assertEquals;
12import static org.onlab.junit.TestTools.delay;
13
14/**
15 * Unit tests for AcceptLoop.
16 */
17public class AcceptorLoopTest extends AbstractLoopTest {
18
19 private static final int PORT = 9876;
20
21 private static final SocketAddress SOCK_ADDR = new InetSocketAddress("127.0.0.1", PORT);
22
23 private static class MyAcceptLoop extends AcceptorLoop {
24 private final CountDownLatch loopStarted = new CountDownLatch(1);
25 private final CountDownLatch loopFinished = new CountDownLatch(1);
26 private final CountDownLatch runDone = new CountDownLatch(1);
27 private final CountDownLatch ceaseLatch = new CountDownLatch(1);
28
29 private int acceptCount = 0;
30
31 MyAcceptLoop() throws IOException {
32 super(500, SOCK_ADDR);
33 }
34
35 @Override
36 protected void acceptConnection(ServerSocketChannel ssc) throws IOException {
37 acceptCount++;
38 }
39
40 @Override
41 public void loop() throws IOException {
42 loopStarted.countDown();
43 super.loop();
44 loopFinished.countDown();
45 }
46
47 @Override
48 public void run() {
49 super.run();
50 runDone.countDown();
51 }
52
53 @Override
54 public void shutdown() {
55 super.shutdown();
56 ceaseLatch.countDown();
57 }
58 }
59
60 @Test
61// @Ignore("Doesn't shut down the socket")
62 public void basic() throws IOException {
63 MyAcceptLoop myAccLoop = new MyAcceptLoop();
64 AcceptorLoop accLoop = myAccLoop;
65 exec.execute(accLoop);
66 waitForLatch(myAccLoop.loopStarted, "loopStarted");
67 delay(200); // take a quick nap
68 accLoop.shutdown();
69 waitForLatch(myAccLoop.loopFinished, "loopFinished");
70 waitForLatch(myAccLoop.runDone, "runDone");
71 assertEquals(0, myAccLoop.acceptCount);
72 }
73}