1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test @summary com.apple.junit.java.nio.Selector
  26  * @library ../../regtesthelpers
  27  * @run junit SelectorKqueueTest
  28  */
  29 import junit.framework.*;
  30 
  31 import java.io.IOException;
  32 import java.net.InetSocketAddress;
  33 import java.nio.ByteBuffer;
  34 import java.nio.channels.*;
  35 import java.util.Iterator;
  36 
  37 public class SelectorKqueueTest extends TestCase {
  38 
  39     public static final boolean DEBUG = false;
  40     public static final int PORT = 1234;
  41     private Pipe signal;
  42     private Thread listener;
  43     private volatile boolean running;
  44 
  45     public static Test suite() {
  46         return new TestSuite(SelectorKqueueTest.class);
  47     }
  48 
  49     public static void main(String[] args) {
  50         junit.textui.TestRunner.run(suite());
  51     }
  52 
  53     public void testSelector() {
  54         SelectorKqueueTest test;
  55 
  56         try {
  57             test = new SelectorKqueueTest();
  58             test.start();
  59             Thread.sleep(3000);
  60             test.stop();
  61         } catch (Throwable x) {
  62             x.printStackTrace(System.err);
  63         }
  64     }
  65 
  66     public SelectorKqueueTest() {
  67         super();
  68         signal = null;
  69         listener = null;
  70         running = false;
  71     }
  72 
  73     public void start() throws IOException {
  74         if (DEBUG) {
  75             System.out.println("start");
  76         };
  77 
  78         running = true;
  79 
  80         signal = Pipe.open();
  81         signal.source().configureBlocking(false);
  82 
  83         listener = new Thread() {
  84             public void run() {
  85                 try {
  86                     listen();
  87                 } catch (Throwable x) {
  88                     x.printStackTrace(System.err);
  89                 }
  90             }
  91         };
  92         listener.setPriority(Thread.NORM_PRIORITY);
  93         listener.setDaemon(true);
  94         listener.start();
  95     }
  96 
  97     public void stop() throws IOException, InterruptedException {
  98         if (DEBUG) {
  99             System.out.println("stop");
 100         };
 101 
 102         running = false;
 103         signal();
 104         listener.join();
 105 
 106         signal.sink().close();
 107         signal.source().close();
 108     }
 109 
 110     protected void listen() throws IOException {
 111         ServerSocketChannel server;
 112         Selector selector;
 113         SelectionKey key;
 114         Iterator iter;
 115         int ret;
 116 
 117         if (DEBUG) {
 118             System.out.println("start listening");
 119         };
 120 
 121         selector = Selector.open();
 122 
 123         server = ServerSocketChannel.open();
 124         server.configureBlocking(false);
 125         server.socket().setReuseAddress(true);
 126         server.socket().bind(new InetSocketAddress(PORT), 5);
 127 
 128         while (running) {
 129             server.register(selector, SelectionKey.OP_ACCEPT);
 130             signal.source().register(selector, SelectionKey.OP_READ);
 131 
 132             ret = selector.select();
 133             if (DEBUG) {
 134                 System.out.println("got #" + ret);
 135             };
 136 
 137             iter = selector.selectedKeys().iterator();
 138             while (iter.hasNext()) {
 139                 key = (SelectionKey) iter.next();
 140                 iter.remove();
 141 
 142                 if (key.isValid() && key.isAcceptable()) {
 143                     if (DEBUG) {
 144                         System.out.println(" accept");
 145                     };
 146                 }
 147 
 148                 if (key.isValid() && key.isReadable()) {
 149                     if (DEBUG) {
 150                         System.out.println(" read");
 151                     };
 152                 }
 153 
 154                 if (key.isValid() && key.isWritable()) {
 155                     if (DEBUG) {
 156                         System.out.println(" write");
 157                     };
 158                 }
 159             }
 160         }
 161 
 162         server.close();
 163         selector.close();
 164 
 165         if (DEBUG) {
 166             System.out.println("stop listening");
 167         };
 168     }
 169 
 170     protected void signal() throws IOException {
 171         byte[] dummy = new byte[]{(byte) 0};
 172 
 173 //System.out.println("signal server");
 174         signal.sink().write(ByteBuffer.wrap(dummy));
 175     }
 176 }